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

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