Image improvements.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@6481 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
fedor 2000-04-18 16:58:14 +00:00
parent a51fd0ad14
commit b9de76dad5
5 changed files with 103 additions and 16 deletions

View file

@ -1,3 +1,17 @@
2000-04-18 Adam Fedor <fedor@gnu.org>
* 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 <jagapen@whitewater.chem.wisc.edu> 2000-04-17 Jonathan Gapen <jagapen@whitewater.chem.wisc.edu>
* Headers/gnustep/gui/NSColorPanel.h: New ivars for panel objects. * Headers/gnustep/gui/NSColorPanel.h: New ivars for panel objects.

View file

@ -49,6 +49,7 @@ typedef struct {
u_short photoInterp; /* photometric interpretation of bitmap data, */ u_short photoInterp; /* photometric interpretation of bitmap data, */
u_short compression; u_short compression;
u_short extraSamples; /* Alpha */ u_short extraSamples; /* Alpha */
int assocAlpha;
int quality; /* compression quality (for jpeg) 1 to 255 */ int quality; /* compression quality (for jpeg) 1 to 255 */
int numImages; /* number of images in tiff */ int numImages; /* number of images in tiff */
int error; int error;

View file

@ -45,17 +45,17 @@
/* Maximum number of planes */ /* Maximum number of planes */
#define MAX_PLANES 5 #define MAX_PLANES 5
/* Backend protocol - methods that must be implemented by the backend to /* Backend methods (optional) */
complete the class */ @interface NSBitmapImageRep (Backend)
@protocol NXBitmapImageRepBackend + (NSArray *) _wrasterFileTypes;
- (BOOL) draw; - _initFromWrasterFile: (NSString *)filename number: (int)imageNumber;
@end @end
@implementation NSBitmapImageRep @implementation NSBitmapImageRep
/* Given a TIFF image (from the libtiff library), load the image information /* Given a TIFF image (from the libtiff library), load the image information
into our data structure. Reads the specified image. */ into our data structure. Reads the specified image. */
- _initFromImage: (TIFF *)image number: (int)imageNumber - _initFromTIFFImage: (TIFF *)image number: (int)imageNumber
{ {
NSString* space; NSString* space;
NSTiffInfo* info; NSTiffInfo* info;
@ -64,6 +64,7 @@
info = NSTiffGetInfo(imageNumber, image); info = NSTiffGetInfo(imageNumber, image);
if (!info) if (!info)
{ {
RELEASE(self);
NSLog(@"Tiff read invalid TIFF info in directory %d", imageNumber); NSLog(@"Tiff read invalid TIFF info in directory %d", imageNumber);
return nil; return nil;
} }
@ -99,6 +100,7 @@
if (NSTiffRead(image, info, [self bitmapData])) if (NSTiffRead(image, info, [self bitmapData]))
{ {
RELEASE(self);
NSLog(@"Tiff read invalid TIFF image data in directory %d", imageNumber); NSLog(@"Tiff read invalid TIFF image data in directory %d", imageNumber);
return nil; return nil;
} }
@ -106,6 +108,11 @@
return self; return self;
} }
- _initFromWrasterFile: (NSString *)filename number: (int)imageNumber
{
return nil;
}
+ (id) imageRepWithData: (NSData *)tiffData + (id) imageRepWithData: (NSData *)tiffData
{ {
NSArray* array; NSArray* array;
@ -135,7 +142,7 @@
for (i = 0; i < images; i++) for (i = 0; i < images; i++)
{ {
NSBitmapImageRep* imageRep; NSBitmapImageRep* imageRep;
imageRep = [[[self class] alloc] _initFromImage: image number: i]; imageRep = [[[self class] alloc] _initFromTIFFImage: image number: i];
if (imageRep) if (imageRep)
[array addObject: AUTORELEASE(imageRep)]; [array addObject: AUTORELEASE(imageRep)];
} }
@ -144,6 +151,43 @@
return array; 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 /* Loads only the default (first) image from the TIFF image contained in
data. */ data. */
- (id) initWithData: (NSData *)tiffData - (id) initWithData: (NSData *)tiffData
@ -153,10 +197,12 @@
image = NSTiffOpenDataRead((char *)[tiffData bytes], [tiffData length]); image = NSTiffOpenDataRead((char *)[tiffData bytes], [tiffData length]);
if (image == 0) 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); NSTiffClose(image);
return self; return self;
} }
@ -280,9 +326,23 @@
return [[pasteboard types] containsObject: NSTIFFPboardType]; return [[pasteboard types] containsObject: NSTIFFPboardType];
} }
+ (NSArray *) _wrasterFileTypes
{
return nil;
}
+ (NSArray *) imageFileTypes + (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 + (NSArray *) imagePasteboardTypes

View file

@ -82,23 +82,32 @@ static NSMutableArray* imageReps = NULL;
// FIXME: Should this be an exception? Should we even check this? // FIXME: Should this be an exception? Should we even check this?
if (!ext) if (!ext)
{ {
NSLog(@"extension missing from filename - '%@'", filename); NSLog(@"Extension missing from filename - '%@'", filename);
return nil; return nil;
} }
array = [NSMutableArray arrayWithCapacity: 1];
array = nil;
count = [imageReps count]; count = [imageReps count];
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
Class rep = [imageReps objectAtIndex: i]; Class rep = [imageReps objectAtIndex: i];
if ([[rep imageFileTypes] indexOfObject: ext] != NSNotFound) 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:)]) if ([rep respondsToSelector: @selector(imageRepsWithData:)])
[array addObjectsFromArray: [rep imageRepsWithData: data]]; array = [rep imageRepsWithData: data];
else if ([rep respondsToSelector: @selector(imageRepWithData:)]) else if ([rep respondsToSelector: @selector(imageRepWithData:)])
[array addObject: [rep imageRepWithData: data]]; array = [rep imageRepWithData: data];
if ([array count] > 0)
break;
} }
} }
return (NSArray *)array; return (NSArray *)array;

View file

@ -285,8 +285,11 @@ NSTiffGetInfo(int imageNumber, TIFF* image)
TIFFGetField(image, TIFFTAG_JPEGQUALITY, &info->quality); TIFFGetField(image, TIFFTAG_JPEGQUALITY, &info->quality);
TIFFGetField(image, TIFFTAG_SUBFILETYPE, &info->subfileType); TIFFGetField(image, TIFFTAG_SUBFILETYPE, &info->subfileType);
TIFFGetField(image, TIFFTAG_EXTRASAMPLES, &info->extraSamples, &sample_info); TIFFGetField(image, TIFFTAG_EXTRASAMPLES, &info->extraSamples, &sample_info);
if (info->extraSamples == EXTRASAMPLE_ASSOCALPHA && sample_info) info->extraSamples = (info->extraSamples == 1
info->extraSamples = sample_info[0]; && ((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. */ /* If the following tags aren't present then use the TIFF defaults. */
TIFFGetFieldDefaulted(image, TIFFTAG_BITSPERSAMPLE, &info->bitsPerSample); TIFFGetFieldDefaulted(image, TIFFTAG_BITSPERSAMPLE, &info->bitsPerSample);