libs-gui/Source/NSBitmapImageRep+GIF.m
Mark Tracy 9f633f60f6 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

561 lines
16 KiB
Objective-C

/* NSBitmapImageRep+GIF.m
Methods for reading GIF images
Copyright (C) 2003, 2004 Free Software Foundation, Inc.
Written by: Stefan Kleine Stegemann <stefan@wms-network.de>
Date: Nov 2003
GIF writing, properties and transparency: Mark Tracy <tracy454@concentric.net>
Date: Nov 2006
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 Library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "NSBitmapImageRep+GIF.h"
#if HAVE_LIBUNGIF || HAVE_LIBGIF
/*
gif_lib.h (4.1.0b1, possibly other versions) uses Object as the name of an
argument to a function. This causes a conflict with Object declared by the
objective-c headers.
*/
#define Object GS_GifLib_Object
#include <gif_lib.h>
#undef Object
#include <Foundation/NSString.h>
#include <Foundation/NSData.h>
#include <Foundation/NSException.h>
#include <Foundation/NSValue.h>
#include "AppKit/NSGraphics.h"
/* -----------------------------------------------------------
The following types and functions are for interacting with
the gif library.
----------------------------------------------------------- */
/* settings for reading interlaced images */
static int InterlaceOffset[] = { 0, 4, 2, 1 };
static int InterlaceJumps[] = { 8, 8, 4, 2 };
/* Holds the information for the input function. */
typedef struct gs_gif_input_src
{
const void *data;
unsigned length;
unsigned pos;
} gs_gif_input_src;
/* Provides data for the gif library. */
static int gs_gif_input(GifFileType *file, GifByteType *buffer, int len)
{
/* according the the libungif sources, this functions has
to act like fread. */
int bytesRead;
gs_gif_input_src *src = (gs_gif_input_src *)file->UserData;
if (src->pos < src->length)
{
if ((src->pos + len) > src->length)
{
bytesRead = (src->length - src->pos);
}
else
{
bytesRead = len;
}
/* We have to copy the data here, looking at
the libungif source makes this clear. */
memcpy(buffer, src->data + src->pos, bytesRead);
src->pos = src->pos + bytesRead;
}
else
{
bytesRead = 0;
}
return bytesRead;
}
/* Initialze a new input source to be used with
gs_gif_input. The passed structure has to be
allocated outside this function. */
static void gs_gif_init_input_source(gs_gif_input_src *src, NSData *data)
{
src->data = [data bytes];
src->length = [data length];
src->pos = 0;
}
/* Function to write GIF to buffer */
static int gs_gif_output(GifFileType *file, const GifByteType *buffer, int len)
{
if (len <= 0) return 0;
NSMutableData * nsData = file->UserData;
[nsData appendBytes: buffer length: len];
return len;
}
/* -----------------------------------------------------------
The gif loading part of NSBitmapImageRep
----------------------------------------------------------- */
@implementation NSBitmapImageRep (GIFReading)
/* Return YES if this looks like a GIF. */
+ (BOOL) _bitmapIsGIF: (NSData *)imageData
{
struct gs_gif_input_src src;
GifFileType* file;
if (!imageData || ![imageData length])
{
return NO;
}
gs_gif_init_input_source(&src, imageData);
file = DGifOpen(&src, gs_gif_input);
if (file == NULL)
{
/* we do not use giferror here because it doesn't
seem to be thread-safe (the error code is a global
variable, so we might get the wrong error here. */
return NO;
}
DGifCloseFile(file);
return YES;
}
#define SET_ERROR_MSG(msg) \
if (errorMsg != NULL) \
{\
*errorMsg = msg; \
}\
else \
{\
NSLog(msg);\
}
#define GIF_CREATE_ERROR(msg) \
SET_ERROR_MSG(msg); \
if (file != NULL) \
{\
DGifCloseFile(file); \
}\
if (imgBuffer != NULL) \
{\
NSZoneFree([self zone], imgBuffer); \
}\
RELEASE(self); \
return nil;
#define CALL_CHECKED(f, where) \
gifrc = f; \
if (gifrc != GIF_OK) \
{\
NSString* msg = [NSString stringWithFormat: @"reading gif failed (%@)", \
where]; \
GIF_CREATE_ERROR(msg);\
}
/* Read a gif image. Assume it is from a gif file. */
- (id) _initBitmapFromGIF: (NSData *)imageData
errorMessage: (NSString **)errorMsg
{
struct gs_gif_input_src src;
GifFileType *file = NULL;
GifRecordType recordType;
GifByteType *extension;
GifPixelType *imgBuffer = NULL;
GifPixelType *imgBufferPos; /* a position inside imgBuffer */
unsigned char *rgbBuffer; /* image converted to rgb */
unsigned rgbBufferPos;
unsigned rgbBufferSize;
ColorMapObject *colorMap;
GifColorType *color;
unsigned char colorIndex;
unsigned pixelSize, rowSize;
int extCode;
int gifrc; /* required by CALL_CHECKED */
int i, j; /* counters */
int imgHeight = 0, imgWidth = 0, imgRow = 0, imgCol = 0;
BOOL hasAlpha = NO;
unsigned char transparentColor = 0;
int sPP = 3; /* samples per pixel */
unsigned short duration = 0;
/* open the image */
gs_gif_init_input_source(&src, imageData);
file = DGifOpen(&src, gs_gif_input);
if (file == NULL)
{
/* we do not use giferror here because it doesn't
seem to be thread-safe (the error code is a global
variable, so we might get the wrong error here. */
GIF_CREATE_ERROR(@"unable to open gif from data");
/* Not reached. */
}
/* allocate a buffer for the decoded image */
pixelSize = sizeof(GifPixelType);
rowSize = file->SWidth * pixelSize;
imgBuffer = NSZoneMalloc([self zone], file->SHeight * rowSize);
if (imgBuffer == NULL)
{
GIF_CREATE_ERROR(@"could not allocate input buffer");
/* Not reached. */
}
/* set the background color */
memset(imgBuffer, file->SBackGroundColor, file->SHeight * rowSize);
/* read the image
* this delivers the first image in a multi-image gif
*/
do
{
CALL_CHECKED(DGifGetRecordType(file, &recordType), @"GetRecordType");
switch (recordType)
{
case IMAGE_DESC_RECORD_TYPE:
{
CALL_CHECKED(DGifGetImageDesc(file), @"GetImageDesc");
imgWidth = file->Image.Width;
imgHeight = file->Image.Height;
imgRow = file->Image.Top;
imgCol = file->Image.Left;
if ((file->Image.Left + file->Image.Width > file->SWidth)
|| (file->Image.Top + file->Image.Height > file->SHeight))
{
GIF_CREATE_ERROR(@"image does not fit into screen dimensions");
}
if (file->Image.Interlace)
{
for (i = 0; i < 4; i++)
{
for (j = imgRow + InterlaceOffset[i]; j < imgRow + imgHeight;
j = j + InterlaceJumps[i])
{
imgBufferPos =
imgBuffer + (j * rowSize) + (imgCol * pixelSize);
CALL_CHECKED(DGifGetLine(file, imgBufferPos, imgWidth),
@"GetLine(Interlaced)");
}
}
}
else
{
for (i = 0; i < imgHeight; i++)
{
imgBufferPos =
imgBuffer + ((imgRow++) * rowSize) + (imgCol * pixelSize);
CALL_CHECKED(DGifGetLine(file, imgBufferPos, imgWidth),
@"GetLine(Non-Interlaced)");
}
}
break;
}
case EXTENSION_RECORD_TYPE:
{
/* transparency support */
CALL_CHECKED(DGifGetExtension(file, &extCode, &extension), @"GetExtension");
if (extCode == GRAPHICS_EXT_FUNC_CODE)
{
hasAlpha = (extension[1] & 0x01);
transparentColor = extension[4];
duration = extension[3];
duration = (duration << 8) + extension[2];
}
while (extension != NULL)
{
CALL_CHECKED(DGifGetExtensionNext(file, &extension), @"GetExtensionNext");
}
break;
}
case TERMINATE_RECORD_TYPE:
default:
{
break;
}
}
} while ((recordType != IMAGE_DESC_RECORD_TYPE)
&& (recordType != TERMINATE_RECORD_TYPE));
/* convert the image to rgb */
sPP = hasAlpha? 4 : 3;
rgbBufferSize = file->SHeight * (file->SWidth * sizeof(unsigned char) * sPP);
rgbBuffer = NSZoneMalloc([self zone], rgbBufferSize);
if (rgbBuffer == NULL)
{
GIF_CREATE_ERROR(@"could not allocate image buffer");
/* Not reached. */
}
colorMap = (file->Image.ColorMap ? file->Image.ColorMap : file->SColorMap);
rgbBufferPos = 0;
for (i = 0; i < file->SHeight; i++)
{
imgBufferPos = imgBuffer + (i * rowSize);
for (j = 0; j < file->SWidth; j++)
{
colorIndex = *(imgBufferPos + j*pixelSize);
color = &colorMap->Colors[colorIndex];
rgbBuffer[rgbBufferPos++] = color->Red;
rgbBuffer[rgbBufferPos++] = color->Green;
rgbBuffer[rgbBufferPos++] = color->Blue;
if (hasAlpha)
rgbBuffer[rgbBufferPos++] = (transparentColor == colorIndex)? 0 : 255;
}
}
NSZoneFree([self zone], imgBuffer);
/* initialize self */
[self initWithBitmapDataPlanes: &rgbBuffer
pixelsWide: file->SWidth
pixelsHigh: file->SHeight
bitsPerSample: 8
samplesPerPixel: sPP
hasAlpha: hasAlpha
isPlanar: NO
colorSpaceName: NSCalibratedRGBColorSpace
bytesPerRow: file->SWidth * sPP
bitsPerPixel: 8 * sPP];
_imageData = [[NSData alloc] initWithBytesNoCopy: rgbBuffer
length: rgbBufferSize];
[self setProperty: NSImageRGBColorTable
withValue: [NSData dataWithBytes: colorMap->Colors
length: sizeof(GifColorType)*colorMap->ColorCount]];
if (duration > 0)
{
[self setProperty: NSImageCurrentFrameDuration
withValue: [NSNumber numberWithFloat: (100.0 * duration)]];
}
[self setProperty: NSImageCurrentFrame
withValue: [NSNumber numberWithInt: 0]];
/* don't forget to close the gif */
DGifCloseFile(file);
return self;
}
- (NSData *) _GIFRepresentationWithProperties: (NSDictionary *) properties
errorMessage: (NSString **)errorMsg
{
NSMutableData * GIFRep = nil; // our return value
GifFileType * GIFFile = NULL;
GifByteType * rgbPlanes = NULL; // giflib needs planar RGB
GifByteType * redPlane = NULL;
GifByteType * greenPlane = NULL;
GifByteType * bluePlane = NULL;
int width, height;
GifByteType * GIFImage = NULL; // intermediate image storage
GifByteType * GIFImageP = NULL;
int h; // general-purpose loop counter
ColorMapObject * GIFColorMap = NULL;
int colorMapSize = 256;
int status; // return status for giflib calls
NSString * colorSpaceName;
BOOL isRGB, hasAlpha;
unsigned char * bitmapData = NULL;
unsigned char * planes[5]; // MAX_PLANES = 5
NSData * colorTable = NULL; // passed in from properties
NSLog(@"GIF representation is experimental");
width = [self pixelsWide];
height = [self pixelsHigh];
if ( !width || !height )
{
SET_ERROR_MSG(@"GIFRepresentation: image is zero size");
return nil;
}
// Giflib wants planar RGB so convert as necessary
colorSpaceName = [self colorSpaceName];
isRGB = ([colorSpaceName isEqualToString: NSDeviceRGBColorSpace] ||
[colorSpaceName isEqualToString: NSCalibratedRGBColorSpace]);
if ( !isRGB )
{
SET_ERROR_MSG(@"GIFRepresentation: Only RGB is supported at this time.");
return nil;
}
hasAlpha = [self hasAlpha];
if ([self isPlanar])
{
[self getBitmapDataPlanes: planes];
redPlane = planes[0];
greenPlane = planes[1];
bluePlane = planes[2];
}
else // interleaved RGB or RGBA
{
OBJC_MALLOC(rgbPlanes, GifByteType, width*height*3);
if ( !rgbPlanes )
{
SET_ERROR_MSG(@"GIFRepresentation: malloc out of memory.");
return nil;
}
redPlane = rgbPlanes;
greenPlane = redPlane + width*height;
bluePlane = greenPlane + width*height;
bitmapData = [self bitmapData];
for (h = 0; h < width*height; h++)
{
*redPlane++ = *bitmapData++;
*greenPlane++ = *bitmapData++;
*bluePlane++ = *bitmapData++;
if (hasAlpha) bitmapData++; // ignore alpha channel
}
redPlane = rgbPlanes;
greenPlane = redPlane + width*height;
bluePlane = greenPlane + width*height;
}
// If you have a color table, you must be certain that it is GIF format
colorTable = [self valueForProperty: NSImageRGBColorTable]; // nil is OK
colorMapSize = (colorTable)? [colorTable length]/sizeof(GifColorType) : 256;
GIFColorMap = MakeMapObject(colorMapSize, [colorTable bytes]);
if ( !GIFColorMap )
{
SET_ERROR_MSG(@"GIFRepresentation (giflib): MakeMapObject() failed.");
OBJC_FREE(rgbPlanes);
return nil;
}
OBJC_MALLOC(GIFImage, GifByteType, height*width);
if ( !GIFImage )
{
SET_ERROR_MSG(@"GIFRepresentation: malloc out of memory.");
OBJC_FREE(rgbPlanes);
}
status = QuantizeBuffer(width, height, &colorMapSize,
redPlane, greenPlane, bluePlane,
GIFImage, GIFColorMap->Colors);
if (status == GIF_ERROR)
{
OBJC_FREE(GIFImage);
OBJC_FREE(rgbPlanes);
return nil;
}
// QuantizeBuffer returns an optimized colorMapSize,
// but we must round up to nearest power of 2
// otherwise MakeColorMap() fails
for (h = 0; h < 8; h++)
if ((1<<h) >= colorMapSize) break;
colorMapSize = 1<<h;
GIFColorMap->ColorCount = colorMapSize;
GIFColorMap->BitsPerPixel = h;
if ( ![self isPlanar] ) OBJC_FREE(rgbPlanes);
// Write the converted image out to the NSData
GIFRep = [NSMutableData dataWithLength: 0];
if ( !GIFRep )
{
OBJC_FREE(GIFImage);
return nil;
}
GIFFile = EGifOpen(GIFRep, gs_gif_output);
status = EGifPutScreenDesc(GIFFile, width, height, 8, 0, NULL);
if (status == GIF_ERROR)
{
SET_ERROR_MSG(@"GIFRepresentation (giflib): EGifPutScreenDesc() failed.");
OBJC_FREE(GIFImage);
return nil;
}
// note we are not supporting interlaced mode
status = EGifPutImageDesc(GIFFile, 0, 0, width, height, FALSE, GIFColorMap);
if (status == GIF_ERROR)
{
SET_ERROR_MSG(@"GIFRepresentation (giflib): EGifPutImageDesc() failed.");
OBJC_FREE(GIFImage);
return nil;
}
GIFImageP = GIFImage;
for (h = 0; h < height ; h++)
{
status = EGifPutLine(GIFFile, GIFImageP, width);
if (status == GIF_ERROR)
{
SET_ERROR_MSG(@"GIFRepresentation (giflib): EGifPutLine() failed.");
OBJC_FREE(GIFImage);
return nil;
}
GIFImageP += width;
}
status = EGifCloseFile(GIFFile);
OBJC_FREE(GIFImage);
return GIFRep;
}
@end
#else /* !HAVE_LIBUNGIF || !HAVE_LIBGIF */
@implementation NSBitmapImageRep (GIFReading)
+ (BOOL) _bitmapIsGIF: (NSData *)imageData
{
return NO;
}
- (id) _initBitmapFromGIF: (NSData *)imageData
errorMessage: (NSString **)errorMsg
{
RELEASE(self);
return nil;
}
- (NSData *) _GIFRepresentationWithProperties: (NSDictionary *) properties
errorMessage: (NSString **)errorMsg
{
return nil;
}
@end
#endif /* !HAVE_LIBUNGIF || !HAVE_LIBGIF */