libs-gui/Source/NSBitmapImageRep+PNG.m

390 lines
9.8 KiB
Mathematica
Raw Normal View History

/*
NSBitmapImageRep+PNG.m
Methods for loading .png images.
Copyright (C) 2003 Free Software Foundation, Inc.
Written by: Alexander Malmberg <alexander@malmberg.org>
Date: 2003-12-07
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; see the file COPYING.LIB.
If not, see <http://www.gnu.org/licenses/> or write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "NSBitmapImageRep+PNG.h"
#if HAVE_LIBPNG
#ifdef HAVE_LIBPNG_PNG_H
#include <libpng/png.h>
#else
#include <png.h>
#endif
#include <Foundation/NSData.h>
#include <Foundation/NSException.h>
Change Log Mon. 20-Nov-2006 Mark Tracy <tracy454 at concentric dot net> Many changes related to bitmap images 1. NSBitmapImateRep attribute global strings were not defined Fix: add definitions to externs.h, and declarations to NSBitmapImageRep.h Comment: Two strings defined in Cocoa were commented out: NSImageColorSyncData is proprietary to Apple, and NSImageEXIFData has no support elsewhere in GNUstep. I propose adding GSImageICCProfileData if and when color management is added to GNUstep. 2. LZW compression in TIFF was disabled for lack of a test of its availability Fix: Implement NSTiffIsCodecConfigured(codec) in tiff.m Comment: As of libtiff-3.7.0, there is a function call in the API to test availability at runtime. For libtiff-3.6.0 (earlier?) there are macros #defined in tiffconf.h. The implementation check the library version at compile time, and uses one of the two methods. I have not tested the second method for lack of an installation of an old libtiff. 3. -canCompressUsing: relied on a static list of capabilities Fix: Use the new NSTiffIsCodecConfigured(codec) in NSBitmapImageRep.m Comment: The static list could be wrong, as it was on my system. Also eliminate the supports_lzw_compression flag. 4. +getTIFFCompressionTypes:count: relied on a static list of compressors. Fix: Use the new NSTiffIsCodecConfigured(codec) in NSBitmapImageRep.m Comment: Compares GNUstep supported compressors against actual availability. Also change the private instance methods _localFromCompressionType and _compressionTypeFromLocal to private class methods so that they can be used in -initWithTIFFImage:number: and -TIFFRepresentationUsingCompression:factor: and +getTIFFCompressionTypes:count: This is probably a clumsy implementation but it works. 5. -setProperty:toValue: and -valueForProperty: were not implemented Fix: Add a new instance variable NSMutableDictionary * _properties to NSBitmapImageRep.h and implemented accessors in NSBitmapImageRep.m. Patch -_initFromTIFFImage to set compression type and factor in _properties. Comment: This feature is used to pass options to and from JPEG, PNG, TIFF, and GIF in Cocoa, although the docs are kind of vague. In one case the Cocoa docs said the properties were set when reading a TIFF, but the implementation didn't; I chose to implement the docs. Cocoa does use properties when exporting bitmaps, so I implemented that. 6. Checked and updated NSBitmapImageFileType in NSBitmapImageRep.h Fix: confirmed the enumeration values against Cocoa, and added NSJPEG2000FileType = 5 Comment: JPEG-2000 is not implemented, just reserved a space for it. 7. -representationUsingType:properties: was not implemented Fix: Implement export of TIFF, JPEG, GIF and PNG in NSBitmapImage.m Comment: See the change notes for JPEG, GIF, and PNG for more. BMP and JPEG-2000 are not implemented; they just log a message to that effect. As apparently Cocoa does it this way, if you pass nil for properties, it falls back to the internal _properties, and if that is empty, there are some safe defaults. 8. +representationfOfImageRepsInArray:UsingType:properties: was not implemented Fix: Partially implement in NSBitmapImageRep.m Comment: I just stole the incomplete code from +TIFFRepresentationOfImageRepsInArray: since I have yet to find an explanation of how this really ought to work. 9. JPEG export didn't handle alpha channel, properties or errors. Fix: Add -_JPEGRepresentationWithProperties:errorMessage: to NSBitmapImageRep+JPEG.h and greatly rework Nicolas Roard's code in NSBitmapImageRep+JPEG.m. Patch -_initBitmapFromJPEG:errorMessage to write properties. Comment: Major rewrite of Nicolas Roard's JPEG export code. To do: Support for planar bitmaps and support for colorspaces other than RGB(A). 10. PNG export not implemented Fix: Add -_PNGRepresentationWithProperties: to NSBitmapImageRep+PNG.h and implement NSBitmapImageRep+PNG.m Comment: No support yet for planar bitmaps. Only supports NS*WhiteColorSpace and NS*RGBColorSpace. Does support alpha. Support for reading and writing NSImageGamma is experimental. In keeping with Cocoa, the property NSImageGamma ranges from 0.0 to 1.0; representing the range of minimum supported gamma to maximum supported gamma, in this case 1.0 to 2.5. This is in contrast to GNUstep where by convention the property would range from 0.0 to 255.0. To do: proper error message support 11. GIF export not implemented Fix: Add -_GIFRepresentationWithPropterties:errorMessage: to NSBitmapImageRep+GIF.h and implement in NSBitmapImageRep+GIF.m Comments: Supports only RGB(A) colorspaces, but ignores alpha. Supports planar or interleaved bitmaps. Supports properties NSImageRGBColorTable. 12. -_initBitmapFromGIF:errorMessage: did not support transparency Fix: Don't ignore control blocks in NSBitmapImageRep+GIF.m; check for transparency. Comment: If a transparent color is found, it adds an alpha channel to the bitmap. Also, save the color table in properties. 13. -_initBitmapFromGIF:errorMessage: would show the last image in a multi-image GIF file Fix: Break the parsing loop after the first image in NSBitmapImageRep+GIF.m Comment: Also check for frame duration, and set that property. There is not yet any support for animated GIF. This will require some additional infrastructure, and I won't do it unless asked. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@24140 72102866-910b-0410-8b05-ffd578937521
2006-11-21 06:36:26 +00:00
#include <Foundation/NSValue.h>
#include "AppKit/NSGraphics.h"
Change Log Mon. 20-Nov-2006 Mark Tracy <tracy454 at concentric dot net> Many changes related to bitmap images 1. NSBitmapImateRep attribute global strings were not defined Fix: add definitions to externs.h, and declarations to NSBitmapImageRep.h Comment: Two strings defined in Cocoa were commented out: NSImageColorSyncData is proprietary to Apple, and NSImageEXIFData has no support elsewhere in GNUstep. I propose adding GSImageICCProfileData if and when color management is added to GNUstep. 2. LZW compression in TIFF was disabled for lack of a test of its availability Fix: Implement NSTiffIsCodecConfigured(codec) in tiff.m Comment: As of libtiff-3.7.0, there is a function call in the API to test availability at runtime. For libtiff-3.6.0 (earlier?) there are macros #defined in tiffconf.h. The implementation check the library version at compile time, and uses one of the two methods. I have not tested the second method for lack of an installation of an old libtiff. 3. -canCompressUsing: relied on a static list of capabilities Fix: Use the new NSTiffIsCodecConfigured(codec) in NSBitmapImageRep.m Comment: The static list could be wrong, as it was on my system. Also eliminate the supports_lzw_compression flag. 4. +getTIFFCompressionTypes:count: relied on a static list of compressors. Fix: Use the new NSTiffIsCodecConfigured(codec) in NSBitmapImageRep.m Comment: Compares GNUstep supported compressors against actual availability. Also change the private instance methods _localFromCompressionType and _compressionTypeFromLocal to private class methods so that they can be used in -initWithTIFFImage:number: and -TIFFRepresentationUsingCompression:factor: and +getTIFFCompressionTypes:count: This is probably a clumsy implementation but it works. 5. -setProperty:toValue: and -valueForProperty: were not implemented Fix: Add a new instance variable NSMutableDictionary * _properties to NSBitmapImageRep.h and implemented accessors in NSBitmapImageRep.m. Patch -_initFromTIFFImage to set compression type and factor in _properties. Comment: This feature is used to pass options to and from JPEG, PNG, TIFF, and GIF in Cocoa, although the docs are kind of vague. In one case the Cocoa docs said the properties were set when reading a TIFF, but the implementation didn't; I chose to implement the docs. Cocoa does use properties when exporting bitmaps, so I implemented that. 6. Checked and updated NSBitmapImageFileType in NSBitmapImageRep.h Fix: confirmed the enumeration values against Cocoa, and added NSJPEG2000FileType = 5 Comment: JPEG-2000 is not implemented, just reserved a space for it. 7. -representationUsingType:properties: was not implemented Fix: Implement export of TIFF, JPEG, GIF and PNG in NSBitmapImage.m Comment: See the change notes for JPEG, GIF, and PNG for more. BMP and JPEG-2000 are not implemented; they just log a message to that effect. As apparently Cocoa does it this way, if you pass nil for properties, it falls back to the internal _properties, and if that is empty, there are some safe defaults. 8. +representationfOfImageRepsInArray:UsingType:properties: was not implemented Fix: Partially implement in NSBitmapImageRep.m Comment: I just stole the incomplete code from +TIFFRepresentationOfImageRepsInArray: since I have yet to find an explanation of how this really ought to work. 9. JPEG export didn't handle alpha channel, properties or errors. Fix: Add -_JPEGRepresentationWithProperties:errorMessage: to NSBitmapImageRep+JPEG.h and greatly rework Nicolas Roard's code in NSBitmapImageRep+JPEG.m. Patch -_initBitmapFromJPEG:errorMessage to write properties. Comment: Major rewrite of Nicolas Roard's JPEG export code. To do: Support for planar bitmaps and support for colorspaces other than RGB(A). 10. PNG export not implemented Fix: Add -_PNGRepresentationWithProperties: to NSBitmapImageRep+PNG.h and implement NSBitmapImageRep+PNG.m Comment: No support yet for planar bitmaps. Only supports NS*WhiteColorSpace and NS*RGBColorSpace. Does support alpha. Support for reading and writing NSImageGamma is experimental. In keeping with Cocoa, the property NSImageGamma ranges from 0.0 to 1.0; representing the range of minimum supported gamma to maximum supported gamma, in this case 1.0 to 2.5. This is in contrast to GNUstep where by convention the property would range from 0.0 to 255.0. To do: proper error message support 11. GIF export not implemented Fix: Add -_GIFRepresentationWithPropterties:errorMessage: to NSBitmapImageRep+GIF.h and implement in NSBitmapImageRep+GIF.m Comments: Supports only RGB(A) colorspaces, but ignores alpha. Supports planar or interleaved bitmaps. Supports properties NSImageRGBColorTable. 12. -_initBitmapFromGIF:errorMessage: did not support transparency Fix: Don't ignore control blocks in NSBitmapImageRep+GIF.m; check for transparency. Comment: If a transparent color is found, it adds an alpha channel to the bitmap. Also, save the color table in properties. 13. -_initBitmapFromGIF:errorMessage: would show the last image in a multi-image GIF file Fix: Break the parsing loop after the first image in NSBitmapImageRep+GIF.m Comment: Also check for frame duration, and set that property. There is not yet any support for animated GIF. This will require some additional infrastructure, and I won't do it unless asked. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@24140 72102866-910b-0410-8b05-ffd578937521
2006-11-21 06:36:26 +00:00
#if defined(PNG_FLOATING_POINT_SUPPORT)
# define PNG_FLOATING_POINT 1
#else
# define PNG_FLOATING_POINT 0
#endif
#if defined(PNG_gAMA_SUPPORT)
# define PNG_gAMA 1
#else
# define PNG_gAMA 0
#endif
@implementation NSBitmapImageRep (PNG)
+ (BOOL) _bitmapIsPNG: (NSData *)imageData
{
if (![imageData length])
return NO;
if (!png_sig_cmp((png_bytep)[imageData bytes], 0, [imageData length]))
return YES;
return NO;
}
typedef struct
{
NSData *data;
unsigned int offset;
} reader_struct_t;
static void reader_func(png_structp png_struct, png_bytep data,
png_size_t length)
{
reader_struct_t *r = png_get_io_ptr(png_struct);
if (r->offset + length > [r->data length])
{
png_error(png_struct, "end of buffer");
return;
}
memcpy(data, [r->data bytes] + r->offset, length);
r->offset += length;
}
- (id) _initBitmapFromPNG: (NSData *)imageData
{
png_structp png_struct;
png_infop png_info, png_end_info;
int width,height;
unsigned char *buf;
int bytes_per_row;
int type,channels,depth;
BOOL alpha;
int bpp;
NSString *colorspace;
reader_struct_t reader;
if (!(self = [super init]))
return nil;
png_struct = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_struct)
{
RELEASE(self);
return nil;
}
png_info = png_create_info_struct(png_struct);
if (!png_info)
{
png_destroy_read_struct(&png_struct, NULL, NULL);
RELEASE(self);
return nil;
}
png_end_info = png_create_info_struct(png_struct);
if (!png_end_info)
{
png_destroy_read_struct(&png_struct, &png_info, NULL);
RELEASE(self);
return nil;
}
if (setjmp(png_jmpbuf(png_struct)))
{
png_destroy_read_struct(&png_struct, &png_info, &png_end_info);
RELEASE(self);
return nil;
}
reader.data = imageData;
reader.offset = 0;
png_set_read_fn(png_struct, &reader, reader_func);
png_read_info(png_struct, png_info);
width = png_get_image_width(png_struct, png_info);
height = png_get_image_height(png_struct, png_info);
bytes_per_row = png_get_rowbytes(png_struct, png_info);
type = png_get_color_type(png_struct, png_info);
channels = png_get_channels(png_struct, png_info);
depth = png_get_bit_depth(png_struct, png_info);
switch (type)
{
case PNG_COLOR_TYPE_GRAY:
colorspace = NSCalibratedWhiteColorSpace;
alpha = NO;
NSAssert(channels == 1, @"unexpected channel/color_type combination");
bpp = depth;
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
colorspace = NSCalibratedWhiteColorSpace;
alpha = YES;
NSAssert(channels == 2, @"unexpected channel/color_type combination");
bpp = depth * 2;
break;
case PNG_COLOR_TYPE_PALETTE:
png_set_palette_to_rgb(png_struct);
channels = 3;
depth = 8;
alpha = NO;
if (png_get_valid(png_struct, png_info, PNG_INFO_tRNS))
{
alpha = YES;
channels++;
png_set_tRNS_to_alpha(png_struct);
}
bpp = channels * 8;
bytes_per_row = channels * width;
colorspace = NSCalibratedRGBColorSpace;
break;
case PNG_COLOR_TYPE_RGB:
colorspace = NSCalibratedRGBColorSpace;
alpha = NO;
bpp = channels * depth; /* channels might be 4 if there's a filler */
channels = 3;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
colorspace = NSCalibratedRGBColorSpace;
alpha = YES;
NSAssert(channels == 4, @"unexpected channel/color_type combination");
bpp = 4 * depth;
break;
default:
NSLog(@"NSBitmapImageRep+PNG: unknown color type %i", type);
RELEASE(self);
return nil;
}
buf = NSZoneMalloc([self zone], bytes_per_row * height);
{
unsigned char *row_pointers[height];
int i;
for (i=0;i<height;i++)
row_pointers[i]=buf+i*bytes_per_row;
png_read_image(png_struct, row_pointers);
}
[self initWithBitmapDataPlanes: &buf
pixelsWide: width
pixelsHigh: height
bitsPerSample: depth
samplesPerPixel: channels
hasAlpha: alpha
isPlanar: NO
colorSpaceName: colorspace
bitmapFormat: NSAlphaNonpremultipliedBitmapFormat
bytesPerRow: bytes_per_row
bitsPerPixel: bpp];
_imageData = [[NSData alloc]
initWithBytesNoCopy: buf
length: bytes_per_row * height];
Change Log Mon. 20-Nov-2006 Mark Tracy <tracy454 at concentric dot net> Many changes related to bitmap images 1. NSBitmapImateRep attribute global strings were not defined Fix: add definitions to externs.h, and declarations to NSBitmapImageRep.h Comment: Two strings defined in Cocoa were commented out: NSImageColorSyncData is proprietary to Apple, and NSImageEXIFData has no support elsewhere in GNUstep. I propose adding GSImageICCProfileData if and when color management is added to GNUstep. 2. LZW compression in TIFF was disabled for lack of a test of its availability Fix: Implement NSTiffIsCodecConfigured(codec) in tiff.m Comment: As of libtiff-3.7.0, there is a function call in the API to test availability at runtime. For libtiff-3.6.0 (earlier?) there are macros #defined in tiffconf.h. The implementation check the library version at compile time, and uses one of the two methods. I have not tested the second method for lack of an installation of an old libtiff. 3. -canCompressUsing: relied on a static list of capabilities Fix: Use the new NSTiffIsCodecConfigured(codec) in NSBitmapImageRep.m Comment: The static list could be wrong, as it was on my system. Also eliminate the supports_lzw_compression flag. 4. +getTIFFCompressionTypes:count: relied on a static list of compressors. Fix: Use the new NSTiffIsCodecConfigured(codec) in NSBitmapImageRep.m Comment: Compares GNUstep supported compressors against actual availability. Also change the private instance methods _localFromCompressionType and _compressionTypeFromLocal to private class methods so that they can be used in -initWithTIFFImage:number: and -TIFFRepresentationUsingCompression:factor: and +getTIFFCompressionTypes:count: This is probably a clumsy implementation but it works. 5. -setProperty:toValue: and -valueForProperty: were not implemented Fix: Add a new instance variable NSMutableDictionary * _properties to NSBitmapImageRep.h and implemented accessors in NSBitmapImageRep.m. Patch -_initFromTIFFImage to set compression type and factor in _properties. Comment: This feature is used to pass options to and from JPEG, PNG, TIFF, and GIF in Cocoa, although the docs are kind of vague. In one case the Cocoa docs said the properties were set when reading a TIFF, but the implementation didn't; I chose to implement the docs. Cocoa does use properties when exporting bitmaps, so I implemented that. 6. Checked and updated NSBitmapImageFileType in NSBitmapImageRep.h Fix: confirmed the enumeration values against Cocoa, and added NSJPEG2000FileType = 5 Comment: JPEG-2000 is not implemented, just reserved a space for it. 7. -representationUsingType:properties: was not implemented Fix: Implement export of TIFF, JPEG, GIF and PNG in NSBitmapImage.m Comment: See the change notes for JPEG, GIF, and PNG for more. BMP and JPEG-2000 are not implemented; they just log a message to that effect. As apparently Cocoa does it this way, if you pass nil for properties, it falls back to the internal _properties, and if that is empty, there are some safe defaults. 8. +representationfOfImageRepsInArray:UsingType:properties: was not implemented Fix: Partially implement in NSBitmapImageRep.m Comment: I just stole the incomplete code from +TIFFRepresentationOfImageRepsInArray: since I have yet to find an explanation of how this really ought to work. 9. JPEG export didn't handle alpha channel, properties or errors. Fix: Add -_JPEGRepresentationWithProperties:errorMessage: to NSBitmapImageRep+JPEG.h and greatly rework Nicolas Roard's code in NSBitmapImageRep+JPEG.m. Patch -_initBitmapFromJPEG:errorMessage to write properties. Comment: Major rewrite of Nicolas Roard's JPEG export code. To do: Support for planar bitmaps and support for colorspaces other than RGB(A). 10. PNG export not implemented Fix: Add -_PNGRepresentationWithProperties: to NSBitmapImageRep+PNG.h and implement NSBitmapImageRep+PNG.m Comment: No support yet for planar bitmaps. Only supports NS*WhiteColorSpace and NS*RGBColorSpace. Does support alpha. Support for reading and writing NSImageGamma is experimental. In keeping with Cocoa, the property NSImageGamma ranges from 0.0 to 1.0; representing the range of minimum supported gamma to maximum supported gamma, in this case 1.0 to 2.5. This is in contrast to GNUstep where by convention the property would range from 0.0 to 255.0. To do: proper error message support 11. GIF export not implemented Fix: Add -_GIFRepresentationWithPropterties:errorMessage: to NSBitmapImageRep+GIF.h and implement in NSBitmapImageRep+GIF.m Comments: Supports only RGB(A) colorspaces, but ignores alpha. Supports planar or interleaved bitmaps. Supports properties NSImageRGBColorTable. 12. -_initBitmapFromGIF:errorMessage: did not support transparency Fix: Don't ignore control blocks in NSBitmapImageRep+GIF.m; check for transparency. Comment: If a transparent color is found, it adds an alpha channel to the bitmap. Also, save the color table in properties. 13. -_initBitmapFromGIF:errorMessage: would show the last image in a multi-image GIF file Fix: Break the parsing loop after the first image in NSBitmapImageRep+GIF.m Comment: Also check for frame duration, and set that property. There is not yet any support for animated GIF. This will require some additional infrastructure, and I won't do it unless asked. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@24140 72102866-910b-0410-8b05-ffd578937521
2006-11-21 06:36:26 +00:00
if (PNG_INFO_gAMA & png_info->valid)
{
double file_gamma = 2.2;
if (PNG_FLOATING_POINT)
{
png_get_gAMA(png_struct, png_info, &file_gamma);
// remap file_gamma [1.0, 2.5] to property [0.0, 1.0]
file_gamma = (file_gamma - 1.0)/1.5;
}
else // fixed point
{
png_fixed_point int_gamma = 220000;
png_get_gAMA_fixed(png_struct, png_info, &int_gamma);
// remap gamma [0.0, 1.0] to [100000, 250000]
file_gamma = ((double)int_gamma - 100000.0)/150000.0;
}
[self setProperty: NSImageGamma
withValue: [NSNumber numberWithDouble: file_gamma]];
//NSLog(@"PNG file gamma: %f", file_gamma);
}
png_destroy_read_struct(&png_struct, &png_info, &png_end_info);
return self;
}
Change Log Mon. 20-Nov-2006 Mark Tracy <tracy454 at concentric dot net> Many changes related to bitmap images 1. NSBitmapImateRep attribute global strings were not defined Fix: add definitions to externs.h, and declarations to NSBitmapImageRep.h Comment: Two strings defined in Cocoa were commented out: NSImageColorSyncData is proprietary to Apple, and NSImageEXIFData has no support elsewhere in GNUstep. I propose adding GSImageICCProfileData if and when color management is added to GNUstep. 2. LZW compression in TIFF was disabled for lack of a test of its availability Fix: Implement NSTiffIsCodecConfigured(codec) in tiff.m Comment: As of libtiff-3.7.0, there is a function call in the API to test availability at runtime. For libtiff-3.6.0 (earlier?) there are macros #defined in tiffconf.h. The implementation check the library version at compile time, and uses one of the two methods. I have not tested the second method for lack of an installation of an old libtiff. 3. -canCompressUsing: relied on a static list of capabilities Fix: Use the new NSTiffIsCodecConfigured(codec) in NSBitmapImageRep.m Comment: The static list could be wrong, as it was on my system. Also eliminate the supports_lzw_compression flag. 4. +getTIFFCompressionTypes:count: relied on a static list of compressors. Fix: Use the new NSTiffIsCodecConfigured(codec) in NSBitmapImageRep.m Comment: Compares GNUstep supported compressors against actual availability. Also change the private instance methods _localFromCompressionType and _compressionTypeFromLocal to private class methods so that they can be used in -initWithTIFFImage:number: and -TIFFRepresentationUsingCompression:factor: and +getTIFFCompressionTypes:count: This is probably a clumsy implementation but it works. 5. -setProperty:toValue: and -valueForProperty: were not implemented Fix: Add a new instance variable NSMutableDictionary * _properties to NSBitmapImageRep.h and implemented accessors in NSBitmapImageRep.m. Patch -_initFromTIFFImage to set compression type and factor in _properties. Comment: This feature is used to pass options to and from JPEG, PNG, TIFF, and GIF in Cocoa, although the docs are kind of vague. In one case the Cocoa docs said the properties were set when reading a TIFF, but the implementation didn't; I chose to implement the docs. Cocoa does use properties when exporting bitmaps, so I implemented that. 6. Checked and updated NSBitmapImageFileType in NSBitmapImageRep.h Fix: confirmed the enumeration values against Cocoa, and added NSJPEG2000FileType = 5 Comment: JPEG-2000 is not implemented, just reserved a space for it. 7. -representationUsingType:properties: was not implemented Fix: Implement export of TIFF, JPEG, GIF and PNG in NSBitmapImage.m Comment: See the change notes for JPEG, GIF, and PNG for more. BMP and JPEG-2000 are not implemented; they just log a message to that effect. As apparently Cocoa does it this way, if you pass nil for properties, it falls back to the internal _properties, and if that is empty, there are some safe defaults. 8. +representationfOfImageRepsInArray:UsingType:properties: was not implemented Fix: Partially implement in NSBitmapImageRep.m Comment: I just stole the incomplete code from +TIFFRepresentationOfImageRepsInArray: since I have yet to find an explanation of how this really ought to work. 9. JPEG export didn't handle alpha channel, properties or errors. Fix: Add -_JPEGRepresentationWithProperties:errorMessage: to NSBitmapImageRep+JPEG.h and greatly rework Nicolas Roard's code in NSBitmapImageRep+JPEG.m. Patch -_initBitmapFromJPEG:errorMessage to write properties. Comment: Major rewrite of Nicolas Roard's JPEG export code. To do: Support for planar bitmaps and support for colorspaces other than RGB(A). 10. PNG export not implemented Fix: Add -_PNGRepresentationWithProperties: to NSBitmapImageRep+PNG.h and implement NSBitmapImageRep+PNG.m Comment: No support yet for planar bitmaps. Only supports NS*WhiteColorSpace and NS*RGBColorSpace. Does support alpha. Support for reading and writing NSImageGamma is experimental. In keeping with Cocoa, the property NSImageGamma ranges from 0.0 to 1.0; representing the range of minimum supported gamma to maximum supported gamma, in this case 1.0 to 2.5. This is in contrast to GNUstep where by convention the property would range from 0.0 to 255.0. To do: proper error message support 11. GIF export not implemented Fix: Add -_GIFRepresentationWithPropterties:errorMessage: to NSBitmapImageRep+GIF.h and implement in NSBitmapImageRep+GIF.m Comments: Supports only RGB(A) colorspaces, but ignores alpha. Supports planar or interleaved bitmaps. Supports properties NSImageRGBColorTable. 12. -_initBitmapFromGIF:errorMessage: did not support transparency Fix: Don't ignore control blocks in NSBitmapImageRep+GIF.m; check for transparency. Comment: If a transparent color is found, it adds an alpha channel to the bitmap. Also, save the color table in properties. 13. -_initBitmapFromGIF:errorMessage: would show the last image in a multi-image GIF file Fix: Break the parsing loop after the first image in NSBitmapImageRep+GIF.m Comment: Also check for frame duration, and set that property. There is not yet any support for animated GIF. This will require some additional infrastructure, and I won't do it unless asked. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@24140 72102866-910b-0410-8b05-ffd578937521
2006-11-21 06:36:26 +00:00
/***** PNG writing support ******/
static void writer_func(png_structp png_struct, png_bytep data,
png_size_t length)
{
NSMutableData * PNGRep = png_get_io_ptr(png_struct);
[PNGRep appendBytes: data length: length];
}
- (NSData *) _PNGRepresentationWithProperties: (NSDictionary *) properties
{
png_structp png_struct;
png_infop png_info;
int width, height, depth;
unsigned char * bitmapData;
int bytes_per_row;
NSString * colorspace;
NSMutableData * PNGRep = nil;
int type = -1; // illegal value
int interlace = PNG_INTERLACE_NONE;
int transforms = PNG_TRANSFORM_IDENTITY; // no transformations
NSNumber * gammaNumber = nil;
double gamma = 0.0;
// FIXME: Need to convert to non-pre-multiplied format
Change Log Mon. 20-Nov-2006 Mark Tracy <tracy454 at concentric dot net> Many changes related to bitmap images 1. NSBitmapImateRep attribute global strings were not defined Fix: add definitions to externs.h, and declarations to NSBitmapImageRep.h Comment: Two strings defined in Cocoa were commented out: NSImageColorSyncData is proprietary to Apple, and NSImageEXIFData has no support elsewhere in GNUstep. I propose adding GSImageICCProfileData if and when color management is added to GNUstep. 2. LZW compression in TIFF was disabled for lack of a test of its availability Fix: Implement NSTiffIsCodecConfigured(codec) in tiff.m Comment: As of libtiff-3.7.0, there is a function call in the API to test availability at runtime. For libtiff-3.6.0 (earlier?) there are macros #defined in tiffconf.h. The implementation check the library version at compile time, and uses one of the two methods. I have not tested the second method for lack of an installation of an old libtiff. 3. -canCompressUsing: relied on a static list of capabilities Fix: Use the new NSTiffIsCodecConfigured(codec) in NSBitmapImageRep.m Comment: The static list could be wrong, as it was on my system. Also eliminate the supports_lzw_compression flag. 4. +getTIFFCompressionTypes:count: relied on a static list of compressors. Fix: Use the new NSTiffIsCodecConfigured(codec) in NSBitmapImageRep.m Comment: Compares GNUstep supported compressors against actual availability. Also change the private instance methods _localFromCompressionType and _compressionTypeFromLocal to private class methods so that they can be used in -initWithTIFFImage:number: and -TIFFRepresentationUsingCompression:factor: and +getTIFFCompressionTypes:count: This is probably a clumsy implementation but it works. 5. -setProperty:toValue: and -valueForProperty: were not implemented Fix: Add a new instance variable NSMutableDictionary * _properties to NSBitmapImageRep.h and implemented accessors in NSBitmapImageRep.m. Patch -_initFromTIFFImage to set compression type and factor in _properties. Comment: This feature is used to pass options to and from JPEG, PNG, TIFF, and GIF in Cocoa, although the docs are kind of vague. In one case the Cocoa docs said the properties were set when reading a TIFF, but the implementation didn't; I chose to implement the docs. Cocoa does use properties when exporting bitmaps, so I implemented that. 6. Checked and updated NSBitmapImageFileType in NSBitmapImageRep.h Fix: confirmed the enumeration values against Cocoa, and added NSJPEG2000FileType = 5 Comment: JPEG-2000 is not implemented, just reserved a space for it. 7. -representationUsingType:properties: was not implemented Fix: Implement export of TIFF, JPEG, GIF and PNG in NSBitmapImage.m Comment: See the change notes for JPEG, GIF, and PNG for more. BMP and JPEG-2000 are not implemented; they just log a message to that effect. As apparently Cocoa does it this way, if you pass nil for properties, it falls back to the internal _properties, and if that is empty, there are some safe defaults. 8. +representationfOfImageRepsInArray:UsingType:properties: was not implemented Fix: Partially implement in NSBitmapImageRep.m Comment: I just stole the incomplete code from +TIFFRepresentationOfImageRepsInArray: since I have yet to find an explanation of how this really ought to work. 9. JPEG export didn't handle alpha channel, properties or errors. Fix: Add -_JPEGRepresentationWithProperties:errorMessage: to NSBitmapImageRep+JPEG.h and greatly rework Nicolas Roard's code in NSBitmapImageRep+JPEG.m. Patch -_initBitmapFromJPEG:errorMessage to write properties. Comment: Major rewrite of Nicolas Roard's JPEG export code. To do: Support for planar bitmaps and support for colorspaces other than RGB(A). 10. PNG export not implemented Fix: Add -_PNGRepresentationWithProperties: to NSBitmapImageRep+PNG.h and implement NSBitmapImageRep+PNG.m Comment: No support yet for planar bitmaps. Only supports NS*WhiteColorSpace and NS*RGBColorSpace. Does support alpha. Support for reading and writing NSImageGamma is experimental. In keeping with Cocoa, the property NSImageGamma ranges from 0.0 to 1.0; representing the range of minimum supported gamma to maximum supported gamma, in this case 1.0 to 2.5. This is in contrast to GNUstep where by convention the property would range from 0.0 to 255.0. To do: proper error message support 11. GIF export not implemented Fix: Add -_GIFRepresentationWithPropterties:errorMessage: to NSBitmapImageRep+GIF.h and implement in NSBitmapImageRep+GIF.m Comments: Supports only RGB(A) colorspaces, but ignores alpha. Supports planar or interleaved bitmaps. Supports properties NSImageRGBColorTable. 12. -_initBitmapFromGIF:errorMessage: did not support transparency Fix: Don't ignore control blocks in NSBitmapImageRep+GIF.m; check for transparency. Comment: If a transparent color is found, it adds an alpha channel to the bitmap. Also, save the color table in properties. 13. -_initBitmapFromGIF:errorMessage: would show the last image in a multi-image GIF file Fix: Break the parsing loop after the first image in NSBitmapImageRep+GIF.m Comment: Also check for frame duration, and set that property. There is not yet any support for animated GIF. This will require some additional infrastructure, and I won't do it unless asked. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@24140 72102866-910b-0410-8b05-ffd578937521
2006-11-21 06:36:26 +00:00
if ([self isPlanar]) // don't handle planar yet
{
return nil;
}
// get the image parameters
width = [self pixelsWide];
height = [self pixelsHigh];
bytes_per_row = [self bytesPerRow];
colorspace = [self colorSpaceName];
depth = [self bitsPerSample];
gammaNumber = [properties objectForKey: NSImageGamma];
gamma = [gammaNumber doubleValue];
if ([[properties objectForKey: NSImageInterlaced] boolValue])
interlace = PNG_INTERLACE_ADAM7;
if ([colorspace isEqualToString: NSCalibratedWhiteColorSpace] ||
[colorspace isEqualToString: NSDeviceWhiteColorSpace])
type = PNG_COLOR_TYPE_GRAY;
if ([colorspace isEqualToString: NSCalibratedRGBColorSpace] ||
[colorspace isEqualToString: NSDeviceRGBColorSpace])
type = PNG_COLOR_TYPE_RGB;
if ([self hasAlpha]) type = type | PNG_COLOR_MASK_ALPHA;
// make the PNG structures
// ignore errors until I write the handlers
png_struct = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_struct)
{
return nil;
}
png_info = png_create_info_struct(png_struct);
if (!png_info)
{
png_destroy_write_struct(&png_struct, NULL);
return nil;
}
if (setjmp(png_jmpbuf(png_struct)))
{
png_destroy_write_struct(&png_struct, &png_info);
return nil;
}
// init structures
PNGRep = [NSMutableData dataWithLength: 0];
png_info_init_3(&png_info, png_sizeof(png_info));
png_set_write_fn(png_struct, PNGRep, writer_func, NULL);
png_set_IHDR(png_struct, png_info, width, height, depth,
type, interlace, PNG_COMPRESSION_TYPE_BASE,
PNG_FILTER_TYPE_BASE);
if (gammaNumber)
{
NSLog(@"PNGRepresentation: gamma support is experimental");
if (PNG_FLOATING_POINT)
{
// remap gamma [0.0, 1.0] to [1.0, 2.5]
png_set_gAMA(png_struct, png_info, (gamma * 1.5 + 1.0));
}
else // fixed point
{
// remap gamma [0.0, 1.0] to [100000, 250000]
int int_gamma = (int)(gamma * 150000.0 + 100000.0);
png_set_gAMA_fixed(png_struct, png_info, int_gamma);
}
}
// get rgb data and row pointers and
// write PNG out to NSMutableData
bitmapData = [self bitmapData];
{
unsigned char *row_pointers[height];
int i;
for (i = 0 ; i < height ; i++)
row_pointers[i] = bitmapData + i * bytes_per_row;
png_set_rows(png_struct, png_info, row_pointers);
png_write_png(png_struct, png_info, transforms, NULL);
}
//NSLog(@"PNG representation is experimental: %i bytes written", [PNGRep length]);
Change Log Mon. 20-Nov-2006 Mark Tracy <tracy454 at concentric dot net> Many changes related to bitmap images 1. NSBitmapImateRep attribute global strings were not defined Fix: add definitions to externs.h, and declarations to NSBitmapImageRep.h Comment: Two strings defined in Cocoa were commented out: NSImageColorSyncData is proprietary to Apple, and NSImageEXIFData has no support elsewhere in GNUstep. I propose adding GSImageICCProfileData if and when color management is added to GNUstep. 2. LZW compression in TIFF was disabled for lack of a test of its availability Fix: Implement NSTiffIsCodecConfigured(codec) in tiff.m Comment: As of libtiff-3.7.0, there is a function call in the API to test availability at runtime. For libtiff-3.6.0 (earlier?) there are macros #defined in tiffconf.h. The implementation check the library version at compile time, and uses one of the two methods. I have not tested the second method for lack of an installation of an old libtiff. 3. -canCompressUsing: relied on a static list of capabilities Fix: Use the new NSTiffIsCodecConfigured(codec) in NSBitmapImageRep.m Comment: The static list could be wrong, as it was on my system. Also eliminate the supports_lzw_compression flag. 4. +getTIFFCompressionTypes:count: relied on a static list of compressors. Fix: Use the new NSTiffIsCodecConfigured(codec) in NSBitmapImageRep.m Comment: Compares GNUstep supported compressors against actual availability. Also change the private instance methods _localFromCompressionType and _compressionTypeFromLocal to private class methods so that they can be used in -initWithTIFFImage:number: and -TIFFRepresentationUsingCompression:factor: and +getTIFFCompressionTypes:count: This is probably a clumsy implementation but it works. 5. -setProperty:toValue: and -valueForProperty: were not implemented Fix: Add a new instance variable NSMutableDictionary * _properties to NSBitmapImageRep.h and implemented accessors in NSBitmapImageRep.m. Patch -_initFromTIFFImage to set compression type and factor in _properties. Comment: This feature is used to pass options to and from JPEG, PNG, TIFF, and GIF in Cocoa, although the docs are kind of vague. In one case the Cocoa docs said the properties were set when reading a TIFF, but the implementation didn't; I chose to implement the docs. Cocoa does use properties when exporting bitmaps, so I implemented that. 6. Checked and updated NSBitmapImageFileType in NSBitmapImageRep.h Fix: confirmed the enumeration values against Cocoa, and added NSJPEG2000FileType = 5 Comment: JPEG-2000 is not implemented, just reserved a space for it. 7. -representationUsingType:properties: was not implemented Fix: Implement export of TIFF, JPEG, GIF and PNG in NSBitmapImage.m Comment: See the change notes for JPEG, GIF, and PNG for more. BMP and JPEG-2000 are not implemented; they just log a message to that effect. As apparently Cocoa does it this way, if you pass nil for properties, it falls back to the internal _properties, and if that is empty, there are some safe defaults. 8. +representationfOfImageRepsInArray:UsingType:properties: was not implemented Fix: Partially implement in NSBitmapImageRep.m Comment: I just stole the incomplete code from +TIFFRepresentationOfImageRepsInArray: since I have yet to find an explanation of how this really ought to work. 9. JPEG export didn't handle alpha channel, properties or errors. Fix: Add -_JPEGRepresentationWithProperties:errorMessage: to NSBitmapImageRep+JPEG.h and greatly rework Nicolas Roard's code in NSBitmapImageRep+JPEG.m. Patch -_initBitmapFromJPEG:errorMessage to write properties. Comment: Major rewrite of Nicolas Roard's JPEG export code. To do: Support for planar bitmaps and support for colorspaces other than RGB(A). 10. PNG export not implemented Fix: Add -_PNGRepresentationWithProperties: to NSBitmapImageRep+PNG.h and implement NSBitmapImageRep+PNG.m Comment: No support yet for planar bitmaps. Only supports NS*WhiteColorSpace and NS*RGBColorSpace. Does support alpha. Support for reading and writing NSImageGamma is experimental. In keeping with Cocoa, the property NSImageGamma ranges from 0.0 to 1.0; representing the range of minimum supported gamma to maximum supported gamma, in this case 1.0 to 2.5. This is in contrast to GNUstep where by convention the property would range from 0.0 to 255.0. To do: proper error message support 11. GIF export not implemented Fix: Add -_GIFRepresentationWithPropterties:errorMessage: to NSBitmapImageRep+GIF.h and implement in NSBitmapImageRep+GIF.m Comments: Supports only RGB(A) colorspaces, but ignores alpha. Supports planar or interleaved bitmaps. Supports properties NSImageRGBColorTable. 12. -_initBitmapFromGIF:errorMessage: did not support transparency Fix: Don't ignore control blocks in NSBitmapImageRep+GIF.m; check for transparency. Comment: If a transparent color is found, it adds an alpha channel to the bitmap. Also, save the color table in properties. 13. -_initBitmapFromGIF:errorMessage: would show the last image in a multi-image GIF file Fix: Break the parsing loop after the first image in NSBitmapImageRep+GIF.m Comment: Also check for frame duration, and set that property. There is not yet any support for animated GIF. This will require some additional infrastructure, and I won't do it unless asked. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@24140 72102866-910b-0410-8b05-ffd578937521
2006-11-21 06:36:26 +00:00
png_destroy_write_struct(&png_struct, &png_info);
return PNGRep;
}
@end
#else /* !HAVE_LIBPNG */
@implementation NSBitmapImageRep (PNG)
+ (BOOL) _bitmapIsPNG: (NSData *)imageData
{
return NO;
}
- (id) _initBitmapFromPNG: (NSData *)imageData
{
RELEASE(self);
return nil;
}
Change Log Mon. 20-Nov-2006 Mark Tracy <tracy454 at concentric dot net> Many changes related to bitmap images 1. NSBitmapImateRep attribute global strings were not defined Fix: add definitions to externs.h, and declarations to NSBitmapImageRep.h Comment: Two strings defined in Cocoa were commented out: NSImageColorSyncData is proprietary to Apple, and NSImageEXIFData has no support elsewhere in GNUstep. I propose adding GSImageICCProfileData if and when color management is added to GNUstep. 2. LZW compression in TIFF was disabled for lack of a test of its availability Fix: Implement NSTiffIsCodecConfigured(codec) in tiff.m Comment: As of libtiff-3.7.0, there is a function call in the API to test availability at runtime. For libtiff-3.6.0 (earlier?) there are macros #defined in tiffconf.h. The implementation check the library version at compile time, and uses one of the two methods. I have not tested the second method for lack of an installation of an old libtiff. 3. -canCompressUsing: relied on a static list of capabilities Fix: Use the new NSTiffIsCodecConfigured(codec) in NSBitmapImageRep.m Comment: The static list could be wrong, as it was on my system. Also eliminate the supports_lzw_compression flag. 4. +getTIFFCompressionTypes:count: relied on a static list of compressors. Fix: Use the new NSTiffIsCodecConfigured(codec) in NSBitmapImageRep.m Comment: Compares GNUstep supported compressors against actual availability. Also change the private instance methods _localFromCompressionType and _compressionTypeFromLocal to private class methods so that they can be used in -initWithTIFFImage:number: and -TIFFRepresentationUsingCompression:factor: and +getTIFFCompressionTypes:count: This is probably a clumsy implementation but it works. 5. -setProperty:toValue: and -valueForProperty: were not implemented Fix: Add a new instance variable NSMutableDictionary * _properties to NSBitmapImageRep.h and implemented accessors in NSBitmapImageRep.m. Patch -_initFromTIFFImage to set compression type and factor in _properties. Comment: This feature is used to pass options to and from JPEG, PNG, TIFF, and GIF in Cocoa, although the docs are kind of vague. In one case the Cocoa docs said the properties were set when reading a TIFF, but the implementation didn't; I chose to implement the docs. Cocoa does use properties when exporting bitmaps, so I implemented that. 6. Checked and updated NSBitmapImageFileType in NSBitmapImageRep.h Fix: confirmed the enumeration values against Cocoa, and added NSJPEG2000FileType = 5 Comment: JPEG-2000 is not implemented, just reserved a space for it. 7. -representationUsingType:properties: was not implemented Fix: Implement export of TIFF, JPEG, GIF and PNG in NSBitmapImage.m Comment: See the change notes for JPEG, GIF, and PNG for more. BMP and JPEG-2000 are not implemented; they just log a message to that effect. As apparently Cocoa does it this way, if you pass nil for properties, it falls back to the internal _properties, and if that is empty, there are some safe defaults. 8. +representationfOfImageRepsInArray:UsingType:properties: was not implemented Fix: Partially implement in NSBitmapImageRep.m Comment: I just stole the incomplete code from +TIFFRepresentationOfImageRepsInArray: since I have yet to find an explanation of how this really ought to work. 9. JPEG export didn't handle alpha channel, properties or errors. Fix: Add -_JPEGRepresentationWithProperties:errorMessage: to NSBitmapImageRep+JPEG.h and greatly rework Nicolas Roard's code in NSBitmapImageRep+JPEG.m. Patch -_initBitmapFromJPEG:errorMessage to write properties. Comment: Major rewrite of Nicolas Roard's JPEG export code. To do: Support for planar bitmaps and support for colorspaces other than RGB(A). 10. PNG export not implemented Fix: Add -_PNGRepresentationWithProperties: to NSBitmapImageRep+PNG.h and implement NSBitmapImageRep+PNG.m Comment: No support yet for planar bitmaps. Only supports NS*WhiteColorSpace and NS*RGBColorSpace. Does support alpha. Support for reading and writing NSImageGamma is experimental. In keeping with Cocoa, the property NSImageGamma ranges from 0.0 to 1.0; representing the range of minimum supported gamma to maximum supported gamma, in this case 1.0 to 2.5. This is in contrast to GNUstep where by convention the property would range from 0.0 to 255.0. To do: proper error message support 11. GIF export not implemented Fix: Add -_GIFRepresentationWithPropterties:errorMessage: to NSBitmapImageRep+GIF.h and implement in NSBitmapImageRep+GIF.m Comments: Supports only RGB(A) colorspaces, but ignores alpha. Supports planar or interleaved bitmaps. Supports properties NSImageRGBColorTable. 12. -_initBitmapFromGIF:errorMessage: did not support transparency Fix: Don't ignore control blocks in NSBitmapImageRep+GIF.m; check for transparency. Comment: If a transparent color is found, it adds an alpha channel to the bitmap. Also, save the color table in properties. 13. -_initBitmapFromGIF:errorMessage: would show the last image in a multi-image GIF file Fix: Break the parsing loop after the first image in NSBitmapImageRep+GIF.m Comment: Also check for frame duration, and set that property. There is not yet any support for animated GIF. This will require some additional infrastructure, and I won't do it unless asked. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@24140 72102866-910b-0410-8b05-ffd578937521
2006-11-21 06:36:26 +00:00
- (NSData *) _PNGRepresentationWithProperties: (NSDictionary *) properties
{
return nil;
}
@end
#endif /* !HAVE_LIBPNG */