Re: grid on picture
Re: grid on picture
- Subject: Re: grid on picture
- From: Jeff LaMarche <email@hidden>
- Date: Tue, 18 Mar 2003 13:58:44 -0500
On Saturday, March 15, 2003, at 05:59 AM, Carlos Coutinho wrote:
I'm very very new to cocoa
I want to open picture and put a grid on it and in each square paint
it with the most visible color of the square.
In the end i'll end up with a picture that has one color squares
(visible ones, for example one inch side)
Is this what you're looking for? It's a category on NSImage that
overlays a portion of an image with a pixel mosaic based on that
portion:
----- BEGIN NSImage-Mosaic.h----
//
------------------------------------------------------------------------
-----------
// NSImage-Mosaic.h
//
------------------------------------------------------------------------
-----------
// Created by Jeff LaMarche on Wed May 01 2002.
// Copyright (c) 2002 Naked Software. All rights reserved.
//
------------------------------------------------------------------------
-----------
// THIS SOURCE CODE IS PROVIDED AS-IS WITH NO WARRANTY OF ANY KIND
//
------------------------------------------------------------------------
-----------
// You may use and redistribute this source code without limitation
//
------------------------------------------------------------------------
-----------
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
/*!
@header NSImage-Mosaic.h
This is a category on NSImage that provides two instance methods for
creating new images based on an existing image that is an exact copy of
the existing image EXCEPT that one or more rectanglar sections are
converted to a "COPS"-style square-tile mosaic.
*/
/*!
@category NSImage(Mosaic)
@discussion This is a category on NSImage that provides two instance
methods for creatin an exact copy of the existing image EXCEPT that one
or more rectanglar sections are converted to a "COPS"-style square-tile
mosaic. This version works with images that have any number of
channels, but applies the mosaic to any channel found, including alpha
channels.
*/
@interface NSImage(Mosaic)
/*!
@method mosaicRect:withTileSize:
@abstract Creates new image based on this image, except with the area
in a defined NSRect blurred with a square-tile mosaic. <B>NOTE:</B>
Mosaic is applied to all channels, including alpha channels.
@param rect An NSRect defining the area to be distorted.
@param tileSize The size (in pixels) of one side of a mosaic tile
@result Autoreleased NSImage that is a copy of this image except with
defined rect blurred out using a mosaic. If the section could not be
processed for any reason (e.g. an invalid tileSize), then this method
simply returns a reference to self, so you should compare the returned
value against the original image to see to see if they are the same if
you plan on making any modifications to the converted image. If you do
not, you could end up accidentally making unintended changes to the
original image.
*/
-(NSImage *)mosaicRect:(NSRect)rect withSize:(int)tileSize;
/*!
@method mosaicRects:andTileSize
@abstract Creates image based on this image, except with the area in
multiple rectangles turned into a square-tile mosaic. <B>NOTE:</B>
Mosaic is applied to all channels, including alpha channels.
@param rects An NSArray with all of the NSRects to be converted, stored
using NSValue objects
@param tileSize The size (in pixels) of one side of a mosaic tile
@result Autoreleased NSImage that is a copy of this image except with
defined rects blurred out using a mosaic. If none of the sections could
be processed for some reason (e.g. an invalid tileSize), then this
method simply returns a reference to self, so you should compare the
returned value against the original image to see if they are the same
if you plan on making any modifications to the converted image. If you
do not, you could end up accidentally making unintended changes to the
original image.
*/
-(NSImage *)mosaicRects:(NSArray *)rects andTileSize:(int)tileSize;
@end
---END NSImage-Mosaic.h----
--- BEGIN NSImage-Mosaic.m ----
//
------------------------------------------------------------------------
-----------
// NSImage-Mosaic.m
//
------------------------------------------------------------------------
-----------
// Created by Jeff LaMarche on Wed May 01 2002.
// Copyright (c) 2002 Naked Software. All rights reserved.
//
------------------------------------------------------------------------
-----------
// THIS SOURCE CODE IS PROVIDED AS-IS WITH NO WARRANTY OF ANY KIND
//
------------------------------------------------------------------------
-----------
// You may use and redistribute this source code without limitation
//
------------------------------------------------------------------------
-----------
#import "NSImage-Mosaic.h"
@implementation NSImage(Mosaic)
//
------------------------------------------------------------------------
-----------
-(NSImage *)mosaicRect:(NSRect)rect withSize:(int)tileSize
//
------------------------------------------------------------------------
-----------
{
NSBitmapImageRep *mosaicImage;
NSImage *image;
NSData *data;
unsigned char * imageBuffer;
int currRow, currCol, tileRow, tileCol, i;
int pixelCount = 0;
int rowBytes = 0;
int bytesPerPixel = 0;
int curPos = 0;
NSMutableData * channelAvgs;
int * chanAvg;
if (tileSize < 2)
{
NSLog(@"Tiles can't be smaller than 2x2 pixels");
return self;
}
data = [[NSData alloc] initWith
Data:[self TIFFRepresentation]];
mosaicImage = [[NSBitmapImageRep alloc] initWith
Data:data];
imageBuffer = [mosaicImage bitmapData];
image = [[NSImage alloc] initWithSize:[self size]];
rowBytes=[mosaicImage bytesPerRow];
bytesPerPixel = rowBytes / [self size].width;
channelAvgs = [NSMutableData dataWithLength:bytesPerPixel *
sizeof(int)];
chanAvg = [channelAvgs mutableBytes];
for (currRow = rect.origin.y; currRow < (rect.size.height +
rect.origin.y); currRow += tileSize)
{
for (currCol = rect.origin.x; currCol < (rect.size.width +
rect.origin.x); currCol += tileSize)
{
pixelCount = 0;
for (tileRow = 0; tileRow < tileSize; tileRow++)
{
for (tileCol = 0; tileCol < tileSize; tileCol++)
{
curPos = ((currRow + tileRow) * rowBytes) + ((currCol + tileCol)
* bytesPerPixel);
pixelCount++;
for (i = 0; i < bytesPerPixel; i++)
chanAvg[i] += imageBuffer[curPos + i];
}
}
for (i = 0; i < bytesPerPixel; i++)
chanAvg[i] /= pixelCount;
for (tileRow = 0; tileRow < tileSize; tileRow++)
{
// Stay within defined rect
if ((currRow + tileRow) >= (rect.origin.y + rect.size.height) )
break;
for (tileCol = 0; tileCol < tileSize; tileCol++)
{
curPos = ((currRow + tileRow) * rowBytes) + ((currCol + tileCol)
* bytesPerPixel);
if ((currCol + tileCol) >= (rect.origin.x + rect.size.width) )
break;
for (i = 0; i < bytesPerPixel; i++)
imageBuffer[curPos + i] = chanAvg[i];
}
}
// Reset channel averages to 0
for (i=0; i < bytesPerPixel; i++)
chanAvg[i] = 0;
}
}
[image addRepresentation:mosaicImage];
return image;
}
//
------------------------------------------------------------------------
-----------
-(NSImage *)mosaicRects:(NSArray *)rects andTileSize:(int)tileSize
//
------------------------------------------------------------------------
-----------
{
int i;
NSImage *image = [[self copy] autorelease];
for (i = 0; i < [rects count]; i++)
image = [image mosaic:[[rects objectAtIndex:i] rectValue]
withSize:tileSize];
return image;
}
@end
--- End NSImage-Mosaic.m ----
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.