Reputation: 188
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
Upvotes: 0
Views: 1993
Reputation: 41005
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();
Upvotes: 2