FROM : Marco Binder
DATE : Tue Nov 19 15:16:23 2002
Since Ben is still struggling with his PDF to grayscale TIFF
conversion, and he is definitly not the only one with trouble like
that, I thought, for the sake of completeness of the archives, I d set
up some quick and dirty sample project, showing how to:
- create an Image from a PDF file
- get the TIFFData from that image and use it for a new image
- create and use the NSBitmapImageRep of the TIFFData and
- use the bitmap data directly to convert it into an 8-bit, 1 sample,
no alpha grayscale NSBitmapImageRep
- save the resulting grayscale TIFF to disk
Please note that I havent really spent more than an hour on this
project and therefore programming style may have suffered a bit. I
havent optimized it in any way for performance and it is plainly for
illustrating purposes!
You can download it (as a simple .sit) at:
http://www.marco-binder.de/dev/PDF2Grayscale.sit
marco
Am Montag, 18.11.02 um 22:52 Uhr schrieb Ben Mackin:
> On 11/15/02 5:04 AM, "Marco Binder" <<email_removed>> wrote:
>
>> Your problem with "black what should be white" may boil down to the
>> fact, taht what is white is not actually white but R,G,B = 0,0,0 just
>> at alpha 0 which makes it transparent and you got a white background
>> "below" your image. If you now save it without alpha, it s just 0,0,0
>> =
>> black. You ll then have to catch this case (R=0, G=0, B=0, alpha=0)
>> and
>> set the pixel value in your new imageRep to 255 = white.
>
> Would it just work to make every pixel in my imageRep to white first,
> and
> then use the code to convert the image to greyscale. This way the
> background
> would be set to white, and I would have to deal with a check of R=0,
> G=0,
> B=0, and alpha=0?
>
> So if that is the case, and it would work, I was thinking of (looking
> at
> your code) using the following. Unfortunately this doesn't work the
> image
> still is black in all areas that should be white. Maybe my thought
> wasn't
> correct, and I will need to do the RGBA check. Let me know if you spot
> a
> mistake in that code that would not be drawing the white image.
>
> NSBitmapImageRep *bensRep;
> unsigned char *data = [imgRep bitmapData];
> unsigned char *dataPtr;
> int spp = [imgRep samplesPerPixel];
> int bps = ([imgRep bitsPerPixel]/8)/spp;
> int len = [imgRep pixelsHigh]*[imgRep pixelsWide]*spp*bps;
>
> bensRep = [[NSBitmapImageRep alloc]
> initWithBitmapDataPlanes:NULL
> pixelsWide:[imgRep pixelsWide]
> pixelsHigh:[imgRep pixelsHigh]
> bitsPerSample:8
> samplesPerPixel:1
> hasAlpha:NO
> isPlanar:NO
> colorSpaceName:NSCalibratedWhiteColorSpace
> bytesPerRow:0
> bitsPerPixel:0];
>
> [bensRep setSize:[imgRep size]];
> dataPtr = [bensRep bitmapData];
>
> if (bps==1)
> {
> int o = 0;
> int i = 0;
> for (i=0; i<len; i+=spp)
> {
> *(dataPtr+o) = 255;
> o+=1;
> }
>
> o = 0;
> i = 0;
> for (i=0; i<len; i+=spp)
> {
> int R = (int)*(data+i);
> int G = (int)*(data+i+1);
> int B = (int)*(data+i+2);
> int grayValue = (int)((R+G+B)/3);
> *(dataPtr+o) = grayValue;
> o+=1;
> }
> }
>
> Thanks,
> Ben
>
> http://www.shayufilms.com
>
>
--
|\ /| E-Mail: <email_removed> WWW: www.marco-binder.de
| \/ | Telefon: 07531 / 94 19 94 Fax: 07531 / 94 19 92
| |ARCO Snail-Mail: Banater Str. 3 - 78467 Konstanz
BINDER _____________________________________________________
--
|\ /| E-Mail: <email_removed> WWW: www.marco-binder.de
| \/ | Telefon: 07531 / 94 19 94 Fax: 07531 / 94 19 92
| |ARCO Snail-Mail: Banater Str. 3 - 78467 Konstanz
BINDER _____________________________________________________
_______________________________________________
cocoa-dev mailing list | <email_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
DATE : Tue Nov 19 15:16:23 2002
Since Ben is still struggling with his PDF to grayscale TIFF
conversion, and he is definitly not the only one with trouble like
that, I thought, for the sake of completeness of the archives, I d set
up some quick and dirty sample project, showing how to:
- create an Image from a PDF file
- get the TIFFData from that image and use it for a new image
- create and use the NSBitmapImageRep of the TIFFData and
- use the bitmap data directly to convert it into an 8-bit, 1 sample,
no alpha grayscale NSBitmapImageRep
- save the resulting grayscale TIFF to disk
Please note that I havent really spent more than an hour on this
project and therefore programming style may have suffered a bit. I
havent optimized it in any way for performance and it is plainly for
illustrating purposes!
You can download it (as a simple .sit) at:
http://www.marco-binder.de/dev/PDF2Grayscale.sit
marco
Am Montag, 18.11.02 um 22:52 Uhr schrieb Ben Mackin:
> On 11/15/02 5:04 AM, "Marco Binder" <<email_removed>> wrote:
>
>> Your problem with "black what should be white" may boil down to the
>> fact, taht what is white is not actually white but R,G,B = 0,0,0 just
>> at alpha 0 which makes it transparent and you got a white background
>> "below" your image. If you now save it without alpha, it s just 0,0,0
>> =
>> black. You ll then have to catch this case (R=0, G=0, B=0, alpha=0)
>> and
>> set the pixel value in your new imageRep to 255 = white.
>
> Would it just work to make every pixel in my imageRep to white first,
> and
> then use the code to convert the image to greyscale. This way the
> background
> would be set to white, and I would have to deal with a check of R=0,
> G=0,
> B=0, and alpha=0?
>
> So if that is the case, and it would work, I was thinking of (looking
> at
> your code) using the following. Unfortunately this doesn't work the
> image
> still is black in all areas that should be white. Maybe my thought
> wasn't
> correct, and I will need to do the RGBA check. Let me know if you spot
> a
> mistake in that code that would not be drawing the white image.
>
> NSBitmapImageRep *bensRep;
> unsigned char *data = [imgRep bitmapData];
> unsigned char *dataPtr;
> int spp = [imgRep samplesPerPixel];
> int bps = ([imgRep bitsPerPixel]/8)/spp;
> int len = [imgRep pixelsHigh]*[imgRep pixelsWide]*spp*bps;
>
> bensRep = [[NSBitmapImageRep alloc]
> initWithBitmapDataPlanes:NULL
> pixelsWide:[imgRep pixelsWide]
> pixelsHigh:[imgRep pixelsHigh]
> bitsPerSample:8
> samplesPerPixel:1
> hasAlpha:NO
> isPlanar:NO
> colorSpaceName:NSCalibratedWhiteColorSpace
> bytesPerRow:0
> bitsPerPixel:0];
>
> [bensRep setSize:[imgRep size]];
> dataPtr = [bensRep bitmapData];
>
> if (bps==1)
> {
> int o = 0;
> int i = 0;
> for (i=0; i<len; i+=spp)
> {
> *(dataPtr+o) = 255;
> o+=1;
> }
>
> o = 0;
> i = 0;
> for (i=0; i<len; i+=spp)
> {
> int R = (int)*(data+i);
> int G = (int)*(data+i+1);
> int B = (int)*(data+i+2);
> int grayValue = (int)((R+G+B)/3);
> *(dataPtr+o) = grayValue;
> o+=1;
> }
> }
>
> Thanks,
> Ben
>
> http://www.shayufilms.com
>
>
--
|\ /| E-Mail: <email_removed> WWW: www.marco-binder.de
| \/ | Telefon: 07531 / 94 19 94 Fax: 07531 / 94 19 92
| |ARCO Snail-Mail: Banater Str. 3 - 78467 Konstanz
BINDER _____________________________________________________
--
|\ /| E-Mail: <email_removed> WWW: www.marco-binder.de
| \/ | Telefon: 07531 / 94 19 94 Fax: 07531 / 94 19 92
| |ARCO Snail-Mail: Banater Str. 3 - 78467 Konstanz
BINDER _____________________________________________________
_______________________________________________
cocoa-dev mailing list | <email_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
| Related mails | Author | Date |
|---|---|---|
| No related mails found. | ||






Cocoa mail archive

