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.

444 lines
11KB

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