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.

456 lines
12KB

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