The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

445 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #if (JUCE_MAC || JUCE_IOS) && USE_COREGRAPHICS_RENDERING && JUCE_USE_COREIMAGE_LOADER
  19. Image juce_loadWithCoreImage (InputStream& input);
  20. #else
  21. //==============================================================================
  22. class GIFLoader
  23. {
  24. public:
  25. GIFLoader (InputStream& in)
  26. : input (in),
  27. dataBlockIsZero (false), fresh (false), finished (false),
  28. currentBit (0), lastBit (0), lastByteIndex (0),
  29. codeSize (0), setCodeSize (0), maxCode (0), maxCodeSize (0),
  30. firstcode (0), oldcode (0), clearCode (0), endCode (0)
  31. {
  32. int imageWidth, imageHeight;
  33. if (! getSizeFromHeader (imageWidth, imageHeight))
  34. return;
  35. uint8 buf [16];
  36. if (in.read (buf, 3) != 3)
  37. return;
  38. int numColours = 2 << (buf[0] & 7);
  39. int transparent = -1;
  40. if ((buf[0] & 0x80) != 0)
  41. readPalette (numColours);
  42. for (;;)
  43. {
  44. if (input.read (buf, 1) != 1 || buf[0] == ';')
  45. break;
  46. if (buf[0] == '!')
  47. {
  48. if (readExtension (transparent))
  49. continue;
  50. break;
  51. }
  52. if (buf[0] != ',')
  53. continue;
  54. if (input.read (buf, 9) == 9)
  55. {
  56. imageWidth = (int) ByteOrder::littleEndianShort (buf + 4);
  57. imageHeight = (int) ByteOrder::littleEndianShort (buf + 6);
  58. numColours = 2 << (buf[8] & 7);
  59. if ((buf[8] & 0x80) != 0)
  60. if (! readPalette (numColours))
  61. break;
  62. image = Image (transparent >= 0 ? Image::ARGB : Image::RGB,
  63. imageWidth, imageHeight, transparent >= 0);
  64. image.getProperties()->set ("originalImageHadAlpha", transparent >= 0);
  65. readImage ((buf[8] & 0x40) != 0, transparent);
  66. }
  67. break;
  68. }
  69. }
  70. Image image;
  71. private:
  72. InputStream& input;
  73. uint8 buffer [260];
  74. PixelARGB palette [256];
  75. bool dataBlockIsZero, fresh, finished;
  76. int currentBit, lastBit, lastByteIndex;
  77. int codeSize, setCodeSize;
  78. int maxCode, maxCodeSize;
  79. int firstcode, oldcode;
  80. int clearCode, endCode;
  81. enum { maxGifCode = 1 << 12 };
  82. int table [2] [maxGifCode];
  83. int stack [2 * maxGifCode];
  84. int* sp;
  85. bool getSizeFromHeader (int& w, int& h)
  86. {
  87. char b[6];
  88. if (input.read (b, 6) == 6
  89. && (strncmp ("GIF87a", b, 6) == 0
  90. || strncmp ("GIF89a", b, 6) == 0))
  91. {
  92. if (input.read (b, 4) == 4)
  93. {
  94. w = (int) ByteOrder::littleEndianShort (b);
  95. h = (int) ByteOrder::littleEndianShort (b + 2);
  96. return w > 0 && h > 0;
  97. }
  98. }
  99. return false;
  100. }
  101. bool readPalette (const int numCols)
  102. {
  103. for (int i = 0; i < numCols; ++i)
  104. {
  105. uint8 rgb[4];
  106. input.read (rgb, 3);
  107. palette[i].setARGB (0xff, rgb[0], rgb[1], rgb[2]);
  108. palette[i].premultiply();
  109. }
  110. return true;
  111. }
  112. int readDataBlock (uint8* const dest)
  113. {
  114. uint8 n;
  115. if (input.read (&n, 1) == 1)
  116. {
  117. dataBlockIsZero = (n == 0);
  118. if (dataBlockIsZero || (input.read (dest, n) == n))
  119. return n;
  120. }
  121. return -1;
  122. }
  123. int readExtension (int& transparent)
  124. {
  125. uint8 type;
  126. if (input.read (&type, 1) != 1)
  127. return false;
  128. uint8 b [260];
  129. int n = 0;
  130. if (type == 0xf9)
  131. {
  132. n = readDataBlock (b);
  133. if (n < 0)
  134. return 1;
  135. if ((b[0] & 1) != 0)
  136. transparent = b[3];
  137. }
  138. do
  139. {
  140. n = readDataBlock (b);
  141. }
  142. while (n > 0);
  143. return n >= 0;
  144. }
  145. void clearTable()
  146. {
  147. int i;
  148. for (i = 0; i < clearCode; ++i)
  149. {
  150. table[0][i] = 0;
  151. table[1][i] = i;
  152. }
  153. for (; i < maxGifCode; ++i)
  154. {
  155. table[0][i] = 0;
  156. table[1][i] = 0;
  157. }
  158. }
  159. void initialise (const int inputCodeSize)
  160. {
  161. setCodeSize = inputCodeSize;
  162. codeSize = setCodeSize + 1;
  163. clearCode = 1 << setCodeSize;
  164. endCode = clearCode + 1;
  165. maxCodeSize = 2 * clearCode;
  166. maxCode = clearCode + 2;
  167. getCode (0, true);
  168. fresh = true;
  169. clearTable();
  170. sp = stack;
  171. }
  172. int readLZWByte()
  173. {
  174. if (fresh)
  175. {
  176. fresh = false;
  177. for (;;)
  178. {
  179. firstcode = oldcode = getCode (codeSize, false);
  180. if (firstcode != clearCode)
  181. return firstcode;
  182. }
  183. }
  184. if (sp > stack)
  185. return *--sp;
  186. int code;
  187. while ((code = getCode (codeSize, false)) >= 0)
  188. {
  189. if (code == clearCode)
  190. {
  191. clearTable();
  192. codeSize = setCodeSize + 1;
  193. maxCodeSize = 2 * clearCode;
  194. maxCode = clearCode + 2;
  195. sp = stack;
  196. firstcode = oldcode = getCode (codeSize, false);
  197. return firstcode;
  198. }
  199. else if (code == endCode)
  200. {
  201. if (dataBlockIsZero)
  202. return -2;
  203. uint8 buf [260];
  204. int n;
  205. while ((n = readDataBlock (buf)) > 0)
  206. {}
  207. if (n != 0)
  208. return -2;
  209. }
  210. const int incode = code;
  211. if (code >= maxCode)
  212. {
  213. *sp++ = firstcode;
  214. code = oldcode;
  215. }
  216. while (code >= clearCode)
  217. {
  218. *sp++ = table[1][code];
  219. if (code == table[0][code])
  220. return -2;
  221. code = table[0][code];
  222. }
  223. *sp++ = firstcode = table[1][code];
  224. if ((code = maxCode) < maxGifCode)
  225. {
  226. table[0][code] = oldcode;
  227. table[1][code] = firstcode;
  228. ++maxCode;
  229. if (maxCode >= maxCodeSize && maxCodeSize < maxGifCode)
  230. {
  231. maxCodeSize <<= 1;
  232. ++codeSize;
  233. }
  234. }
  235. oldcode = incode;
  236. if (sp > stack)
  237. return *--sp;
  238. }
  239. return code;
  240. }
  241. int getCode (const int codeSize_, const bool shouldInitialise)
  242. {
  243. if (shouldInitialise)
  244. {
  245. currentBit = 0;
  246. lastBit = 0;
  247. finished = false;
  248. return 0;
  249. }
  250. if ((currentBit + codeSize_) >= lastBit)
  251. {
  252. if (finished)
  253. return -1;
  254. buffer[0] = buffer [lastByteIndex - 2];
  255. buffer[1] = buffer [lastByteIndex - 1];
  256. const int n = readDataBlock (buffer + 2);
  257. if (n == 0)
  258. finished = true;
  259. lastByteIndex = 2 + n;
  260. currentBit = (currentBit - lastBit) + 16;
  261. lastBit = (2 + n) * 8 ;
  262. }
  263. int result = 0;
  264. int i = currentBit;
  265. for (int j = 0; j < codeSize_; ++j)
  266. {
  267. result |= ((buffer[i >> 3] & (1 << (i & 7))) != 0) << j;
  268. ++i;
  269. }
  270. currentBit += codeSize_;
  271. return result;
  272. }
  273. bool readImage (const int interlace, const int transparent)
  274. {
  275. uint8 c;
  276. if (input.read (&c, 1) != 1)
  277. return false;
  278. initialise (c);
  279. if (transparent >= 0)
  280. palette [transparent].setARGB (0, 0, 0, 0);
  281. int xpos = 0, ypos = 0, yStep = 8, pass = 0;
  282. const Image::BitmapData destData (image, Image::BitmapData::writeOnly);
  283. uint8* p = destData.getPixelPointer (0, 0);
  284. const bool hasAlpha = image.hasAlphaChannel();
  285. for (;;)
  286. {
  287. const int index = readLZWByte();
  288. if (index < 0)
  289. break;
  290. if (hasAlpha)
  291. ((PixelARGB*) p)->set (palette [index]);
  292. else
  293. ((PixelRGB*) p)->set (palette [index]);
  294. p += destData.pixelStride;
  295. if (++xpos == destData.width)
  296. {
  297. xpos = 0;
  298. if (interlace)
  299. {
  300. ypos += yStep;
  301. while (ypos >= destData.height)
  302. {
  303. switch (++pass)
  304. {
  305. case 1: ypos = 4; yStep = 8; break;
  306. case 2: ypos = 2; yStep = 4; break;
  307. case 3: ypos = 1; yStep = 2; break;
  308. default: return true;
  309. }
  310. }
  311. }
  312. else
  313. {
  314. if (++ypos >= destData.height)
  315. break;
  316. }
  317. p = destData.getPixelPointer (xpos, ypos);
  318. }
  319. }
  320. return true;
  321. }
  322. JUCE_DECLARE_NON_COPYABLE (GIFLoader);
  323. };
  324. #endif
  325. //==============================================================================
  326. GIFImageFormat::GIFImageFormat() {}
  327. GIFImageFormat::~GIFImageFormat() {}
  328. String GIFImageFormat::getFormatName() { return "GIF"; }
  329. bool GIFImageFormat::canUnderstand (InputStream& in)
  330. {
  331. char header [4];
  332. return (in.read (header, sizeof (header)) == sizeof (header))
  333. && header[0] == 'G'
  334. && header[1] == 'I'
  335. && header[2] == 'F';
  336. }
  337. Image GIFImageFormat::decodeImage (InputStream& in)
  338. {
  339. #if (JUCE_MAC || JUCE_IOS) && USE_COREGRAPHICS_RENDERING && JUCE_USE_COREIMAGE_LOADER
  340. return juce_loadWithCoreImage (in);
  341. #else
  342. const ScopedPointer <GIFLoader> loader (new GIFLoader (in));
  343. return loader->image;
  344. #endif
  345. }
  346. bool GIFImageFormat::writeImageToStream (const Image& /*sourceImage*/, OutputStream& /*destStream*/)
  347. {
  348. jassertfalse; // writing isn't implemented for GIFs!
  349. return false;
  350. }