0

This is my first assignment about image processing. I'm assuming index of each pixel on output image are represented as matrix below:

00 01 02 03 04 05

10 11 12 13 14 15

20 21 22 23 24 25

At each index of output image I have different color to draw on. For ex, at index 00 I have redcolor available to put there and so on with other indexes. My question is how can I draw these color into the indexes to create the output image?

Update

This is what I have right now:

 inputImgAvg //Image for processing
 CGContextRef context = UIGraphicsGetCurrentContext();

 float  yy = groutW / 2;            // skip over grout on edge
     float stride =(int) (tileW + groutW +0.5);
        for(int y=0; y<tilesY; y++) {               //Number tile in Y direction
            float xx = groutW / 2 ;         // skip over grout on edge
            for(int x=0; x<tilesX; x++) {
                tileRGB = [inputImgAvg colorAtPixel:CGPointMake(x,y)];

                //Right here I'm checking tileRGB with list of available color
                //Find out the closest color 
                //Now i'm just checking with greenColor

                // best matching tile is found in idx position in vector;
                // scale and copy it into proper location in the output
                CGContextSetFillColor(context, CGColorGetComponents( [[UIColor greenColor] CGColor]));

But I got this error. Can you point out what did I do wrong?

<Error>: CGContextSetFillColor: invalid context 0x0
<Error>: CGContextFillRects: invalid context 0x0
e-sushi
  • 13,786
  • 10
  • 38
  • 57
user1139699
  • 188
  • 1
  • 9
  • You'll get the best responses from Stack Overflow if you actually attempt your assignment and then ask questions about specific problems you're having. – theTRON Jan 26 '12 at 23:52
  • I think this question might be what you are looking for: http://stackoverflow.com/questions/448125/how-to-get-pixel-data-from-a-uiimage-cocoa-touch-or-cgimage-core-graphics – Russell Zahniser Jan 26 '12 at 23:52
  • My question is how can I draw these color into the indexes to create the output image? I can handle the rest, I'm new to these. In QT, I just do like this "drawImage(input-Rect,color-to-draw)" – user1139699 Jan 26 '12 at 23:57
  • The link is about get rgb at x&y position. My question is have rgb how to put back it on output image with xy? – user1139699 Jan 27 '12 at 00:06
  • What type of image are you working with? A CGImage? An NSImage? – user1118321 Jan 27 '12 at 00:56

1 Answers1

2

This thread answers the question:

http://www.iphonedevsdk.com/forum/iphone-sdk-development/34247-cgimage-pixel-array.html

Create a CGContext using CGBitmapContextCreate, which lets you supply the data for the image. You can then write pixels into the data using a pointer and setting the bytes yourself.

Once you are done, use UIGraphicsGetImageFromCurrentContext() or equivalent to grab the context data out into a UIImage object.

If that all seems a bit low-level, another option would be to just create a CGContext and draw 1x1 rectangles into it. It won't be amazingly fast, but it won't be as slow as you think because CG functions are all pure C and any redundancy gets optimised out by the compiler:

//create drawing context
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();  

//draw pixels
for (int x = 0; x < width; x++)
{
   for (int y = 0; y < height; y++)
   {
      CGContextSetFillColor( ... your color here ... );
      CGContextFillRect(context, CGRectMake(x, y, 1.0f, 1.0f));
   }
}

//capture resultant image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103
  • Thanks for your answer. It helped me a lot. But I don't really understand what did u mean by "You can then write pixels into data by using a pointer". I used CGDataProviderCopyData to get data of source image. And I converted the data to UInt8. Now how can I pass color that I need to draw into data by using pointer? – user1139699 Feb 05 '12 at 07:59
  • Using CGDataProviderCopyData probably won't work because you need to work on the actual data of the image, not a copy of it. Each pixel consist of 4 UInt8s, red, green, blue, alpha. That is how colors are made. The image data is just those 4 bytes over and over, so try setting the bytes and see what the result it. you may have to be careful around the end of each row of colours because there are some extra spacing bytes. – Nick Lockwood Feb 05 '12 at 11:02
  • I tried the second option but I got this error : CGContextSetFillColor: invalid context 0x0 Please see updated code above and help me. Thanks – user1139699 Feb 06 '12 at 03:56
  • Did you forget the UIGraphicsBeginImageContextWithOptions(...) part? – Nick Lockwood Feb 06 '12 at 09:37
  • Yes, I forgot that line. However, even with that line I'm getting another error like this CGContextSetFillColor: invalid context 0x193e3bc – user1139699 Feb 06 '12 at 18:50
  • what width and height are you setting for your context? Are they zero by any chance? Also, are you passing the context as the first parameter to CGContextSetFillColor? – Nick Lockwood Feb 06 '12 at 19:23
  • CGContextSetFillColor(context, CGColorGetComponents( [[UIColor greenColor] CGColor])); – user1139699 Feb 06 '12 at 22:46
  • If you're using UIColors, you can just say [[UIColor greenColor] setFill]; instead of using CGContextSetFillColor. I don't think that's why your context is invalid though. What width and height are you using? – Nick Lockwood Feb 06 '12 at 22:53
  • I think I only do UIGraphicsGetCurrentContext(); is this the problem? The width and the height of the current processing image. – user1139699 Feb 06 '12 at 23:01