1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
//
// Lynkeos
// $Id$
//
// Created by Jean-Etienne LAMIAUD on Tue Mar 29 2005.
// Copyright (c) 2005-2023. Jean-Etienne LAMIAUD
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
#import <AppKit/NSGraphics.h>
#include <LynkeosCore/LynkeosImageBuffer.h>
#include "processing_core.h"
#include "MyTiff16Reader.h"
@implementation MyTiff16Reader
+ (void) load
{
// Nothing to do, this is just to force the runtime to load this class
}
+ (void) lynkeosFileTypes:(NSArray**)fileTypes
{
*fileTypes = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],@"tif",
[NSNumber numberWithInt:1],@"tiff",
nil];
}
- (id) init
{
self = [super init];
if ( self != nil )
{
_tiffFile = NULL;
_min = HUGE_VAL;
_max = -HUGE_VAL;
}
return( self );
}
- (id) initWithURL:(NSURL*)url
{
self = [self init];
if ( self != nil )
{
const char *filePath = [[url path] fileSystemRepresentation];
// Is this file a TIFF image ?
TIFF *tiff = TIFFOpen( filePath, "r" );
if ( tiff == NULL )
{
[self release];
self = nil;
}
else
{
TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &_width);
TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &_height);
TIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &_stripH);
TIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &_nBits);
TIFFGetField(tiff, TIFFTAG_SAMPLEFORMAT, &_sampleType);
TIFFGetField(tiff, TIFFTAG_PLANARCONFIG, &_planar);
TIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &_nPlanes );
if ( (_nPlanes == 1 || _nPlanes == 3 || _nPlanes == 4) /* Monochrome or RGB(A) image */
&& ( ( (_sampleType == 0 || _sampleType == SAMPLEFORMAT_UINT)
&& (_nBits == 16 || _nBits == 8)) /* Only 8, 16 uint */
|| ( _sampleType == SAMPLEFORMAT_IEEEFP
&& _nBits == 32) ) /* Or 32 float */
&& _stripH != 0 ) /* Organized as strips */
{
_tiffFile = (char*)malloc( strlen(filePath) + 1 );
strcpy( _tiffFile, filePath );
}
else
{
/* Not an image worth of us !... */
[self release];
self = nil;
}
TIFFClose( tiff );
}
}
return( self );
}
- (void) dealloc
{
if ( _tiffFile != NULL )
free( _tiffFile );
[super dealloc];
}
- (void) imageWidth:(u_short*)w height:(u_short*)h
{
*w = _width;
*h = _height;
}
- (u_short) numberOfPlanes
{
return( _nPlanes == 4 ? 3 : _nPlanes );
}
- (void) getMinLevel:(double*)vmin maxLevel:(double*)vmax
{
if ( _min > _max )
{
// for IEEE_FP images, provide the real min and max
if ( _sampleType == SAMPLEFORMAT_IEEEFP )
{
const u_short imagePlanes = [self numberOfPlanes];
LynkeosImageBuffer *buf =
[LynkeosImageBuffer imageBufferWithNumberOfPlanes: imagePlanes
width:_width
height:_height];
[self getImageSample:[buf colorPlanes]
withPlanes:imagePlanes atX:0 Y:0 W:_width H:_height
lineWidth:buf->_padw];
[buf getMinLevel:&_min maxLevel:&_max];
}
else
{
_min = 0.0;
_max = 255.0;
}
}
*vmin = _min;
*vmax = _max;
}
- (NSImage*) getNSImage
{
NSImage *image = nil;
NSBitmapImageRep* bitmap;
TIFF *tiff = TIFFOpen( _tiffFile, "r" );
u_long i;
NSAssert( tiff != NULL, @"Unable to open the TIFF file" );
if ( _sampleType != SAMPLEFORMAT_IEEEFP )
{
// Create a RGBA bitmap
bitmap = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:_width
pixelsHigh:_height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:_width*4
bitsPerPixel:32] autorelease];
NSAssert( bitmap != nil, @"Failed to create a bitmap for a TIFF image" );
uint32_t *pixels = (uint32_t*)[bitmap bitmapData];
TIFFReadRGBAImageOriented( tiff, _width, _height, pixels,
ORIENTATION_TOPLEFT, 0 );
// Unfortunately libtiff put it in little endian order
for( i = 0; i < _width*_height; i++ )
pixels[i] = NSSwapLittleIntToHost(pixels[i]);
}
// TIFFReadRGBAImageOriented is unable to read IEEE fp images
else
{
const u_short imagePlanes = [self numberOfPlanes];
double tmin[imagePlanes+1], tmax[imagePlanes+1], tgamma[imagePlanes+1];
LynkeosImageBuffer *buf =
[LynkeosImageBuffer imageBufferWithNumberOfPlanes:imagePlanes
width:_width
height:_height];
[self getImageSample:[buf colorPlanes]
withPlanes:imagePlanes atX:0 Y:0 W:_width H:_height
lineWidth:buf->_padw];
if ( _min > _max )
[buf getMinLevel:&_min maxLevel:&_max];
for( i = 0; i <= imagePlanes; i++ )
{
tmin[i] = _min;
tmax[i] = _max;
tgamma[i] = 1.0;
}
#if !GNUSTEP
CGImageRef img = [buf getImageInRect:LynkeosMakeIntegerRect(0, 0, _width, _height)
withBlack:tmin white:tmax gamma:tgamma];
bitmap = [[[NSBitmapImageRep alloc] initWithCGImage: img] autorelease];
CGImageRelease(img);
#else
bitmap = [buf getNSImageWithBlack:tmin white:tmax gamma:tgamma];
#endif
}
TIFFClose( tiff );
image = [[[NSImage alloc] initWithSize:NSMakeSize(_width,_height)] autorelease];
if ( image != nil )
[image addRepresentation:bitmap];
return( image );
}
/* Macro to insert the monochromatic conversion with the required precision */
#define PLANAR_MONOCHROMATIC_CONVERSION(floating_type) \
{ \
floating_type *s = &((floating_type*)sample[0])[(ys-y)*lineW+xs-x]; \
if ( cs == 0 ) \
*s = v; \
else \
*s += v; \
if ( cs == imagePlanes-1 ) \
*s /= (float)imagePlanes; \
}
/*! Pixels values are scaled to remain with a 256 maximum while retaining
* 16 bits precision (because they are floating precision numbers) when
* applicable
*/
- (void) getImageSample:(REAL * const * const)sample
withPlanes:(u_short)nPlanes
atX:(short)x Y:(short)y W:(u_short)w H:(u_short)h
lineWidth:(u_short)lineW
{
const u_short imagePlanes = [self numberOfPlanes];
TIFF *tiff = TIFFOpen( _tiffFile, "r" );
BOOL monoConversion = (nPlanes == 1 && imagePlanes != 1 );
tdata_t buf;
u_short xs, ys, cs, ystr;
NSAssert( tiff != NULL, @"Unable to open the TIFF file" );
NSAssert2( nPlanes == imagePlanes || nPlanes == 1,
@"Illegal transfer from %d planes to %d planes",
imagePlanes, nPlanes );
NSAssert( x+w <= (short)_width && y+h <= (short)_height,
@"Sample at least partly outside the image" );
buf = _TIFFmalloc(TIFFStripSize(tiff));
NSAssert( buf != NULL, @"Unable to allocate a strip buffer" );
if (_planar == PLANARCONFIG_SEPARATE)
{
/* We go through all our planes, if the caller wants monochrome data,
* we will accumulate our planes in the output buffer */
for( cs = 0; cs < imagePlanes; cs++ )
{
for (ys = y, ystr = -1; ys < y+h; ys++)
{
uint32_t strip = (ys+cs*_height)/_stripH,
yoffset = (ys+cs*_height)%_stripH;
if ( strip != ystr )
{
/* Start of a new strip */
ystr = strip;
TIFFReadEncodedStrip(tiff, strip, buf, (tsize_t) -1);
}
for( xs = x; xs < x+w; xs++ )
{
float v = 0;
switch ( _nBits )
{
case 8 :
v = (float)((u_char*)buf)[xs+yoffset*_width];
break;
case 16 :
v = (float)((u_short*)buf)[xs+yoffset*_width] / 256.0;
break;
case 32 :
v = ((float*)buf)[xs+yoffset*_width];
break;
default:
NSAssert( NO, @"Inconsistent sample size" );
}
if ( monoConversion )
{
#if PROCESSING_PRECISION == SINGLE_PRECISION
PLANAR_MONOCHROMATIC_CONVERSION(float)
#else
PLANAR_MONOCHROMATIC_CONVERSION(double)
#endif
}
else
SET_SAMPLE(sample[cs], xs-x, ys-y, lineW, v);
}
}
}
}
else
{
for (ys = y, ystr = -1; ys < y+h; ys++)
{
uint32_t strip = ys/_stripH,
yoffset = ys%_stripH;
if ( strip != ystr )
{
/* Start of a new strip */
ystr = strip;
TIFFReadEncodedStrip(tiff, strip, buf, (tsize_t) -1);
}
for( xs = x; xs < x+w; xs++ )
{
u_long i = yoffset*_width*_nPlanes+xs*_nPlanes;
if ( monoConversion )
{
float v = 0;
for( cs = 0; cs < imagePlanes; cs++ )
{
switch ( _nBits )
{
case 8 :
v += (float)((u_char*)buf)[i+cs];
break;
case 16 :
v += (float)((u_short*)buf)[i+cs] / 256.0;
break;
case 32 :
v += ((float*)buf)[i+cs];
break;
default:
NSAssert( NO, @"Inconsistent sample size" );
}
}
SET_SAMPLE(sample[0], xs-x, ys-y, lineW, v/(float)imagePlanes);
}
else
{
for( cs = 0; cs < imagePlanes; cs++ )
{
switch ( _nBits )
{
case 8 :
SET_SAMPLE(sample[cs], xs-x, ys-y, lineW, ((u_char*)buf)[i+cs]);
break;
case 16 :
SET_SAMPLE(sample[cs], xs-x, ys-y, lineW, ((u_short*)buf)[i+cs]/256.0);
break;
case 32 :
SET_SAMPLE(sample[cs], xs-x, ys-y, lineW, ((float*)buf)[i+cs]);
break;
default:
NSAssert( NO, @"Inconsistent sample size" );
}
}
}
}
}
}
_TIFFfree(buf);
TIFFClose( tiff );
}
- (NSDictionary*) getMetaData
{
return( nil );
}
@end
|