[8b9751]: tiff.c Maximize Restore History

Download this file

tiff.c    300 lines (243 with data), 7.9 kB

  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
/* Panorama_Tools - Generate, Edit and Convert Panoramic Images
Copyright (C) 1998,1999 - Helmut Dersch der@fh-furtwangen.de
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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
/*------------------------------------------------------------*/
// Functions to read tiff format
#include "filter.h"
#include "tiffio.h"
#include "ZComb.h"
int readplanarTIFF(Image *im, TIFF* tif);
void RGBAtoARGB(UCHAR* buf, int width, int bitsPerPixel);
void ARGBtoRGBA(UCHAR* buf, int width, int bitsPerPixel);
int readtif(Image *im, TIFF* tif);
// image is allocated, but not image data
int readtif(Image *im, TIFF* tif){
short BitsPerSample, tPhotoMetric, config;
long w, h;
unsigned long **hdl_raster;
if(tif == NULL || im == NULL) return -1;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &BitsPerSample);
TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tPhotoMetric);
TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config);
SetImageDefaults( im );
im->width = w;
im->height = h;
im->bitsPerPixel = 32 * BitsPerSample/8;
im->bytesPerLine = im->width * im->bitsPerPixel/8;
im->dataSize = im->bytesPerLine * im->height;
hdl_raster = (unsigned long**) mymalloc(im->dataSize);
if( hdl_raster == NULL){
PrintError("Not enough memory");
return -1;
}
im->data = (UCHAR**)hdl_raster;
if( tPhotoMetric == PHOTOMETRIC_RGB && config == PLANARCONFIG_CONTIG){
return readplanarTIFF(im, tif);
}
if (TIFFReadRGBAImage(tif, w, h, (void*)*(im->data), 0)) {
// Convert agbr to argb; flip image vertically
unsigned char *cline, *ct, *cb;
int h2 = im->height/2, y;
cline = (unsigned char*)malloc(im->bytesPerLine);
if(cline==NULL){
PrintError("Not enough memory");
return -1;
}
ct = *im->data;
cb = *im->data + (im->height-1)*im->bytesPerLine;
for(y=0; y<h2; y++,ct+=im->bytesPerLine,cb-=im->bytesPerLine){
RGBAtoARGB(ct, im->width, im->bitsPerPixel);
RGBAtoARGB(cb, im->width, im->bitsPerPixel);
memcpy(cline,ct,im->bytesPerLine);
memcpy(ct,cb,im->bytesPerLine);
memcpy(cb,cline,im->bytesPerLine);
}
if(im->height != 2*h2){ // odd number of scanlines
RGBAtoARGB(*im->data+y*im->bytesPerLine, im->width, im->bitsPerPixel);
}
if(cline) free(cline);
}else{
PrintError("Could not read tiff-data");
return -1;
}
return 0;
}
int readTIFF(Image *im, fullPath *sfile){
char filename[512];
TIFF* tif;
int result = 0;
#ifdef __Mac__
unsigned char the_pcUnixFilePath[512];//added by Kekus Digital
Str255 the_cString;
Boolean the_bReturnValue;
CFStringRef the_FilePath;
CFURLRef the_Url;//till here
#endif
if(FullPathtoString( sfile, filename )){
PrintError("Could not get filename");
return -1;
}
#ifdef __Mac__
CopyCStringToPascal(filename,the_cString);//Added by Kekus Digital
the_FilePath = CFStringCreateWithPascalString(kCFAllocatorDefault, the_cString, kCFStringEncodingUTF8);
the_Url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, the_FilePath, kCFURLHFSPathStyle, false);
the_bReturnValue = CFURLGetFileSystemRepresentation(the_Url, true, the_pcUnixFilePath, 512);
strcpy(filename, the_pcUnixFilePath);//till here
#endif
tif = TIFFOpen(filename, "r");
if ( !tif){
PrintError("Could not open tiff-file");
return -1;
}
result = readtif(im, tif);
TIFFClose(tif);
return result;
}
int readplanarTIFF(Image *im, TIFF* tif){
UCHAR* buf;
int y;
short SamplesPerPixel;
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &SamplesPerPixel);
if( SamplesPerPixel > 4 ) return -1;
if( SamplesPerPixel == 3 ){
im->bitsPerPixel = im->bitsPerPixel * 3 / 4;
im->bytesPerLine = im->bytesPerLine * 3 / 4;
}
buf = (UCHAR*)malloc(TIFFScanlineSize(tif));
if( buf == NULL ){
PrintError("Not enough memory");
return -1;
}
for (y = 0; y < im->height; y++){
TIFFReadScanline(tif, buf, y, 0);
RGBAtoARGB( buf, im->width, im->bitsPerPixel);
memcpy( *(im->data) + y*im->bytesPerLine, buf, im->bytesPerLine );
}
free( buf );
ThreeToFourBPP( im );
return 0;
}
int writeTIFF(Image *im, fullPath *sfile){
char string[512];
TIFF *tif;
UCHAR* buf;
int bufsize,y;
#ifdef __Mac__
unsigned char the_pcUnixFilePath[512];//added by Kekus Digital
Str255 the_cString;
Boolean the_bReturnValue;
CFStringRef the_FilePath;
CFURLRef the_Url;//till here
#endif
if(FullPathtoString( sfile, string )){
PrintError("Could not get filename");
return -1;
}
#ifdef __Mac__
CopyCStringToPascal(string,the_cString);//added by Kekus Digital
the_FilePath = CFStringCreateWithPascalString(kCFAllocatorDefault, the_cString, kCFStringEncodingUTF8);
the_Url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, the_FilePath, kCFURLHFSPathStyle, false);
the_bReturnValue = CFURLGetFileSystemRepresentation(the_Url, true, the_pcUnixFilePath, 512);
strcpy(string, the_pcUnixFilePath);//till here
#endif
tif = TIFFOpen(string, "w");
if( !tif ){
PrintError("Could not create TIFF-file");
return -1;
}
// Rik's mask-from-focus hacking
if (ZCombSeeImage(im,string)) {
PrintError("failed ZCombSeeImage");
}
// end Rik's mask-from-focus hacking (for the moment...)
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, im->width);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, im->height);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, im->bitsPerPixel < 48 ? 8 : 16 );
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, im->bitsPerPixel == 24 || im->bitsPerPixel == 48 ? 3 : 4);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS );
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT );
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, im->height );
bufsize = TIFFScanlineSize(tif);
if(bufsize < im->bytesPerLine) bufsize = im->bytesPerLine;
buf = (UCHAR*)malloc(bufsize);
if( buf == NULL ){
PrintError("Not enough memory");
return -1;
}
for (y = 0; y < im->height; y++){
memcpy( buf, *(im->data) + y*im->bytesPerLine, im->bytesPerLine );
ARGBtoRGBA( buf, im->width, im->bitsPerPixel);
TIFFWriteScanline(tif, buf, y, 1);
}
free( buf );
TIFFClose( tif );
return 0;
}
void RGBAtoARGB(UCHAR* buf, int width, int bitsPerPixel){
int x;
switch(bitsPerPixel){
case 32: {
UCHAR pix;
for(x=0; x<width; x++,buf+=4){
pix = buf[3];
buf[3] = buf[2];
buf[2] = buf[1];
buf[1] = buf[0];
buf[0] = pix;
}
}
break;
case 64: {
USHORT *bufs=(USHORT*)buf, pix;
for(x=0; x<width; x++,bufs+=4){
pix = bufs[3];
bufs[3] = bufs[2];
bufs[2] = bufs[1];
bufs[1] = bufs[0];
bufs[0] = pix;
}
}
break;
}
}
void ARGBtoRGBA(UCHAR* buf, int width, int bitsPerPixel){
int x;
switch(bitsPerPixel){
case 32:{
UCHAR pix;
for(x=0; x<width; x++, buf+=4){
pix = buf[0];
buf[0] = buf[1];
buf[1] = buf[2];
buf[2] = buf[3];
buf[3] = pix;
}
}
break;
case 64:{
USHORT *bufs = (USHORT*)buf, pix;
for(x=0; x<width; x++, bufs+=4){
pix = bufs[0];
bufs[0] = bufs[1];
bufs[1] = bufs[2];
bufs[2] = bufs[3];
bufs[3] = pix;
}
}
break;
}
}