From b9de76dad59bd9f604c521e00b0ae129ef4b5714 Mon Sep 17 00:00:00 2001 From: fedor Date: Tue, 18 Apr 2000 16:58:14 +0000 Subject: [PATCH] Image improvements. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@6481 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 14 ++++++ Headers/gnustep/gui/nsimage-tiff.h | 1 + Source/NSBitmapImageRep.m | 78 ++++++++++++++++++++++++++---- Source/NSImageRep.m | 19 ++++++-- Source/tiff.m | 7 ++- 5 files changed, 103 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index dd089913b..4c0ee174c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2000-04-18 Adam Fedor + + * Hooks for using backend libraries to read images. + * Source/NSBitmapImageRep.m (-_initFromWrasterFile:number:): New stub + for backends. + (+imageRepsWithFile:): New method. + (+_wrasterFileTypes): New stub for backend. + (+imageFileTypes): Include wraster file types. + * Source/NSImageRep.m (+imageRepsWithContentsOfFile:): Check/use if + rep implements +imageRepsWithFile. + + * Headers/gnustep/gui/nsimage-tiff.h: Add field for associated alpha. + * Source/tiff.m: Set it. + 2000-04-17 Jonathan Gapen * Headers/gnustep/gui/NSColorPanel.h: New ivars for panel objects. diff --git a/Headers/gnustep/gui/nsimage-tiff.h b/Headers/gnustep/gui/nsimage-tiff.h index 924a90af9..9d629ffba 100644 --- a/Headers/gnustep/gui/nsimage-tiff.h +++ b/Headers/gnustep/gui/nsimage-tiff.h @@ -49,6 +49,7 @@ typedef struct { u_short photoInterp; /* photometric interpretation of bitmap data, */ u_short compression; u_short extraSamples; /* Alpha */ + int assocAlpha; int quality; /* compression quality (for jpeg) 1 to 255 */ int numImages; /* number of images in tiff */ int error; diff --git a/Source/NSBitmapImageRep.m b/Source/NSBitmapImageRep.m index c5b33819c..bb70d8d59 100644 --- a/Source/NSBitmapImageRep.m +++ b/Source/NSBitmapImageRep.m @@ -45,17 +45,17 @@ /* Maximum number of planes */ #define MAX_PLANES 5 -/* Backend protocol - methods that must be implemented by the backend to - complete the class */ -@protocol NXBitmapImageRepBackend -- (BOOL) draw; +/* Backend methods (optional) */ +@interface NSBitmapImageRep (Backend) ++ (NSArray *) _wrasterFileTypes; +- _initFromWrasterFile: (NSString *)filename number: (int)imageNumber; @end @implementation NSBitmapImageRep /* Given a TIFF image (from the libtiff library), load the image information into our data structure. Reads the specified image. */ -- _initFromImage: (TIFF *)image number: (int)imageNumber +- _initFromTIFFImage: (TIFF *)image number: (int)imageNumber { NSString* space; NSTiffInfo* info; @@ -64,6 +64,7 @@ info = NSTiffGetInfo(imageNumber, image); if (!info) { + RELEASE(self); NSLog(@"Tiff read invalid TIFF info in directory %d", imageNumber); return nil; } @@ -99,6 +100,7 @@ if (NSTiffRead(image, info, [self bitmapData])) { + RELEASE(self); NSLog(@"Tiff read invalid TIFF image data in directory %d", imageNumber); return nil; } @@ -106,6 +108,11 @@ return self; } +- _initFromWrasterFile: (NSString *)filename number: (int)imageNumber +{ + return nil; +} + + (id) imageRepWithData: (NSData *)tiffData { NSArray* array; @@ -135,7 +142,7 @@ for (i = 0; i < images; i++) { NSBitmapImageRep* imageRep; - imageRep = [[[self class] alloc] _initFromImage: image number: i]; + imageRep = [[[self class] alloc] _initFromTIFFImage: image number: i]; if (imageRep) [array addObject: AUTORELEASE(imageRep)]; } @@ -144,6 +151,43 @@ return array; } +/* A special method used mostly when we have the wraster library in the + backend, which can read several more image formats */ ++ (NSArray*) imageRepsWithFile: (NSString *)filename +{ + NSString *ext; + int images; + NSMutableArray *array; + NSBitmapImageRep* imageRep; + + /* Don't use this for TIFF images, use the regular ...Data methods */ + ext = [filename pathExtension]; + if (!ext) + { + NSLog(@"Extension missing from filename - '%@'", filename); + return nil; + } + if ([[self imageUnfilteredFileTypes] indexOfObject: ext] != NSNotFound) + { + NSData* data = [NSData dataWithContentsOfFile: filename]; + return [self imageRepsWithData: data]; + } + + array = [NSMutableArray arrayWithCapacity: 2]; + images = 0; + do + { + imageRep = [[[self class] alloc] _initFromWrasterFile: filename + number: images]; + if (imageRep) + [array addObject: AUTORELEASE(imageRep)]; + images++; + } + while (imageRep); + + return array; +} + /* Loads only the default (first) image from the TIFF image contained in data. */ - (id) initWithData: (NSData *)tiffData @@ -153,10 +197,12 @@ image = NSTiffOpenDataRead((char *)[tiffData bytes], [tiffData length]); if (image == 0) { - [NSException raise:NSTIFFException format: @"Read invalid TIFF data"]; + RELEASE(self); + NSLog(@"Tiff read invalid TIFF info from data"); + return nil; } - [self _initFromImage:image number: -1]; + [self _initFromTIFFImage:image number: -1]; NSTiffClose(image); return self; } @@ -280,9 +326,23 @@ return [[pasteboard types] containsObject: NSTIFFPboardType]; } ++ (NSArray *) _wrasterFileTypes +{ + return nil; +} + + (NSArray *) imageFileTypes { - return [self imageUnfilteredFileTypes]; + NSArray *wtypes = [self _wrasterFileTypes]; + if (wtypes) + { + wtypes = AUTORELEASE([wtypes mutableCopy]); + [(NSMutableArray *)wtypes addObjectsFromArray: + [self imageUnfilteredFileTypes]]; + } + else + wtypes = [self imageUnfilteredFileTypes]; + return wtypes; } + (NSArray *) imagePasteboardTypes diff --git a/Source/NSImageRep.m b/Source/NSImageRep.m index 9d6f6ac1a..09c92e273 100644 --- a/Source/NSImageRep.m +++ b/Source/NSImageRep.m @@ -82,23 +82,32 @@ static NSMutableArray* imageReps = NULL; // FIXME: Should this be an exception? Should we even check this? if (!ext) { - NSLog(@"extension missing from filename - '%@'", filename); + NSLog(@"Extension missing from filename - '%@'", filename); return nil; } - array = [NSMutableArray arrayWithCapacity: 1]; + array = nil; count = [imageReps count]; for (i = 0; i < count; i++) { Class rep = [imageReps objectAtIndex: i]; if ([[rep imageFileTypes] indexOfObject: ext] != NSNotFound) { - NSData* data = [NSData dataWithContentsOfFile: filename]; + NSData* data; + + if ([rep respondsToSelector: @selector(imageRepsWithFile:)]) + array = [rep imageRepsWithFile: filename]; + if ([array count] > 0) + break; + + data = [NSData dataWithContentsOfFile: filename]; if ([rep respondsToSelector: @selector(imageRepsWithData:)]) - [array addObjectsFromArray: [rep imageRepsWithData: data]]; + array = [rep imageRepsWithData: data]; else if ([rep respondsToSelector: @selector(imageRepWithData:)]) - [array addObject: [rep imageRepWithData: data]]; + array = [rep imageRepWithData: data]; + if ([array count] > 0) + break; } } return (NSArray *)array; diff --git a/Source/tiff.m b/Source/tiff.m index f8e331410..b86caf665 100644 --- a/Source/tiff.m +++ b/Source/tiff.m @@ -285,8 +285,11 @@ NSTiffGetInfo(int imageNumber, TIFF* image) TIFFGetField(image, TIFFTAG_JPEGQUALITY, &info->quality); TIFFGetField(image, TIFFTAG_SUBFILETYPE, &info->subfileType); TIFFGetField(image, TIFFTAG_EXTRASAMPLES, &info->extraSamples, &sample_info); - if (info->extraSamples == EXTRASAMPLE_ASSOCALPHA && sample_info) - info->extraSamples = sample_info[0]; + info->extraSamples = (info->extraSamples == 1 + && ((sample_info[0] == EXTRASAMPLE_ASSOCALPHA) + || (sample_info[0] == EXTRASAMPLE_UNASSALPHA))); + info->assocAlpha = (info->extraSamples == 1 + && sample_info[0] == EXTRASAMPLE_ASSOCALPHA); /* If the following tags aren't present then use the TIFF defaults. */ TIFFGetFieldDefaulted(image, TIFFTAG_BITSPERSAMPLE, &info->bitsPerSample);