Audio plugin host https://kx.studio/carla
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.

juce_GIFLoader.cpp 12KB

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