~ubuntu-branches/ubuntu/natty/qtpfsgui/natty

« back to all changes in this revision

Viewing changes to src/Fileformat/pfstiff.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-01-06 04:39:36 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080106043936-a9u9g7yih3w16ru5
Tags: 1.9.0-1
* New upstream release.
* Replace “COPYING” with “LICENSE” in the NOT_NEEDED variable of
  debian/rules, following upstream's renaming.
* Update debian/links accordingly.
* Delete the matching TODO item since there's no longer needed to have a
  patched (with HTML tags) license file to get a correct display in the
  “License agreement” tab.
* Update the gcc4.3 patch (drop the hunk touching src/Libpfs/pfs.cpp):
   - 20_gcc4.3_includes.
* Add a link from /usr/share/qtpfsgui/html to the HTML documentation
  under /usr/share/doc/qtpfsgui/html since the former is used at runtime
  to display the manual.

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
  //--- image parameters
50
50
  if(!TIFFGetField(tif, TIFFTAG_COMPRESSION, &comp)) // compression type
51
51
    comp = COMPRESSION_NONE;
52
 
  qDebug("compression=%d",comp);
53
52
 
54
53
  // type of photometric data
55
54
  if(!TIFFGetFieldDefaulted(tif, TIFFTAG_PHOTOMETRIC, &phot))
70
69
      TypeOfData = FLOATLOGLUV;
71
70
      break;
72
71
    case PHOTOMETRIC_RGB:
73
 
      qDebug("Photometric data: RGB");
 
72
//       qDebug("Photometric data: RGB");
74
73
     // read extra samples (# of alpha channels)
75
74
      if (TIFFGetField( tif, TIFFTAG_EXTRASAMPLES,
76
75
                        &extra_samples_per_pixel, &extra_sample_types )!=1)
79
78
      }
80
79
      TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &nSamples);
81
80
      bps = nSamples - extra_samples_per_pixel;
 
81
      has_alpha=(extra_samples_per_pixel==1);
 
82
//       qDebug("nSamples=%d extra_samples_per_pixel=%d",nSamples,extra_samples_per_pixel);
 
83
//       qDebug("has alpha? %s", has_alpha ? "true" : "false");
82
84
      if (bps!=3)
83
85
      {
 
86
        qDebug("TIFF: unsupported samples per pixel for RGB");
84
87
        TIFFClose(tif);
85
88
        throw pfs::Exception("TIFF: unsupported samples per pixel for RGB");
86
89
      }
87
90
      if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bps) || (bps!=8 && bps!=16 && bps!=32))
88
91
      {
 
92
        qDebug("TIFF: unsupported bits per sample for RGB");
89
93
        TIFFClose(tif);
90
94
        throw pfs::Exception("TIFF: unsupported bits per sample for RGB");
91
95
      }
186
190
//given for granted that users of this function call it only after checking that TypeOfData==BYTE
187
191
QImage* TiffReader::readIntoQImage() {
188
192
        uchar *data=new uchar[width*height*4]; //this will contain the image data: data must be 32-bit aligned, in Format: 0xffRRGGBB
189
 
//      qDebug("pfstiff, data=%ld",data);
 
193
//      qDebug("pfstiff, w=%d h=%d",width,height);
190
194
        assert(TypeOfData==BYTE);
191
195
 
192
196
        //--- image length
205
209
                                *(data + 0 + (y*width+x)*4) = bp[x*nSamples+2] ;
206
210
                                *(data + 1 + (y*width+x)*4) = bp[x*nSamples+1] ;
207
211
                                *(data + 2 + (y*width+x)*4) = bp[x*nSamples] ;
208
 
                                *(data + 3 + (y*width+x)*4) = bp[x*nSamples+3];
 
212
                                if (has_alpha)
 
213
                                        *(data + 3 + (y*width+x)*4) = bp[x*nSamples+3];
 
214
                                else
 
215
                                        *(data + 3 + (y*width+x)*4) = 0xff;
209
216
                        } else {
210
217
                                *(data + 3 + (y*width+x)*4) = bp[x*nSamples+2];
211
218
                                *(data + 2 + (y*width+x)*4) = bp[x*nSamples+1];
212
219
                                *(data + 1 + (y*width+x)*4) = bp[x*nSamples];
213
 
                                *(data + 0 + (y*width+x)*4) = bp[x*nSamples+3];
 
220
                                if (has_alpha)
 
221
                                        *(data + 0 + (y*width+x)*4) = bp[x*nSamples+3];
 
222
                                else
 
223
                                        *(data + 0 + (y*width+x)*4) = 0xff;
214
224
                        }
215
225
                }
216
226
        }
236
246
//      qDebug("width=%d, heigh=%d",width,height);
237
247
        tif = TIFFOpen(filename, "w");
238
248
        if( !tif )
239
 
                throw pfs::Exception("TIFF: could not open file for reading.");
 
249
                throw pfs::Exception("TIFF: could not open file for writing.");
240
250
 
241
251
        TIFFSetField (tif, TIFFTAG_IMAGEWIDTH, width);
242
252
        TIFFSetField (tif, TIFFTAG_IMAGELENGTH, height);
245
255
        TIFFSetField (tif, TIFFTAG_ROWSPERSTRIP, 1);
246
256
}
247
257
 
 
258
TiffWriter::TiffWriter( const char* filename, QImage *f ) : tif((TIFF *)NULL) {
 
259
        ldrimage=f;
 
260
        width=f->width();
 
261
        height=f->height();
 
262
        tif = TIFFOpen(filename, "w");
 
263
        if( !tif )
 
264
                throw pfs::Exception("TIFF: could not open file for writing.");
 
265
        TIFFSetField (tif, TIFFTAG_IMAGEWIDTH, f->width());
 
266
        TIFFSetField (tif, TIFFTAG_IMAGELENGTH, f->height());
 
267
        TIFFSetField (tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
 
268
        TIFFSetField (tif, TIFFTAG_EXTRASAMPLES, EXTRASAMPLE_ASSOCALPHA);
 
269
        TIFFSetField (tif, TIFFTAG_SAMPLESPERPIXEL, 4);
 
270
        TIFFSetField (tif, TIFFTAG_ROWSPERSTRIP, 1);
 
271
}
 
272
 
248
273
int TiffWriter::writeFloatTiff() { //write 32 bit float Tiff from pfs::Frame
249
274
        TIFFSetField (tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); // TODO what about others?
250
275
        TIFFSetField (tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
302
327
        TIFFClose(tif);
303
328
        return 0;
304
329
}
 
330
 
 
331
int TiffWriter::write8bitTiff() {
 
332
        TIFFSetField (tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE); // TODO what about others?
 
333
        TIFFSetField (tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
 
334
        TIFFSetField (tif, TIFFTAG_BITSPERSAMPLE, 8);
 
335
 
 
336
        tsize_t strip_size = TIFFStripSize (tif);
 
337
        tstrip_t strips_num = TIFFNumberOfStrips (tif);
 
338
 
 
339
        char* strip_buf=(char*)_TIFFmalloc(strip_size); //enough space for a strip
 
340
        if (!strip_buf)
 
341
                throw pfs::Exception("TIFF: error allocating buffer.");
 
342
 
 
343
        for (unsigned int s=0; s<strips_num; s++) {
 
344
                for (unsigned int col=0; col<width; col++) {
 
345
                        strip_buf[4*col+0]=qRed  (*( (QRgb*)( ldrimage->bits() ) + width*s + col ));
 
346
                        strip_buf[4*col+1]=qGreen(*( (QRgb*)( ldrimage->bits() ) + width*s + col ));
 
347
                        strip_buf[4*col+2]=qBlue (*( (QRgb*)( ldrimage->bits() ) + width*s + col ));
 
348
                        strip_buf[4*col+3]=qAlpha(*( (QRgb*)( ldrimage->bits() ) + width*s + col ));
 
349
                }
 
350
                if (TIFFWriteEncodedStrip (tif, s, strip_buf, strip_size) == 0) {
 
351
                        qDebug("error writing strip");
 
352
                        return -1;
 
353
                }
 
354
        }
 
355
        _TIFFfree(strip_buf);
 
356
        TIFFClose(tif);
 
357
        return 0;
 
358
}