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

kimgio

  • kimgio
webp.cpp
1 // WebP read support
2 // © 2024 Alexander Hajnal
3 // Based loosely on jp2.cpp
4 //
5 // If implementing write support it's suggested to use lossless mode with exact
6 // mode enabled when the quality setting is 100 and for other qualities to use
7 // lossy mode with the default settings.
8 //
9 // This library is distributed under the conditions of the GNU LGPL.
10 #include "config.h"
11 
12 #include <tdetempfile.h>
13 #include <tqfile.h>
14 #include <tqimage.h>
15 
16 #include <webp/decode.h>
17 #include <cstdlib>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 TDE_EXPORT void kimgio_webp_read( TQImageIO* io )
24 {
25  int width, height;
26  FILE* in;
27 
28  // === Read the source file ===
29  // Based on code in jp2.cpp
30 
31  // for QIODevice's other than TQFile, a temp. file is used.
32  KTempFile* tempf = 0;
33 
34  TQFile* qf = 0;
35  if( ( qf = dynamic_cast<TQFile*>( io->ioDevice() ) ) ) {
36  // great, it's a TQFile. Let's just take the filename.
37  in = fopen( TQFile::encodeName( qf->name() ), "rb" );
38  } else {
39  // not a TQFile. Copy the whole data to a temp. file.
40  tempf = new KTempFile();
41  if( tempf->status() != 0 ) {
42  delete tempf;
43  return;
44  } // if
45  tempf->setAutoDelete( true );
46  TQFile* out = tempf->file();
47  // 4096 (=4k) is a common page size.
48  TQByteArray b( 4096 );
49  TQ_LONG size;
50  // 0 or -1 is EOF / error
51  while( ( size = io->ioDevice()->readBlock( b.data(), 4096 ) ) > 0 ) {
52  // in case of a write error, still give the decoder a try
53  if( ( out->writeBlock( b.data(), size ) ) == -1 ) break;
54  } // while
55  // flush everything out to disk
56  out->flush();
57 
58  in = fopen( TQFile::encodeName( tempf->name() ), "rb" );
59  } // else
60  if( ! in ) {
61  delete tempf;
62  return;
63  } // if
64 
65  // File is now open
66 
67  // === Load compressed data ===
68 
69  // Find file's size
70  fseek(in, 0L, SEEK_END); // Seek to end of file
71  long size = ftell(in); // Get position (i.e. the file size)
72  fseek(in, 0L, SEEK_SET); // Seek back to start of file
73 
74  // Sanity check
75  if ( size > SIZE_MAX ) {
76  // File size is larger than a size_t can hold
77  fclose( in );
78  delete tempf;
79  return;
80  }
81 
82  // Allocate a buffer for the compressed data
83  uint8_t* compressed_image = (uint8_t*)malloc(size);
84  if( ! compressed_image ) {
85  // malloc failed
86  fclose( in );
87  delete tempf;
88  return;
89  } // if
90 
91  // Read compressed image into buffer
92  size_t bytes_read = fread( compressed_image, sizeof(uint8_t), size, in );
93 
94  // Close the compressed image file
95  fclose( in );
96  delete tempf;
97 
98  if ( bytes_read < size ) {
99  // Read failed
100  free( compressed_image );
101  return;
102  }
103 
104  // === Decompress image ===
105 
106  // Get image dimensions
107  if ( ! WebPGetInfo( compressed_image, size, &width, &height ) ) {
108  // Error
109  free( compressed_image );
110  return;
111  }
112 
113  // Create an appropriately sized image
114  TQImage image;
115  if( ! image.create( width, height, 32 ) ) {
116  // Error
117  free( compressed_image );
118  return;
119  }
120 
121  // Enable alpha channel
122  image.setAlphaBuffer(true);
123 
124  // Get the image buffer
125  uint32_t* data = (uint32_t*)image.bits();
126 
127  // Decompress the image
128 #ifdef WORDS_BIGENDIAN
129  if ( ! WebPDecodeARGBInto( compressed_image, size, (uint8_t*)data, width*height*4, width*4) ) {
130 #else
131  if ( ! WebPDecodeBGRAInto( compressed_image, size, (uint8_t*)data, width*height*4, width*4) ) {
132 #endif
133  // Error
134  free( compressed_image );
135  return;
136  }
137 
138  // Free the compressed image buffer
139  free( compressed_image );
140 
141  // Finalize load
142  io->setImage( image );
143  io->setStatus( 0 );
144 } // kimgio_webp_read
145 
146 #ifdef __cplusplus
147 }
148 #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.