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.

449 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6385)
  19. //==============================================================================
  20. class GIFLoader
  21. {
  22. public:
  23. GIFLoader (InputStream& in)
  24. : input (in),
  25. dataBlockIsZero (false), fresh (false), finished (false),
  26. currentBit (0), lastBit (0), lastByteIndex (0),
  27. codeSize (0), setCodeSize (0), maxCode (0), maxCodeSize (0),
  28. firstcode (0), oldcode (0), clearCode (0), endCode (0)
  29. {
  30. int imageWidth, imageHeight;
  31. if (! getSizeFromHeader (imageWidth, imageHeight))
  32. return;
  33. uint8 buf [16];
  34. if (in.read (buf, 3) != 3)
  35. return;
  36. int numColours = 2 << (buf[0] & 7);
  37. int transparent = -1;
  38. if ((buf[0] & 0x80) != 0)
  39. readPalette (numColours);
  40. for (;;)
  41. {
  42. if (input.read (buf, 1) != 1 || buf[0] == ';')
  43. break;
  44. if (buf[0] == '!')
  45. {
  46. if (readExtension (transparent))
  47. continue;
  48. break;
  49. }
  50. if (buf[0] != ',')
  51. continue;
  52. if (input.read (buf, 9) == 9)
  53. {
  54. imageWidth = (int) ByteOrder::littleEndianShort (buf + 4);
  55. imageHeight = (int) ByteOrder::littleEndianShort (buf + 6);
  56. numColours = 2 << (buf[8] & 7);
  57. if ((buf[8] & 0x80) != 0)
  58. if (! readPalette (numColours))
  59. break;
  60. image = Image (transparent >= 0 ? Image::ARGB : Image::RGB,
  61. imageWidth, imageHeight, transparent >= 0);
  62. image.getProperties()->set ("originalImageHadAlpha", transparent >= 0);
  63. readImage ((buf[8] & 0x40) != 0, transparent);
  64. }
  65. break;
  66. }
  67. }
  68. Image image;
  69. private:
  70. InputStream& input;
  71. uint8 buffer [260];
  72. PixelARGB palette [256];
  73. bool dataBlockIsZero, fresh, finished;
  74. int currentBit, lastBit, lastByteIndex;
  75. int codeSize, setCodeSize;
  76. int maxCode, maxCodeSize;
  77. int firstcode, oldcode;
  78. int clearCode, endCode;
  79. enum { maxGifCode = 1 << 12 };
  80. int table [2] [maxGifCode];
  81. int stack [2 * maxGifCode];
  82. int* sp;
  83. bool getSizeFromHeader (int& w, int& h)
  84. {
  85. // Add an extra byte for the zero terminator
  86. char b[7]{};
  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. JUCE_END_IGNORE_WARNINGS_MSVC
  324. #endif
  325. //==============================================================================
  326. GIFImageFormat::GIFImageFormat() {}
  327. GIFImageFormat::~GIFImageFormat() {}
  328. String GIFImageFormat::getFormatName() { return "GIF"; }
  329. bool GIFImageFormat::usesFileExtension (const File& f) { return f.hasFileExtension ("gif"); }
  330. bool GIFImageFormat::canUnderstand (InputStream& in)
  331. {
  332. char header [4];
  333. return (in.read (header, sizeof (header)) == (int) sizeof (header))
  334. && header[0] == 'G'
  335. && header[1] == 'I'
  336. && header[2] == 'F';
  337. }
  338. Image GIFImageFormat::decodeImage (InputStream& in)
  339. {
  340. #if (JUCE_MAC || JUCE_IOS) && USE_COREGRAPHICS_RENDERING && JUCE_USE_COREIMAGE_LOADER
  341. return juce_loadWithCoreImage (in);
  342. #else
  343. const std::unique_ptr<GIFLoader> loader (new GIFLoader (in));
  344. return loader->image;
  345. #endif
  346. }
  347. bool GIFImageFormat::writeImageToStream (const Image& /*sourceImage*/, OutputStream& /*destStream*/)
  348. {
  349. jassertfalse; // writing isn't implemented for GIFs!
  350. return false;
  351. }
  352. } // namespace juce