Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Draw a image into a window in Carbon

10 views
Skip to first unread message

Antrox

unread,
Mar 5, 2007, 6:12:17 PM3/5/07
to
Hi!
I have the position and the 16bits pixel value of an image and I would
like to draw into a window. I´ve read about Quartz but I don´t
understand how use createimage from the pixels and so draw the image.
Can you show me a way to do this. I´ve read that printing pixel by
pixel is so slow.

Thanks.

David Phillip Oster

unread,
Mar 6, 2007, 12:03:52 AM3/6/07
to
In article <1173136337.7...@c51g2000cwc.googlegroups.com>,
"Antrox" <antonio.g...@gmail.com> wrote:

> I have the position and the 16bits pixel value of an image and I would

> like to draw into a window. I扉e read about Quartz but I don愒


> understand how use createimage from the pixels and so draw the image.

> Can you show me a way to do this. I扉e read that printing pixel by
> pixel is so slow.

Carbon or Cocoa?

can you use a Control to hold the image, or do you want to do it
directly?

QuicktimeGraphicImporters, although a bit old fashioned now, give you a
lot of image loading and display power.

Do you men 16 bits per pixel, or 16 bits per color component?

Antrox

unread,
Mar 6, 2007, 5:45:54 AM3/6/07
to
On 6 mar, 06:03, David Phillip Oster <o...@ieee.org> wrote:
> In article <1173136337.770463.150...@c51g2000cwc.googlegroups.com>,

>
> "Antrox" <antonio.gil.gar...@gmail.com> wrote:
> > I have the position and the 16bits pixel value of an image and I would
> > like to draw into a window. I´ve read about Quartz but I don´t

> > understand how use createimage from the pixels and so draw the image.
> > Can you show me a way to do this. I´ve read that printing pixel by

> > pixel is so slow.
>
> Carbon or Cocoa?
>
> can you use a Control to hold the image, or do you want to do it
> directly?
>
> QuicktimeGraphicImporters, although a bit old fashioned now, give you a
> lot of image loading and display power.
>
> Do you men 16 bits per pixel, or 16 bits per color component?

Hi! Thank you for your answer.
I´m working with Carbon and it´s 16bits per pixel. I would prefer do
it directly but if it´s faster using a control I don´t care.

Thanks.

David Phillip Oster

unread,
Mar 6, 2007, 10:27:24 AM3/6/07
to
In article <1173177954....@n33g2000cwc.googlegroups.com>,
"Antrox" <antonio.g...@gmail.com> wrote:

> On 6 mar, 06:03, David Phillip Oster <o...@ieee.org> wrote:
> > In article <1173136337.770463.150...@c51g2000cwc.googlegroups.com>,
> >
> > "Antrox" <antonio.gil.gar...@gmail.com> wrote:
> > > I have the position and the 16bits pixel value of an image and I would

> > > like to draw into a window. I扉e read about Quartz but I don愒


> > > understand how use createimage from the pixels and so draw the image.

> > > Can you show me a way to do this. I扉e read that printing pixel by
> > > pixel is so slow.
> >
>

> Hi! Thank you for your answer.

> I惴 working with Carbon and it愀 16bits per pixel. I would prefer do
> it directly but if it愀 faster using a control I don愒 care.

If you are starting from an image file, you can create a Quicktime
GraphicImageImporter initialized with the file, draw it as many times
as you need, then dispose of the GraphicImageImporter:

Rect boundsRect;
FSRef fsRef;
FSSpec fs;
Boolean isDir;

GraphicsImportComponent gi = 0;
FSPathMakeRef((UInt*) "/Users/Me/Desktop/MyPict.jpg", &fsRef, &isDir);
FSGetCatalogInfo(&fsRef, 0, NULL, NULL, &fs, NULL);
GetGraphicsImporterForFile(&fs, &gi);
GraphicsImportGetNaturalBounds(gi, & boundsRect);
GraphicsImportSetBoundsRect(gi, &boundsRect);

GraphicsImportDraw(gi);


CloseComponent(gi);

If you have a 2-D array of pixels, you'll need to wrap it in some data
structure the system understands, use the data structure to draw it,
then dispose of the data structure.

See:
/Developer/ADC Reference Library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_data_mgr/chapter_11_section_1.html

Antrox

unread,
Mar 6, 2007, 2:15:27 PM3/6/07
to
On Mar 6, 4:27 pm, David Phillip Oster <o...@ieee.org> wrote:
> In article <1173177954.271606.67...@n33g2000cwc.googlegroups.com>,

>
> "Antrox" <antonio.gil.gar...@gmail.com> wrote:
> > On 6 mar, 06:03, David Phillip Oster <o...@ieee.org> wrote:
> > > In article <1173136337.770463.150...@c51g2000cwc.googlegroups.com>,
>
> > > "Antrox" <antonio.gil.gar...@gmail.com> wrote:
> > > > I have the position and the 16bits pixel value of an image and I would
> > > > like to draw into a window. I´ve read about Quartz but I don´t

> > > > understand how use createimage from the pixels and so draw the image.
> > > > Can you show me a way to do this. I´ve read that printing pixel by

> > > > pixel is so slow.
>
> > Hi! Thank you for your answer.
> > I´m working with Carbon and it´s 16bits per pixel. I would prefer do
> > it directly but if it´s faster using a control I don´t care.

>
> If you are starting from an image file, you can create a Quicktime
> GraphicImageImporter initialized with the file, draw it as many times
> as you need, then dispose of the GraphicImageImporter:
>
> Rect boundsRect;
> FSRef fsRef;
> FSSpec fs;
> Boolean isDir;
>
> GraphicsImportComponent gi = 0;
> FSPathMakeRef((UInt*) "/Users/Me/Desktop/MyPict.jpg", &fsRef, &isDir);
> FSGetCatalogInfo(&fsRef, 0, NULL, NULL, &fs, NULL);
> GetGraphicsImporterForFile(&fs, &gi);
> GraphicsImportGetNaturalBounds(gi, & boundsRect);
> GraphicsImportSetBoundsRect(gi, &boundsRect);
>
> GraphicsImportDraw(gi);
>
> CloseComponent(gi);
>
> If you have a 2-D array of pixels, you'll need to wrap it in some data
> structure the system understands, use the data structure to draw it,
> then dispose of the data structure.
>
> See:
> /Developer/ADC Reference Library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_data_mgr/chapter_11_section_1.html

Thank you again for your answer but where I have really problems is
passing the array to a PICT wich I could work then. I don´t understand
the way of obtain a CGImage from the array.

Thanks.

David Phillip Oster

unread,
Mar 6, 2007, 10:07:41 PM3/6/07
to
In article <1173208512.5...@30g2000cwc.googlegroups.com>,
"Antrox" <antonio.g...@gmail.com> wrote:

> Thank you again for your answer but where I have really problems is
> passing the array to a PICT wich I could work then. I don´t understand
> the way of obtain a CGImage from the array.

Take a look at:
http://www.cocoabuilder.com/archive/message/cocoa/2003/12/3/854

David Phillip Oster

unread,
Mar 6, 2007, 11:56:50 PM3/6/07
to

> Thank you again for your answer but where I have really problems is
> passing the array to a PICT wich I could work then. I don´t understand
> the way of obtain a CGImage from the array.

Here's a working model:

#import <Cocoa/Cocoa.h>


@interface MyView : NSView {
int width_;
int height_;
unsigned short *imagearray_;
CGImageRef myCGImage_;
CGDataProviderRef dataProvider_;
char *p_;
}
- (char *)p;
- (char *)pBase;
- (char *)pMax;
- (void)setP:(char *)p;
@end
==================================
//
// MyView.m
// CGImage
//
// Created by David Phillip Oster on 3/6/07.
//

#import "MyView.h"
#import <memory.h>
#import <string.h>

static size_t DataProviderGetBytes(void *info, void *buffer, size_t
count){
MyView *me = (MyView *)info;
if ([me pMax] < [me p]) {
count = 0;
}else if ([me pMax] < [me p]+count){
count = [me pMax] - [me p];
}
memmove(buffer, [me p], count);
[me setP:count + [me p]];
return count;
}

static void DataProviderSkipBytes(void *info, size_t count){
MyView *me = (MyView *)info;
[me setP:count + [me p]];
}

static void DataProviderRewind(void *info){
MyView *me = (MyView *)info;
[me setP:[me pBase]];
}

static void DataProviderReleaseInfo(void *info){
}

@implementation MyView

- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}

- (void)awakeFromNib {
width_ = [self frame].size.width;
width_ = ((width_ + (16-1)) / 16) * 16;
height_ = [self frame].size.height;

// initialize our 2 d array of pixels.
imagearray_ = (unsigned short *) malloc(width_*height_
*sizeof(unsigned short));
p_ = (char *) imagearray_;
int i, j, k;
for(j = k = 0; j < height_; ++j) {
unsigned short *p = &imagearray_[j*width_];
for(i = 0; i < width_; ++i, ++k, ++p) {
*p = ((int)(i * (31./width_)) & 0x1F) << 1 |
((int)(i * (31./width_)) & 0x1F) << 6 |
((int)(i * (31./width_)) & 0x1F) << 11;
}
}
CGDataProviderCallbacks callbacks = {
DataProviderGetBytes,
DataProviderSkipBytes,
DataProviderRewind,
DataProviderReleaseInfo
};
dataProvider_ = CGDataProviderCreate(self, &callbacks);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
myCGImage_ = CGImageCreate(width_, height_, 5, 16, width_*2,
colorspace,
kCGBitmapByteOrder32Host, dataProvider_, nil, NO,
kCGRenderingIntentDefault);
CGColorSpaceRelease(colorspace);
}

- (void)dealloc {
if (imagearray_) {
free(imagearray_);
imagearray_ = nil;
}
if (myCGImage_) {
CGImageRelease(myCGImage_);
myCGImage_ = nil;
}
if (dataProvider_) {
CGDataProviderRelease(dataProvider_);
dataProvider_ = nil;
}
[super dealloc];
}

- (void)drawRect:(NSRect)rect {
CGRect cgRect = CGRectMake(rect.origin.x, rect.origin.y,
rect.size.width, rect.size.height);
NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
CGContextDrawImage((CGContextRef) [currentContext graphicsPort],
cgRect, myCGImage_);
}

- (char *)p {
return p_;
}

- (char *)pBase {
return (char *) imagearray_;
}

- (char *)pMax {
return [self pBase] + (width_*height_ *sizeof(unsigned));
}


- (void)setP:(char *)p {
p_ = p;
}


@end

Antrox

unread,
Mar 7, 2007, 12:17:31 PM3/7/07
to
On 7 mar, 05:56, David Phillip Oster <o...@ieee.org> wrote:
> In article <1173208512.508907.172...@30g2000cwc.googlegroups.com>,

Thanks a lot, I´m going to tranform it into Carbon and I´ll try to
create the CGImage, the rest I think it´s less complicated than that.

Antrox

unread,
Mar 26, 2007, 4:46:35 AM3/26/07
to

Hi!
Don´t you know a Carbon Code? I don´t understand well Cocoa and
Objective-C.

Thanks.

David Phillip Oster

unread,
Mar 27, 2007, 11:34:52 PM3/27/07
to
In article <1174898794.9...@p77g2000hsh.googlegroups.com>,
"Antrox" <antonio.g...@gmail.com> wrote:

> Don´t you know a Carbon Code? I don´t understand well Cocoa and
> Objective-C.

Here's the Objective-C, Cocoa solution for drawing an array of pixels
as an image.:

- (void)drawRect:(NSRect)rect {
CGRect cgRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
CGContextDrawImage((CGContextRef) [currentContext graphicsPort], cgRect, myCGImage_);
}


Here's a straight C solution, Carbon solution for drawing an array of
pixels as an image.:

static void DrawUserPane(ControlRef pane, SInt16 part){
Rect rect;
CGContextRef cg = NULL;
GetControlBounds(pane, &rect);
WindowRef window = GetControlOwner(pane);
CGrafPtr winPort = GetWindowPort(window);
QDBeginCGContext(winPort, &cg);
CGRect cgRect = CGRectMake(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);

CGContextDrawImage(cg, cgRect, sCGImage);
QDEndCGContext(winPort, &cg);
}

the rest of the sample, essentially all initialization,
is available for you to download at:

http://www.TurboZen.com/sourceCode/CGImageCarbon.zip

0 new messages