TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hdr.cpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* This file is part of the KDE project
  2. Copyright (C) 2005 Christoph Hormann <chris_hormann@gmx.de>
  3. Copyright (C) 2005 Ignacio Castaño <castanyo@yahoo.es>
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the Lesser GNU General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. */
  9. #include "hdr.h"
  10. #include <tqimage.h>
  11. #include <tqdatastream.h>
  12. #include <kdebug.h>
  13. #include <tdeglobal.h>
  14. typedef TQ_UINT8 uchar;
  15. namespace { // Private.
  16. #define MAXLINE 1024
  17. #define MINELEN 8 // minimum scanline length for encoding
  18. #define MAXELEN 0x7fff // maximum scanline length for encoding
  19. static inline uchar ClipToByte(float value)
  20. {
  21. if (value > 255.0f) return 255;
  22. //else if (value < 0.0f) return 0; // we know value is positive.
  23. return uchar(value);
  24. }
  25. // read an old style line from the hdr image file
  26. // if 'first' is true the first byte is already read
  27. static bool Read_Old_Line (uchar * image, int width, TQDataStream & s)
  28. {
  29. int rshift = 0;
  30. int i;
  31. while (width > 0)
  32. {
  33. s >> image[0];
  34. s >> image[1];
  35. s >> image[2];
  36. s >> image[3];
  37. if (s.atEnd()) return false;
  38. if ((image[0] == 1) && (image[1] == 1) && (image[2] == 1))
  39. {
  40. for (i = image[3] << rshift; i > 0; i--)
  41. {
  42. //memcpy(image, image-4, 4);
  43. (uint &)image[0] = (uint &)image[0-4];
  44. image += 4;
  45. width--;
  46. }
  47. rshift += 8;
  48. }
  49. else
  50. {
  51. image += 4;
  52. width--;
  53. rshift = 0;
  54. }
  55. }
  56. return true;
  57. }
  58. static void RGBE_To_QRgbLine(uchar * image, QRgb * scanline, int width)
  59. {
  60. for (int j = 0; j < width; j++)
  61. {
  62. // v = ldexp(1.0, int(image[3]) - 128);
  63. float v;
  64. int e = int(image[3]) - 128;
  65. if( e > 0 )
  66. {
  67. v = float(1 << e);
  68. }
  69. else
  70. {
  71. v = 1.0f / float(1 << -e);
  72. }
  73. scanline[j] = tqRgb( ClipToByte(float(image[0]) * v),
  74. ClipToByte(float(image[1]) * v),
  75. ClipToByte(float(image[2]) * v) );
  76. image += 4;
  77. }
  78. }
  79. // Load the HDR image.
  80. static bool LoadHDR( TQDataStream & s, const int width, const int height, TQImage & img )
  81. {
  82. uchar val, code;
  83. // Create dst image.
  84. if( !img.create( width, height, 32 ) )
  85. {
  86. return false;
  87. }
  88. TQMemArray<uchar> image( width * 4 );
  89. for (int cline = 0; cline < height; cline++)
  90. {
  91. QRgb * scanline = (QRgb *) img.scanLine( cline );
  92. // determine scanline type
  93. if ((width < MINELEN) || (MAXELEN < width))
  94. {
  95. Read_Old_Line(image.data(), width, s);
  96. RGBE_To_QRgbLine(image.data(), scanline, width);
  97. continue;
  98. }
  99. s >> val;
  100. if (s.atEnd())
  101. {
  102. return true;
  103. }
  104. if (val != 2)
  105. {
  106. s.device()->at( s.device()->at() - 1 );
  107. Read_Old_Line(image.data(), width, s);
  108. RGBE_To_QRgbLine(image.data(), scanline, width);
  109. continue;
  110. }
  111. s >> image[1];
  112. s >> image[2];
  113. s >> image[3];
  114. if (s.atEnd())
  115. {
  116. return true;
  117. }
  118. if ((image[1] != 2) || (image[2] & 128))
  119. {
  120. image[0] = 2;
  121. Read_Old_Line(image.data()+4, width-1, s);
  122. RGBE_To_QRgbLine(image.data(), scanline, width);
  123. continue;
  124. }
  125. if ((image[2] << 8 | image[3]) != width)
  126. {
  127. return false;
  128. }
  129. // read each component
  130. for (int i = 0; i < 4; i++)
  131. {
  132. for (int j = 0; j < width; )
  133. {
  134. s >> code;
  135. if (s.atEnd())
  136. {
  137. return false;
  138. }
  139. if (code > 128)
  140. {
  141. // run
  142. code &= 127;
  143. s >> val;
  144. while( code != 0 )
  145. {
  146. image[i + j * 4] = val;
  147. j++;
  148. code--;
  149. }
  150. }
  151. else
  152. {
  153. // non-run
  154. while( code != 0 )
  155. {
  156. s >> image[i + j * 4];
  157. j++;
  158. code--;
  159. }
  160. }
  161. }
  162. }
  163. RGBE_To_QRgbLine(image.data(), scanline, width);
  164. }
  165. return true;
  166. }
  167. } // namespace
  168. KDE_EXPORT void kimgio_hdr_read( TQImageIO * io )
  169. {
  170. int len;
  171. char line[MAXLINE];
  172. //bool validHeader = false;
  173. bool validFormat = false;
  174. // Parse header
  175. do {
  176. len = io->ioDevice()->readLine(line, MAXLINE);
  177. /*if (strcmp(line, "#?RADIANCE\n") == 0 || strcmp(line, "#?RGBE\n") == 0)
  178. {
  179. validHeader = true;
  180. }*/
  181. if (strcmp(line, "FORMAT=32-bit_rle_rgbe\n") == 0)
  182. {
  183. validFormat = true;
  184. }
  185. } while((len > 0) && (line[0] != '\n'));
  186. if( /*!validHeader ||*/ !validFormat )
  187. {
  188. kdDebug(399) << "Unknown HDR format." << endl;
  189. io->setImage( TQImage() );
  190. io->setStatus( -1 );
  191. return;
  192. }
  193. io->ioDevice()->readLine(line, MAXLINE);
  194. char s1[3], s2[3];
  195. int width, height;
  196. if (sscanf(line, "%2[+-XY] %d %2[+-XY] %d\n", s1, &height, s2, &width) != 4)
  197. //if( sscanf(line, "-Y %d +X %d", &height, &width) < 2 )
  198. {
  199. kdDebug(399) << "Invalid HDR file." << endl;
  200. io->setImage( TQImage() );
  201. io->setStatus( -1 );
  202. return;
  203. }
  204. TQDataStream s( io->ioDevice() );
  205. TQImage img;
  206. if( !LoadHDR(s, width, height, img) )
  207. {
  208. kdDebug(399) << "Error loading HDR file." << endl;
  209. io->setImage( TQImage() );
  210. io->setStatus( -1 );
  211. return;
  212. }
  213. io->setImage( img );
  214. io->setStatus( 0 );
  215. }
  216. KDE_EXPORT void kimgio_hdr_write( TQImageIO * )
  217. {
  218. // intentionally not implemented (since writing low dynamic range data to a HDR file is nonsense.)
  219. }