Image encoding support - first cut

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@5633 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 1999-12-28 16:02:01 +00:00
parent 5ca0d2c57d
commit ec738f36de
9 changed files with 556 additions and 234 deletions

View file

@ -1,3 +1,15 @@
Tue Dec 28 15:58:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
Initial hack of support for encoding/decoding images - untested.
* Source/NSBitmapImageRep.m: Code for writing tiffs etc.
* Source/NSImage.m: Archiving support and tifying.
* Source/NSImageRep.m: Archiving support and tifying.
* Source/tiff.m: modifications for compression and for writing.
* Headers/gnustep/gui/NSBitmapImageRep.h: tidy and support writing.
* Headers/gnustep/gui/NSImage.h: ditto
* Headers/gnustep/gui/NSImageRep.h: ditto
* Headers/gnustep/gui/nsimage-tiff.h: ditto
1999-12-24 David Lazaro <khelekir@encomix.es>
* Source/NSMenuView.m ([NSMenuView -trackWithEvent:]): Added hysteresis

View file

@ -50,80 +50,81 @@ typedef enum _NSTIFFCompression {
@interface NSBitmapImageRep : NSImageRep <NSCoding>
{
// Attributes
unsigned int bytesPerRow;
unsigned int numColors;
unsigned int bitsPerPixel;
unsigned short compression;
BOOL _isPlanar;
unsigned char** imagePlanes;
NSMutableData* imageData;
unsigned int bytesPerRow;
unsigned int numColors;
unsigned int bitsPerPixel;
unsigned short compression;
float comp_factor;
BOOL _isPlanar;
unsigned char **imagePlanes;
NSMutableData *imageData;
// Reserved for back-end use
void *back_end_reserved;
void *back_end_reserved;
}
//
// Allocating and Initializing a New NSBitmapImageRep Object
//
+ (id)imageRepWithData:(NSData *)tiffData;
+ (NSArray *)imageRepsWithData:(NSData *)tiffData;
- (id)initWithData:(NSData *)tiffData;
- (id)initWithFocusedViewRect:(NSRect)rect;
- (id)initWithBitmapDataPlanes:(unsigned char **)planes
pixelsWide:(int)width
pixelsHigh:(int)height
bitsPerSample:(int)bps
samplesPerPixel:(int)spp
hasAlpha:(BOOL)alpha
isPlanar:(BOOL)config
colorSpaceName:(NSString *)colorSpaceName
bytesPerRow:(int)rowBytes
bitsPerPixel:(int)pixelBits;
+ (id) imageRepWithData: (NSData*)tiffData;
+ (NSArray*) imageRepsWithData: (NSData*)tiffData;
- (id) initWithData: (NSData*)tiffData;
- (id) initWithFocusedViewRect: (NSRect)rect;
- (id) initWithBitmapDataPlanes: (unsigned char**)planes
pixelsWide: (int)width
pixelsHigh: (int)height
bitsPerSample: (int)bps
samplesPerPixel: (int)spp
hasAlpha: (BOOL)alpha
isPlanar: (BOOL)config
colorSpaceName: (NSString*)colorSpaceName
bytesPerRow: (int)rowBytes
bitsPerPixel: (int)pixelBits;
//
// Getting Information about the Image
//
- (int)bitsPerPixel;
- (int)samplesPerPixel;
- (BOOL)isPlanar;
- (int)numberOfPlanes;
- (int)bytesPerPlane;
- (int)bytesPerRow;
- (int) bitsPerPixel;
- (int) samplesPerPixel;
- (BOOL) isPlanar;
- (int) numberOfPlanes;
- (int) bytesPerPlane;
- (int) bytesPerRow;
//
// Getting Image Data
//
- (unsigned char *)bitmapData;
- (void)getBitmapDataPlanes:(unsigned char **)data;
- (unsigned char*) bitmapData;
- (void) getBitmapDataPlanes: (unsigned char**)data;
//
// Producing a TIFF Representation of the Image
//
+ (NSData *)TIFFRepresentationOfImageRepsInArray:(NSArray *)anArray;
+ (NSData *)TIFFRepresentationOfImageRepsInArray:(NSArray *)anArray
usingCompression:(NSTIFFCompression)compressionType
factor:(float)factor;
- (NSData *)TIFFRepresentation;
- (NSData *)TIFFRepresentationUsingCompression:(NSTIFFCompression)compressionType
factor:(float)factor;
+ (NSData*) TIFFRepresentationOfImageRepsInArray: (NSArray*)anArray;
+ (NSData*) TIFFRepresentationOfImageRepsInArray: (NSArray*)anArray
usingCompression: (NSTIFFCompression)type
factor: (float)factor;
- (NSData*) TIFFRepresentation;
- (NSData*) TIFFRepresentationUsingCompression: (NSTIFFCompression)type
factor: (float)factor;
//
// Setting and Checking Compression Types
//
+ (void)getTIFFCompressionTypes:(const NSTIFFCompression **)list
count:(int *)numTypes;
+ (NSString *)localizedNameForTIFFCompressionType:(NSTIFFCompression)compression;
- (BOOL)canBeCompressedUsing:(NSTIFFCompression)compression;
- (void)getCompression:(NSTIFFCompression *)compression
factor:(float *)factor;
- (void)setCompression:(NSTIFFCompression)compression
factor:(float)factor;
+ (void) getTIFFCompressionTypes: (const NSTIFFCompression**)list
count: (int*)numTypes;
+ (NSString*) localizedNameForTIFFCompressionType: (NSTIFFCompression)type;
- (BOOL) canBeCompressedUsing: (NSTIFFCompression)compression;
- (void) getCompression: (NSTIFFCompression*)compression
factor: (float*)factor;
- (void) setCompression: (NSTIFFCompression)compression
factor: (float)factor;
//
// NSCoding protocol
//
- (void)encodeWithCoder:aCoder;
- initWithCoder:aDecoder;
- (void) encodeWithCoder: (NSCoder*)aCoder;
- (id) initWithCoder: (NSCoder*)aDecoder;
@end

View file

@ -46,26 +46,20 @@
@interface NSImage : NSObject <NSCoding>
{
// Attributes
NSString* name;
NSString *name;
NSSize _size;
struct __imageFlags {
unsigned scalable:1;
unsigned dataRetained:1;
unsigned flipDraw:1;
unsigned uniqueWindow:1;
unsigned uniqueWasExplicitlySet:1;
unsigned sizeWasExplicitlySet:1;
unsigned builtIn:1;
unsigned needsToExpand:1;
unsigned useEPSOnResolutionMismatch:1;
unsigned colorMatchPreferred:1;
unsigned multipleResolutionMatching:1;
unsigned subImage:1;
unsigned aSynch:1;
unsigned archiveByName:1;
unsigned cacheSeparately:1;
unsigned unboundedCacheDepth:1;
unsigned syncLoad:1;
unsigned archiveByName: 1;
unsigned scalable: 1;
unsigned dataRetained: 1;
unsigned flipDraw: 1;
unsigned sizeWasExplicitlySet: 1;
unsigned useEPSOnResolutionMismatch: 1;
unsigned colorMatchPreferred: 1;
unsigned multipleResolutionMatching: 1;
unsigned cacheSeparately: 1;
unsigned unboundedCacheDepth: 1;
unsigned syncLoad: 1;
} _flags;
NSMutableArray *_reps;
NSColor *_color;
@ -76,131 +70,131 @@
//
// Initializing a New NSImage Instance
//
- (id)initByReferencingFile:(NSString *)filename;
- (id)initWithContentsOfFile:(NSString *)filename;
- (id)initWithData:(NSData *)data;
- (id)initWithPasteboard:(NSPasteboard *)pasteboard;
- (id)initWithSize:(NSSize)aSize;
- (id) initByReferencingFile: (NSString*)filename;
- (id) initWithContentsOfFile: (NSString*)filename;
- (id) initWithData: (NSData*)data;
- (id) initWithPasteboard: (NSPasteboard*)pasteboard;
- (id) initWithSize: (NSSize)aSize;
//
// Setting the Size of the Image
//
- (void)setSize:(NSSize)aSize;
- (NSSize)size;
- (void) setSize: (NSSize)aSize;
- (NSSize) size;
//
// Referring to Images by Name
//
+ (id)imageNamed:(NSString *)name;
- (BOOL)setName:(NSString *)name;
- (NSString *)name;
+ (id) imageNamed: (NSString*)name;
- (BOOL) setName: (NSString*)name;
- (NSString*) name;
//
// Specifying the Image
//
- (void)addRepresentation:(NSImageRep *)imageRep;
- (void)addRepresentations:(NSArray *)imageRepArray;
- (void)lockFocus;
- (void)lockFocusOnRepresentation:(NSImageRep *)imageRep;
- (void)unlockFocus;
- (void) addRepresentation: (NSImageRep*)imageRep;
- (void) addRepresentations: (NSArray*)imageRepArray;
- (void) lockFocus;
- (void) lockFocusOnRepresentation: (NSImageRep*)imageRep;
- (void) unlockFocus;
//
// Using the Image
//
- (void)compositeToPoint:(NSPoint)aPoint
operation:(NSCompositingOperation)op;
- (void)compositeToPoint:(NSPoint)aPoint
fromRect:(NSRect)aRect
operation:(NSCompositingOperation)op;
- (void)dissolveToPoint:(NSPoint)aPoint
fraction:(float)aFloat;
- (void)dissolveToPoint:(NSPoint)aPoint
fromRect:(NSRect)aRect
fraction:(float)aFloat;
- (void) compositeToPoint: (NSPoint)aPoint
operation: (NSCompositingOperation)op;
- (void) compositeToPoint: (NSPoint)aPoint
fromRect: (NSRect)aRect
operation: (NSCompositingOperation)op;
- (void) dissolveToPoint: (NSPoint)aPoint
fraction: (float)aFloat;
- (void) dissolveToPoint: (NSPoint)aPoint
fromRect: (NSRect)aRect
fraction: (float)aFloat;
//
// Choosing Which Image Representation to Use
//
- (void)setPrefersColorMatch:(BOOL)flag;
- (BOOL)prefersColorMatch;
- (void)setUsesEPSOnResolutionMismatch:(BOOL)flag;
- (BOOL)usesEPSOnResolutionMismatch;
- (void)setMatchesOnMultipleResolution:(BOOL)flag;
- (BOOL)matchesOnMultipleResolution;
- (void) setPrefersColorMatch: (BOOL)flag;
- (BOOL) prefersColorMatch;
- (void) setUsesEPSOnResolutionMismatch: (BOOL)flag;
- (BOOL) usesEPSOnResolutionMismatch;
- (void) setMatchesOnMultipleResolution: (BOOL)flag;
- (BOOL) matchesOnMultipleResolution;
//
// Getting the Representations
//
- (NSImageRep *)bestRepresentationForDevice:(NSDictionary *)deviceDescription;
- (NSArray *)representations;
- (void)removeRepresentation:(NSImageRep *)imageRep;
- (NSImageRep*) bestRepresentationForDevice: (NSDictionary*)deviceDescription;
- (NSArray*) representations;
- (void) removeRepresentation: (NSImageRep*)imageRep;
//
// Determining How the Image is Stored
//
- (void)setCachedSeparately:(BOOL)flag;
- (BOOL)isCachedSeparately;
- (void)setDataRetained:(BOOL)flag;
- (BOOL)isDataRetained;
- (void)setCacheDepthMatchesImageDepth:(BOOL)flag;
- (BOOL)cacheDepthMatchesImageDepth;
- (void) setCachedSeparately: (BOOL)flag;
- (BOOL) isCachedSeparately;
- (void) setDataRetained: (BOOL)flag;
- (BOOL) isDataRetained;
- (void) setCacheDepthMatchesImageDepth: (BOOL)flag;
- (BOOL) cacheDepthMatchesImageDepth;
//
// Determining How the Image is Drawn
//
- (BOOL)isValid;
- (void)setScalesWhenResized:(BOOL)flag;
- (BOOL)scalesWhenResized;
- (void)setBackgroundColor:(NSColor *)aColor;
- (NSColor *)backgroundColor;
- (BOOL)drawRepresentation:(NSImageRep *)imageRep
inRect:(NSRect)aRect;
- (void)recache;
- (BOOL) isValid;
- (void) setScalesWhenResized: (BOOL)flag;
- (BOOL) scalesWhenResized;
- (void) setBackgroundColor: (NSColor*)aColor;
- (NSColor*) backgroundColor;
- (BOOL) drawRepresentation: (NSImageRep*)imageRep
inRect: (NSRect)aRect;
- (void) recache;
//
// Assigning a Delegate
//
- (void)setDelegate:(id)anObject;
- (id)delegate;
- (void) setDelegate: (id)anObject;
- (id) delegate;
//
// Producing TIFF Data for the Image
//
- (NSData *)TIFFRepresentation;
- (NSData *)TIFFRepresentationUsingCompression:(NSTIFFCompression)comp
factor:(float)aFloat;
- (NSData*) TIFFRepresentation;
- (NSData*) TIFFRepresentationUsingCompression: (NSTIFFCompression)comp
factor: (float)aFloat;
//
// Managing NSImageRep Subclasses
//
+ (NSArray *)imageUnfilteredFileTypes;
+ (NSArray *)imageUnfilteredPasteboardTypes;
+ (NSArray*) imageUnfilteredFileTypes;
+ (NSArray*) imageUnfilteredPasteboardTypes;
//
// Testing Image Data Sources
//
+ (BOOL)canInitWithPasteboard:(NSPasteboard *)pasteboard;
+ (NSArray *)imageFileTypes;
+ (NSArray *)imagePasteboardTypes;
+ (BOOL) canInitWithPasteboard: (NSPasteboard*)pasteboard;
+ (NSArray*) imageFileTypes;
+ (NSArray*) imagePasteboardTypes;
//
// Methods Implemented by the Delegate
//
- (NSImage *)imageDidNotDraw:(id)sender
inRect:(NSRect)aRect;
- (NSImage*) imageDidNotDraw: (id)sender
inRect: (NSRect)aRect;
//
// NSCoding protocol
//
- (void)encodeWithCoder:aCoder;
- initWithCoder:aDecoder;
- (void) encodeWithCoder: (NSCoder*)aCoder;
- (id) initWithCoder: (NSCoder*)aDecoder;
@end
@interface NSBundle (NSImageAdditions)
- (NSString *)pathForImageResource:(NSString *)name;
- (NSString*) pathForImageResource: (NSString*)name;
@end

View file

@ -42,7 +42,7 @@ enum {
NSImageRepMatchesDevice
};
@interface NSImageRep : NSObject <NSCoding>
@interface NSImageRep : NSObject <NSCoding, NSCopying>
{
// Attributes
NSString* _colorSpace;

View file

@ -48,6 +48,7 @@ typedef struct {
u_short planarConfig; /* meshed or separate */
u_short photoInterp; /* photometric interpretation of bitmap data, */
u_short compression;
int quality; /* compression quality (for jpeg) 1 to 255 */
int numImages; /* number of images in tiff */
int error;
} NSTiffInfo;
@ -61,8 +62,8 @@ typedef struct {
typedef char* realloc_data_callback(char* data, long size);
extern TIFF* NSTiffOpenData(char* data, long size, const char* mode,
realloc_data_callback* realloc_data);
extern TIFF* NSTiffOpenDataRead(const char* data, long size);
extern TIFF* NSTiffOpenDataWrite(char **data, long *size);
extern int NSTiffClose(TIFF* image);
extern int NSTiffWrite(TIFF* image, NSTiffInfo* info, char* data);

View file

@ -93,6 +93,7 @@
bytesPerRow: 0
bitsPerPixel: 0];
compression = info->compression;
comp_factor = 255 * (1 - ((float)info->quality)/100.0);
if (NSTiffRead(imageNumber, image, NULL, [self bitmapData]))
{
@ -112,15 +113,14 @@
return nil;
}
+ (NSArray *) imageRepsWithData: (NSData *)tiffData
+ (NSArray*) imageRepsWithData: (NSData *)tiffData
{
int images;
TIFF* image;
NSTiffInfo* info;
NSMutableArray* array;
int images;
TIFF *image;
NSTiffInfo *info;
NSMutableArray*array;
image = NSTiffOpenData((char *)[tiffData bytes], [tiffData length],
"r", NULL);
image = NSTiffOpenDataRead((char *)[tiffData bytes], [tiffData length]);
if (!image)
{
[NSException raise:NSTIFFException format: @"Read invalid TIFF data"];
@ -133,8 +133,8 @@
NSBitmapImageRep* imageRep;
OBJC_FREE(info);
imageRep = [[[[self class] alloc]
_initFromImage: image number: images] autorelease];
imageRep = AUTORELEASE([[[self class] alloc]
_initFromImage: image number: images]);
[array addObject: imageRep];
images++;
}
@ -149,9 +149,8 @@
{
TIFF *image;
image = NSTiffOpenData((char *)[tiffData bytes], [tiffData length],
"r", NULL);
if (!image)
image = NSTiffOpenDataRead((char *)[tiffData bytes], [tiffData length]);
if (image == 0)
{
[NSException raise:NSTIFFException format: @"Read invalid TIFF data"];
}
@ -195,7 +194,7 @@
numColors = spp;
hasAlpha = alpha;
_isPlanar = isPlanar;
_colorSpace = [colorSpaceName retain];
_colorSpace = RETAIN(colorSpaceName);
if (!pixelBits)
pixelBits = bps * ((_isPlanar) ? 1 : spp);
bitsPerPixel = pixelBits;
@ -218,7 +217,7 @@
- (void) dealloc
{
OBJC_FREE(imagePlanes);
[imageData release];
RELEASE(imageData);
[super dealloc];
}
@ -232,16 +231,18 @@
copy->numColors = numColors;
copy->bitsPerPixel = bitsPerPixel;
copy->compression = compression;
copy->comp_factor = comp_factor;
copy->_isPlanar = _isPlanar;
copy->imagePlanes = 0;
copy->imageData = [imageData copy];
return copy;
}
+ (BOOL) canInitWithData: (NSData *)data
{
TIFF *image = NULL;
image = NSTiffOpenData((char *)[data bytes], [data length], "r", NULL);
TIFF *image = NULL;
image = NSTiffOpenDataRead((char *)[data bytes], [data length]);
NSTiffClose(image);
return (image) ? YES : NO;
@ -249,8 +250,7 @@
+ (BOOL) canInitWithPasteboard: (NSPasteboard *)pasteboard
{
[self notImplemented: _cmd];
return NO;
return [[pasteboard types] containsObject: NSTIFFPboardType];
}
+ (NSArray *) imageFileTypes
@ -327,7 +327,7 @@
length = (long)numColors * bytesPerRow * _pixelsHigh
* sizeof(unsigned char);
imageData = [[NSMutableData dataWithLength: length] retain];
imageData = RETAIN([NSMutableData dataWithLength: length]);
if (!imagePlanes)
OBJC_MALLOC(imagePlanes, unsigned char*, MAX_PLANES);
bits = [imageData mutableBytes];
@ -359,64 +359,150 @@
//
// Producing a TIFF Representation of the Image
//
+ (NSData *) TIFFRepresentationOfImageRepsInArray: (NSArray *)anArray
+ (NSData*) TIFFRepresentationOfImageRepsInArray: (NSArray *)anArray
{
[self notImplemented: _cmd];
return nil;
}
+ (NSData *) TIFFRepresentationOfImageRepsInArray: (NSArray *)anArray
usingCompression: (NSTIFFCompression)compressionType
factor: (float)factor
+ (NSData*) TIFFRepresentationOfImageRepsInArray: (NSArray *)anArray
usingCompression: (NSTIFFCompression)type
factor: (float)factor
{
[self notImplemented: _cmd];
return nil;
}
- (NSData *) TIFFRepresentation
- (NSData*) TIFFRepresentation
{
[self notImplemented: _cmd];
return nil;
NSTiffInfo info;
TIFF *image;
char *bytes = 0;
long length = 0;
info.imageNumber = 0;
info.subfileType = 255;
info.width = _pixelsWide;
info.height = _pixelsHigh;
info.bitsPerSample = bitsPerSample;
info.samplesPerPixel = numColors;
if (_isPlanar)
info.planarConfig = PLANARCONFIG_SEPARATE;
else
info.planarConfig = PLANARCONFIG_CONTIG;
if (_colorSpace == NSDeviceRGBColorSpace)
info.photoInterp = PHOTOMETRIC_RGB;
else if (_colorSpace == NSDeviceWhiteColorSpace)
info.photoInterp = PHOTOMETRIC_MINISBLACK;
else if (_colorSpace == NSDeviceBlackColorSpace)
info.photoInterp = PHOTOMETRIC_MINISWHITE;
else
info.photoInterp = PHOTOMETRIC_RGB;
info.compression = compression;
info.quality = (1 - ((float)comp_factor)/255.0) * 100;
info.numImages = 1;
info.error = 0;
image = NSTiffOpenDataWrite(&bytes, &length);
if (image == 0)
{
[NSException raise: NSTIFFException format: @"Write TIFF open failed"];
}
if (NSTiffWrite(image, &info, [self bitmapData]) != 0)
{
[NSException raise: NSTIFFException format: @"Write TIFF data failed"];
}
NSTiffClose(image);
return [NSData dataWithBytesNoCopy: bytes length: length];
}
- (NSData *) TIFFRepresentationUsingCompression: (NSTIFFCompression)compressionType
factor: (float)factor
- (NSData*) TIFFRepresentationUsingCompression: (NSTIFFCompression)type
factor: (float)factor
{
[self notImplemented: _cmd];
return nil;
NSData *data;
NSTIFFCompression oldType = compression;
float oldFact = comp_factor;
[self setCompression: type factor: factor];
data = [self TIFFRepresentation];
[self setCompression: oldType factor: oldFact];
return data;
}
//
// Setting and Checking Compression Types
//
+ (void) getTIFFCompressionTypes: (const NSTIFFCompression **)list
count: (int *)numTypes
count: (int *)numTypes
{
[self notImplemented: _cmd];
static NSTIFFCompression types[] = {
NSTIFFCompressionNone,
NSTIFFCompressionCCITTFAX3,
NSTIFFCompressionCCITTFAX4,
NSTIFFCompressionLZW,
NSTIFFCompressionJPEG,
NSTIFFCompressionPackBits
};
*list = types;
*numTypes = sizeof(types)/sizeof(*types);
}
+ (NSString *) localizedNameForTIFFCompressionType: (NSTIFFCompression)compression
+ (NSString*) localizedNameForTIFFCompressionType: (NSTIFFCompression)type
{
[self notImplemented: _cmd];
return nil;
switch (type)
{
case NSTIFFCompressionNone: return @"NSTIFFCompressionNone";
case NSTIFFCompressionCCITTFAX3: return @"NSTIFFCompressionCCITTFAX3";
case NSTIFFCompressionCCITTFAX4: return @"NSTIFFCompressionCCITTFAX4";
case NSTIFFCompressionLZW: return @"NSTIFFCompressionLZW";
case NSTIFFCompressionJPEG: return @"NSTIFFCompressionJPEG";
case NSTIFFCompressionNEXT: return @"NSTIFFCompressionNEXT";
case NSTIFFCompressionPackBits: return @"NSTIFFCompressionPackBits";
case NSTIFFCompressionOldJPEG: return @"NSTIFFCompressionOldJPEG";
default: return nil;
}
}
- (BOOL) canBeCompressedUsing: (NSTIFFCompression)compression
- (BOOL) canBeCompressedUsing: (NSTIFFCompression)type
{
[self notImplemented: _cmd];
return NO;
switch (type)
{
case NSTIFFCompressionCCITTFAX3:
case NSTIFFCompressionCCITTFAX4:
if (numColors == 1 && bitsPerSample == 1)
return YES;
else
return NO;
case NSTIFFCompressionNone:
case NSTIFFCompressionLZW:
case NSTIFFCompressionJPEG:
case NSTIFFCompressionPackBits:
return YES;
case NSTIFFCompressionNEXT:
case NSTIFFCompressionOldJPEG:
default:
return NO;
}
}
- (void) getCompression: (NSTIFFCompression *)compression
factor: (float *)factor
- (void) getCompression: (NSTIFFCompression*)type
factor: (float*)factor
{
[self notImplemented: _cmd];
*type = compression;
*factor = comp_factor;
}
- (void) setCompression: (NSTIFFCompression)compression
factor: (float)factor
- (void) setCompression: (NSTIFFCompression)type
factor: (float)factor
{
[self notImplemented: _cmd];
compression = type;
comp_factor = factor;
}
//
@ -424,13 +510,19 @@
//
- (void) encodeWithCoder: (NSCoder*)aCoder
{
[self notImplemented: _cmd];
NSData *data = [self TIFFRepresentation];
[super encodeWithCoder: aCoder];
[data encodeWithCoder: aCoder];
}
- (id) initWithCoder: (NSCoder*)aDecoder
{
[self notImplemented: _cmd];
return nil;
NSData *data;
self = [super initWithCoder: aDecoder];
data = [aDecoder decodeObject];
return [self initWithData: data];
}
@end

View file

@ -253,10 +253,11 @@ static Class cacheClass = 0;
image = [[self allocWithZone: NSDefaultMallocZone()]
initByReferencingFile: path];
if (image)
if (image != nil)
{
[image setName: aName];
RELEASE(image); // Retained in dictionary.
image->_flags.archiveByName = YES;
}
return image;
}
@ -763,11 +764,10 @@ static Class cacheClass = 0;
unsigned i, count;
GSRepData *repd;
if (!imageRepArray)
return;
if (_flags.syncLoad)
[self _loadImageFilenames];
if (_flags.syncLoad == YES)
{
[self _loadImageFilenames];
}
count = [imageRepArray count];
for (i = 0; i < count; i++)
{
@ -1067,10 +1067,114 @@ NSDebugLLog(@"NSImage", @"Partial %@ ... %@ %d", repd->bg, _color, repd->rep);
// NSCoding
- (void) encodeWithCoder: (NSCoder*)coder
{
BOOL flag;
flag = _flags.archiveByName;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
if (flag == YES)
{
/*
* System image - just encode the name.
*/
[coder encodeValueOfObjCType: @encode(id) at: &name];
}
else
{
NSMutableArray *a;
NSEnumerator *e;
NSImageRep *r;
/*
* If the image is not yet loaded - load it now.
*/
if (_flags.syncLoad)
{
[self _loadImageFilenames];
}
/*
* Normal image - encode the ivars
*/
[coder encodeValueOfObjCType: @encode(NSSize) at: &_size];
[coder encodeValueOfObjCType: @encode(id) at: &_color];
flag = _flags.scalable;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _flags.dataRetained;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _flags.flipDraw;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _flags.sizeWasExplicitlySet;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _flags.useEPSOnResolutionMismatch;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _flags.colorMatchPreferred;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _flags.multipleResolutionMatching;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _flags.cacheSeparately;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _flags.unboundedCacheDepth;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
/*
* Now encode an array of all the image reps (excluding cache)
*/
a = [NSMutableArray arrayWithCapacity: 2];
e = [[self representations] objectEnumerator];
while ((r = [e nextObject]) != nil)
{
if ([r isKindOfClass: [NSCachedImageRep class]] == NO)
{
[a addObject: r];
}
}
[coder encodeValueOfObjCType: @encode(id) at: &a];
}
}
- (id) initWithCoder: (NSCoder*)coder
{
BOOL flag;
[coder decodeValueOfObjCType: @encode(BOOL) at: &flag];
if (flag == YES)
{
NSString *theName = [coder decodeObject];
RELEASE(self);
self = RETAIN([NSImage imageNamed: theName]);
}
else
{
NSArray *a;
[coder decodeValueOfObjCType: @encode(NSSize) at: &_size];
[coder decodeValueOfObjCType: @encode(id) at: &_color];
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.scalable = flag;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.dataRetained = flag;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.flipDraw = flag;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.sizeWasExplicitlySet = flag;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.useEPSOnResolutionMismatch = flag;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.colorMatchPreferred = flag;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.multipleResolutionMatching = flag;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.cacheSeparately = flag;
[coder encodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.unboundedCacheDepth = flag;
/*
* get the image reps and add them.
*/
a = [coder decodeObject];
[self addRepresentations: a];
}
return self;
}

View file

@ -30,6 +30,7 @@
#include <string.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSData.h>
#include <Foundation/NSException.h>
#include <Foundation/NSNotification.h>
#include <AppKit/NSImageRep.h>
#include <AppKit/NSBitmapImageRep.h>
@ -85,20 +86,15 @@ static NSMutableArray* imageReps = NULL;
for (i = 0; i < count; i++)
{
Class rep = [imageReps objectAtIndex: i];
#if 0
if ([[rep imageFileTypes] indexOfObject: ext] != NSNotFound)
#else
/* xxxFIXME: not implemented in gcc-2.7.2 runtime. */
if ([rep respondsToSelector: @selector(imageFileTypes)]
&& [[rep imageFileTypes] indexOfObject: ext] != NSNotFound)
#endif
{
NSData* data = [NSData dataWithContentsOfFile: filename];
if ([rep respondsToSelector: @selector(imageRepsWithData:)])
[array addObjectsFromArray: [rep imageRepsWithData: data]];
else if ([rep respondsToSelector: @selector(imageRepWithData:)])
[array addObject: [rep imageRepWithData: data]];
}
{
NSData* data = [NSData dataWithContentsOfFile: filename];
if ([rep respondsToSelector: @selector(imageRepsWithData:)])
[array addObjectsFromArray: [rep imageRepsWithData: data]];
else if ([rep respondsToSelector: @selector(imageRepWithData:)])
[array addObject: [rep imageRepWithData: data]];
}
}
return (NSArray *)array;
}
@ -125,11 +121,12 @@ static NSMutableArray* imageReps = NULL;
{
NSString* ptype;
Class rep = [imageReps objectAtIndex: i];
if ([rep respondsToSelector: @selector(imagePasteboardTypes)]
&& (ptype =
[pasteboard availableTypeFromArray: [rep imagePasteboardTypes]]))
ptype = [pasteboard availableTypeFromArray: [rep imagePasteboardTypes]];
if (ptype != nil)
{
NSData* data = [pasteboard dataForType: ptype];
if ([rep respondsToSelector: @selector(imageRepsWithData:)])
[array addObjectsFromArray: [rep imageRepsWithData: data]];
else if ([rep respondsToSelector: @selector(imageRepWithData:)])
@ -350,8 +347,7 @@ static NSMutableArray* imageReps = NULL;
for (i = 0; i < count; i++)
{
Class rep = [imageReps objectAtIndex: i];
if ([rep respondsToSelector: @selector(imageFileTypes)]
&& [[rep imageFileTypes] indexOfObject: type] != NSNotFound)
if ([[rep imageFileTypes] indexOfObject: type] != NSNotFound)
{
return rep;
}
@ -367,8 +363,8 @@ static NSMutableArray* imageReps = NULL;
for (i = 0; i < count; i++)
{
Class rep = [imageReps objectAtIndex: i];
if ([rep respondsToSelector: @selector(imagePasteboardTypes)]
&& ([[rep imagePasteboardTypes] indexOfObject: type] != NSNotFound))
if ([[rep imagePasteboardTypes] indexOfObject: type] != NSNotFound)
{
return rep;
}
@ -378,7 +374,21 @@ static NSMutableArray* imageReps = NULL;
+ (void) registerImageRepClass: (Class)imageRepClass
{
[imageReps addObject: imageRepClass];
if ([imageReps containsObject: imageRepClass] == NO)
{
Class c = imageRepClass;
while (c != nil && c != [NSObject class] && c != [NSImageRep class])
{
c = [c superclass];
}
if (c != [NSImageRep class])
{
[NSException raise: NSInvalidArgumentException
format: @"Attempt to register non-imagerep class"];
}
[imageReps addObject: imageRepClass];
}
[[NSNotificationCenter defaultCenter]
postNotificationName: NSImageRepRegistryChangedNotification
object: self];

View file

@ -3,11 +3,13 @@
Functions for dealing with tiff images.
Copyright (C) 1996 Free Software Foundation, Inc.
Copyright (C) 1996,1999 Free Software Foundation, Inc.
Author: Adam Fedor <fedor@colorado.edu>
Date: Feb 1996
Support for writing tiffs: Richard Frith-Macdonald
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
@ -73,7 +75,8 @@ typedef struct {
long size;
long position;
const char* mode;
realloc_data_callback* realloc_data;
char **outdata;
long *outposition;
} chandle_t;
/* Client functions that provide reading/writing of data for libtiff */
@ -99,14 +102,17 @@ TiffHandleWrite(thandle_t handle, tdata_t buf, toff_t count)
return 0;
if (chand->position + count > chand->size)
{
if (chand->realloc_data == NULL)
return 0;
chand->size = chand->position + count + 1;
chand->data = chand->realloc_data(chand->data, chand->size);
chand->data = objc_realloc(chand->data, chand->size);
*(chand->outdata) = chand->data;
if (chand->data == NULL)
return 0;
}
memcpy(chand->data + chand->position, buf, count);
chand->position += count;
if (chand->position > *(chand->outposition))
*(chand->outposition) = chand->position;
return count;
}
@ -168,18 +174,38 @@ TiffHandleUnmap(thandle_t handle, tdata_t data, toff_t size)
/* Open a tiff from a stream. Returns NULL if can't read tiff information. */
TIFF*
NSTiffOpenData(char* data, long size, const char* mode,
realloc_data_callback* realloc_data)
NSTiffOpenDataRead(const char* data, long size)
{
chandle_t* handle;
NSDebugLog (@"NSTiffOpenData\n");
OBJC_MALLOC(handle, chandle_t, 1);
handle->data = data;
handle->data = (char*)data;
handle->outdata = 0;
handle->position = 0;
handle->outposition = 0;
handle->size = size;
handle->mode = mode;
handle->realloc_data = realloc_data;
return TIFFClientOpen("NSData", mode,
handle->mode = "r";
return TIFFClientOpen("NSData", "r",
(thandle_t)handle,
TiffHandleRead, TiffHandleWrite,
TiffHandleSeek, TiffHandleClose,
TiffHandleSize,
TiffHandleMap, TiffHandleUnmap);
}
TIFF*
NSTiffOpenDataWrite(char **data, long *size)
{
chandle_t* handle;
NSDebugLog (@"NSTiffOpenData\n");
OBJC_MALLOC(handle, chandle_t, 1);
handle->data = *data;
handle->outdata = data;
handle->position = 0;
handle->outposition = size;
handle->size = *size;
handle->mode = "w";
return TIFFClientOpen("NSData", "w",
(thandle_t)handle,
TiffHandleRead, TiffHandleWrite,
TiffHandleSeek, TiffHandleClose,
@ -214,6 +240,7 @@ NSTiffGetInfo(int imageNumber, TIFF* image)
TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &info->width);
TIFFGetField(image, TIFFTAG_IMAGELENGTH, &info->height);
TIFFGetField(image, TIFFTAG_COMPRESSION, &info->compression);
TIFFGetField(image, TIFFTAG_JPEGQUALITY, &info->quality);
TIFFGetField(image, TIFFTAG_SUBFILETYPE, &info->subfileType);
/* If the following tags aren't present then use the TIFF defaults. */
@ -249,8 +276,8 @@ NSTiffGetInfo(int imageNumber, TIFF* image)
}
#define READ_SCANLINE(sample) \
if ( TIFFReadScanline( image, buf, row, sample ) < 0 ) { \
NSLog(@"tiff: bad data read on line %d\n", row ); \
if (TIFFReadScanline(image, buf, row, sample) < 0) { \
NSLog(@"tiff: bad data read on line %d\n", row); \
error = 1; \
break; \
} \
@ -290,51 +317,51 @@ NSTiffRead(int imageNumber, TIFF* image, NSTiffInfo* info, char* data)
memcpy(info, newinfo, sizeof(NSTiffInfo));
map = NULL;
if ( newinfo->photoInterp == PHOTOMETRIC_PALETTE)
if (newinfo->photoInterp == PHOTOMETRIC_PALETTE)
{
map = NSTiffGetColormap(image);
if (!map)
return -1;
}
maxval = ( 1 << newinfo->bitsPerSample ) - 1;
maxval = (1 << newinfo->bitsPerSample) - 1;
line = ceil((float)newinfo->width * newinfo->bitsPerSample / 8.0);
size = ceil((float)line * newinfo->height * newinfo->samplesPerPixel );
size = ceil((float)line * newinfo->height * newinfo->samplesPerPixel);
scan_line_size = TIFFScanlineSize(image);
OBJC_MALLOC(buf, u_char, TIFFScanlineSize(image));
raster = (u_char *)data;
outP = raster;
switch ( newinfo->photoInterp )
switch (newinfo->photoInterp)
{
case PHOTOMETRIC_MINISBLACK:
case PHOTOMETRIC_MINISWHITE:
if (newinfo->planarConfig == PLANARCONFIG_CONTIG)
{
for ( row = 0; row < newinfo->height; ++row )
for (row = 0; row < newinfo->height; ++row)
{
READ_SCANLINE(0)
for ( col = 0; col < line*newinfo->samplesPerPixel; col++)
for (col = 0; col < line*newinfo->samplesPerPixel; col++)
*outP++ = *inP++;
}
}
else
{
for (i = 0; i < newinfo->samplesPerPixel; i++)
for ( row = 0; row < newinfo->height; ++row )
for (row = 0; row < newinfo->height; ++row)
{
READ_SCANLINE(i)
for ( col = 0; col < line; col++)
for (col = 0; col < line; col++)
*outP++ = *inP++;
}
}
break;
case PHOTOMETRIC_PALETTE:
{
for ( row = 0; row < newinfo->height; ++row )
for (row = 0; row < newinfo->height; ++row)
{
READ_SCANLINE(0)
for ( col = 0; col < newinfo->width; col++)
for (col = 0; col < newinfo->width; col++)
{
*outP++ = map->red[*inP] / 256;
*outP++ = map->green[*inP] / 256;
@ -352,10 +379,10 @@ NSTiffRead(int imageNumber, TIFF* image, NSTiffInfo* info, char* data)
if (newinfo->planarConfig == PLANARCONFIG_CONTIG)
{
NSDebugLog(@"PHOTOMETRIC_RGB: CONTIG\n");
for ( row = 0; row < newinfo->height; ++row )
for (row = 0; row < newinfo->height; ++row)
{
READ_SCANLINE(0)
for ( col = 0; col < newinfo->width; col++)
for (col = 0; col < newinfo->width; col++)
for (i = 0; i < newinfo->samplesPerPixel; i++)
{
*outP++ = *inP++;
@ -366,10 +393,10 @@ NSTiffRead(int imageNumber, TIFF* image, NSTiffInfo* info, char* data)
{
NSDebugLog(@"PHOTOMETRIC_RGB: NOT CONTIG\n");
for (i = 0; i < newinfo->samplesPerPixel; i++)
for ( row = 0; row < newinfo->height; ++row )
for (row = 0; row < newinfo->height; ++row)
{
READ_SCANLINE(i)
for ( col = 0; col < newinfo->width; col++)
for (col = 0; col < newinfo->width; col++)
{
*outP++ = *inP++;
}
@ -386,10 +413,91 @@ NSTiffRead(int imageNumber, TIFF* image, NSTiffInfo* info, char* data)
return error;
}
#define WRITE_SCANLINE(sample) \
if (TIFFWriteScanline(image, buf, row, sample) != 1) { \
NSLog(@"tiff: bad data write on line %d\n", row); \
error = 1; \
break; \
}
int
NSWriteTiff(TIFF* image, NSTiffInfo* info, char* data)
{
return 0;
tdata_t buf = (tdata_t)data;
int i;
int row;
int error = 0;
TIFFSetField(image, TIFFTAG_IMAGEWIDTH, info->width);
TIFFSetField(image, TIFFTAG_IMAGELENGTH, info->height);
TIFFSetField(image, TIFFTAG_COMPRESSION, info->compression);
TIFFSetField(image, TIFFTAG_JPEGQUALITY, info->quality);
TIFFSetField(image, TIFFTAG_SUBFILETYPE, info->subfileType);
TIFFSetField(image, TIFFTAG_BITSPERSAMPLE, info->bitsPerSample);
TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL, info->samplesPerPixel);
TIFFSetField(image, TIFFTAG_PLANARCONFIG, info->planarConfig);
TIFFSetField(image, TIFFTAG_PHOTOMETRIC, info->photoInterp);
switch (info->photoInterp)
{
case PHOTOMETRIC_MINISBLACK:
case PHOTOMETRIC_MINISWHITE:
if (info->planarConfig == PLANARCONFIG_CONTIG)
{
int line = ceil((float)info->width * info->bitsPerSample / 8.0);
for (row = 0; row < info->height; ++row)
{
WRITE_SCANLINE(0)
buf += line;
}
}
else
{
int line = ceil((float)info->width / 8.0);
for (i = 0; i < info->samplesPerPixel; i++)
{
for (row = 0; row < info->height; ++row)
{
WRITE_SCANLINE(i)
buf += line;
}
}
}
break;
case PHOTOMETRIC_RGB:
if (info->planarConfig == PLANARCONFIG_CONTIG)
{
NSDebugLog(@"PHOTOMETRIC_RGB: CONTIG\n");
for (row = 0; row < info->height; ++row)
{
WRITE_SCANLINE(0)
buf += info->width * info->samplesPerPixel;
}
}
else
{
NSDebugLog(@"PHOTOMETRIC_RGB: NOT CONTIG\n");
for (i = 0; i < info->samplesPerPixel; i++)
{
for (row = 0; row < info->height; ++row)
{
WRITE_SCANLINE(i)
buf += info->width;
}
}
}
break;
default:
TIFFError(TIFFFileName(image),
"Can't write photometric %d", info->photoInterp);
break;
}
return error;
}
/*------------------------------------------------------------------------*/