• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kimgio
 

kimgio

  • kimgio
tiffr.cpp
1 // This library is distributed under the conditions of the GNU LGPL.
2 
3 #include "config.h"
4 
5 #ifdef HAVE_LIBTIFF
6 
7 #include <tiffio.h>
8 
9 #include <kdebug.h>
10 #include <tqimage.h>
11 #include <tqfile.h>
12 #include <tdelibs_export.h>
13 
14 #include <assert.h>
15 
16 #if (TIFFLIB_VERSION >= 20210416)
17 #undef uint16
18 #define uint16 uint16_t
19 #undef uint32
20 #define uint32 uint32_t
21 #endif
22 
23 #include "tiffr.h"
24 
25 static tsize_t tiff_read( thandle_t handle, tdata_t buf, tsize_t size )
26 {
27  TQIODevice *dev = reinterpret_cast<TQIODevice *>( handle );
28  return dev->readBlock( reinterpret_cast<char *>( buf ), size );
29 }
30 
31 static tsize_t tiff_write( thandle_t, tdata_t, tsize_t )
32 {
33  return 0;
34 }
35 
36 static toff_t tiff_seek( thandle_t handle, toff_t off, int whence )
37 {
38  TQIODevice *dev = reinterpret_cast<TQIODevice *>( handle );
39 
40  if ( whence == SEEK_CUR )
41  off += dev->at();
42  else if ( whence == SEEK_END )
43  off += dev->size();
44 
45  if ( !dev->at( off ) )
46  return ( toff_t )-1;
47 
48  return dev->at();
49 }
50 
51 static toff_t tiff_size( thandle_t handle )
52 {
53  TQIODevice *dev = reinterpret_cast<TQIODevice *>( handle );
54  return dev->size();
55 }
56 
57 static int tiff_close( thandle_t )
58 {
59  return 0;
60 }
61 
62 static int tiff_map( thandle_t, tdata_t *, toff_t * )
63 {
64  return 0;
65 }
66 
67 static void tiff_unmap( thandle_t, tdata_t, toff_t )
68 {
69 }
70 
71 TDE_EXPORT void kimgio_tiff_read( TQImageIO *io )
72 {
73  TIFF *tiff;
74  uint32 width, height;
75  uint32 *data;
76 
77  uint16 extra_samples_count;
78  uint16 *extra_samples;
79 
80  // FIXME: use qdatastream
81 
82  // open file
83  tiff = TIFFClientOpen( TQFile::encodeName( io->fileName() ), "r",
84  ( thandle_t )io->ioDevice(),
85  tiff_read, tiff_write, tiff_seek, tiff_close,
86  tiff_size, tiff_map, tiff_unmap );
87 
88  if( tiff == 0 ) {
89  return;
90  }
91 
92  // create image with loaded dimensions
93  if ((TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width) != 1) ||
94  (TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height) != 1))
95  {
96  TIFFClose(tiff);
97  return;
98  }
99 
100  TQImage image( width, height, 32 );
101  if( image.isNull()) {
102  TIFFClose( tiff );
103  return;
104  }
105  data = (uint32 *)image.bits();
106 
107  //Sven: changed to %ld for 64bit machines
108  //debug( "unsigned size: %ld, uint32 size: %ld",
109  // (long)sizeof(unsigned), (long)sizeof(uint32) );
110 
111  // read data
112  bool stat =TIFFReadRGBAImage( tiff, width, height, data );
113 
114  if( stat == 0 ) {
115  TIFFClose( tiff );
116  return;
117  }
118 
119  // reverse red and blue
120  for( unsigned i = 0; i < width * height; ++i )
121  {
122  uint32 red = ( 0x00FF0000 & data[i] ) >> 16;
123  uint32 blue = ( 0x000000FF & data[i] ) << 16;
124  data[i] &= 0xFF00FF00;
125  data[i] += red + blue;
126  }
127 
128  // reverse image (it's upside down)
129  for( unsigned ctr = 0; ctr < (height>>1); ) {
130  unsigned *line1 = (unsigned *)image.scanLine( ctr );
131  unsigned *line2 = (unsigned *)image.scanLine( height
132  - ( ++ctr ) );
133 
134  for( unsigned x = 0; x < width; x++ ) {
135  int temp = *line1;
136  *line1 = *line2;
137  *line2 = temp;
138  line1++;
139  line2++;
140  }
141 
142  // swap rows
143  }
144 
145  // Extra Samples
146  if (TIFFGetField(tiff, TIFFTAG_EXTRASAMPLES, &extra_samples_count, &extra_samples) == 1)
147  {
148  kdDebug(399) << "TIFF image has " << extra_samples_count << " extra sample(s)." << endl;
149  for (uint16 i = 0; i < extra_samples_count; i++)
150  {
151  if ((extra_samples[i] == EXTRASAMPLE_ASSOCALPHA) ||
152  (extra_samples[i] == EXTRASAMPLE_UNASSALPHA))
153  {
154  image.setAlphaBuffer(true);
155  break;
156  }
157  }
158  }
159 
160  // set channel order to Qt order
161  // FIXME: Right now they are the same, but will it change?
162 
163 // for( int ctr = (image.numBytes() / sizeof(uint32))+1; ctr ; ctr-- ) {
164 // // TODO: manage alpha with TIFFGetA
165 // *data = tqRgb( TIFFGetR( *data ),
166 // TIFFGetG( *data ), TIFFGetB( *data ) );
167 // data++;
168 // }
169  TIFFClose( tiff );
170 
171  io->setImage( image );
172  io->setStatus ( 0 );
173 }
174 
175 TDE_EXPORT void kimgio_tiff_write( TQImageIO * )
176 {
177  // TODO: stub
178 }
179 
180 #endif

kimgio

Skip menu "kimgio"
  • Main Page
  • File List
  • Related Pages

kimgio

Skip menu "kimgio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kimgio by doxygen 1.9.1
This website is maintained by Timothy Pearson.