Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / March 2007



Tip: Looking for answers? Try searching our database.

Draw a image into a window in Carbon

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Antrox - 05 Mar 2007 23:12 GMT
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 - 06 Mar 2007 05:03 GMT
> 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?
Antrox - 06 Mar 2007 10:45 GMT
> In article <1173136337.770463.150...@c51g2000cwc.googlegroups.com>,
>
[quoted text clipped - 13 lines]
>
> 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 - 06 Mar 2007 15:27 GMT
> > In article <1173136337.770463.150...@c51g2000cwc.googlegroups.com>,
> >
[quoted text clipped - 7 lines]
> 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
Antrox - 06 Mar 2007 19:15 GMT
> In article <1173177954.271606.67...@n33g2000cwc.googlegroups.com>,
>
[quoted text clipped - 36 lines]
> 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 - 07 Mar 2007 03:07 GMT
> 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 - 07 Mar 2007 04:56 GMT
> 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 - 07 Mar 2007 17:17 GMT
> In article <1173208512.508907.172...@30g2000cwc.googlegroups.com>,
>
[quoted text clipped - 152 lines]
>
> @end

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 - 26 Mar 2007 09:46 GMT
> > In article <1173208512.508907.172...@30g2000cwc.googlegroups.com>,
>
[quoted text clipped - 155 lines]
> 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.

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

Thanks.
David Phillip Oster - 28 Mar 2007 04:34 GMT
> 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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.