PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/share/native/sun/awt/splashscreen/splashscreen_gif.c

https://bitbucket.org/psandoz/lambda-jdk-pipeline-patches
C | 324 lines | 236 code | 46 blank | 42 comment | 39 complexity | c743ad0de0d9d8cb493477d7bd1f3096 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0
  1. /*
  2. * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. #include "splashscreen_impl.h"
  26. #include "splashscreen_gfx.h"
  27. #include "../giflib/gif_lib.h"
  28. #define GIF_TRANSPARENT 0x01
  29. #define GIF_USER_INPUT 0x02
  30. #define GIF_DISPOSE_MASK 0x07
  31. #define GIF_DISPOSE_SHIFT 2
  32. #define GIF_NOT_TRANSPARENT -1
  33. #define GIF_DISPOSE_NONE 0 // No disposal specified. The decoder is
  34. // not required to take any action.
  35. #define GIF_DISPOSE_LEAVE 1 // Do not dispose. The graphic is to be left
  36. // in place.
  37. #define GIF_DISPOSE_BACKGND 2 // Restore to background color. The area used by the
  38. // graphic must be restored to the background color.
  39. #define GIF_DISPOSE_RESTORE 3 // Restore to previous. The decoder is required to
  40. // restore the area overwritten by the graphic with
  41. // what was there prior to rendering the graphic.
  42. static const char szNetscape20ext[11] = "NETSCAPE2.0";
  43. #define NSEXT_LOOP 0x01 // Loop Count field code
  44. // convert libungif samples to our ones
  45. #define MAKE_QUAD_GIF(c,a) MAKE_QUAD((c).Red, (c).Green, (c).Blue, (unsigned)(a))
  46. /* stdio FILE* and memory input functions for libungif */
  47. int
  48. SplashStreamGifInputFunc(GifFileType * gif, GifByteType * buf, int n)
  49. {
  50. SplashStream* io = (SplashStream*)gif->UserData;
  51. int rc = io->read(io, buf, n);
  52. return rc;
  53. }
  54. /* These macro help to ensure that we only take part of frame that fits into
  55. logical screen. */
  56. /* Ensure that p belongs to [pmin, pmax) interval. Returns fixed point (if fix is needed) */
  57. #define FIX_POINT(p, pmin, pmax) ( ((p) < (pmin)) ? (pmin) : (((p) > (pmax)) ? (pmax) : (p)))
  58. /* Ensures that line starting at point p does not exceed boundary pmax.
  59. Returns fixed length (if fix is needed) */
  60. #define FIX_LENGTH(p, len, pmax) ( ((p) + (len)) > (pmax) ? ((pmax) - (p)) : (len))
  61. int
  62. SplashDecodeGif(Splash * splash, GifFileType * gif)
  63. {
  64. int stride;
  65. int bufferSize;
  66. byte_t *pBitmapBits, *pOldBitmapBits;
  67. int i, j;
  68. int imageIndex;
  69. int cx, cy, cw, ch; /* clamped coordinates */
  70. const int interlacedOffset[] = { 0, 4, 2, 1, 0 }; /* The way Interlaced image should. */
  71. const int interlacedJumps[] = { 8, 8, 4, 2, 1 }; /* be read - offsets and jumps... */
  72. if (DGifSlurp(gif) == GIF_ERROR) {
  73. return 0;
  74. }
  75. SplashCleanup(splash);
  76. if (!SAFE_TO_ALLOC(gif->SWidth, splash->imageFormat.depthBytes)) {
  77. return 0;
  78. }
  79. stride = gif->SWidth * splash->imageFormat.depthBytes;
  80. if (splash->byteAlignment > 1)
  81. stride =
  82. (stride + splash->byteAlignment - 1) & ~(splash->byteAlignment - 1);
  83. if (!SAFE_TO_ALLOC(gif->SHeight, stride)) {
  84. return 0;
  85. }
  86. if (!SAFE_TO_ALLOC(gif->ImageCount, sizeof(SplashImage*))) {
  87. return 0;
  88. }
  89. bufferSize = stride * gif->SHeight;
  90. pBitmapBits = (byte_t *) malloc(bufferSize);
  91. if (!pBitmapBits) {
  92. return 0;
  93. }
  94. pOldBitmapBits = (byte_t *) malloc(bufferSize);
  95. if (!pOldBitmapBits) {
  96. free(pBitmapBits);
  97. return 0;
  98. }
  99. memset(pBitmapBits, 0, bufferSize);
  100. splash->width = gif->SWidth;
  101. splash->height = gif->SHeight;
  102. splash->frameCount = gif->ImageCount;
  103. splash->frames = (SplashImage *)
  104. malloc(sizeof(SplashImage) * gif->ImageCount);
  105. if (!splash->frames) {
  106. free(pBitmapBits);
  107. free(pOldBitmapBits);
  108. return 0;
  109. }
  110. memset(splash->frames, 0, sizeof(SplashImage) * gif->ImageCount);
  111. splash->loopCount = 1;
  112. for (imageIndex = 0; imageIndex < gif->ImageCount; imageIndex++) {
  113. SavedImage *image = &(gif->SavedImages[imageIndex]);
  114. GifImageDesc *desc = &(image->ImageDesc);
  115. ColorMapObject *colorMap =
  116. desc->ColorMap ? desc->ColorMap : gif->SColorMap;
  117. int transparentColor = -1;
  118. int frameDelay = 100;
  119. int disposeMethod = GIF_DISPOSE_RESTORE;
  120. int colorCount = 0;
  121. rgbquad_t colorMapBuf[SPLASH_COLOR_MAP_SIZE];
  122. cx = FIX_POINT(desc->Left, 0, gif->SWidth);
  123. cy = FIX_POINT(desc->Top, 0, gif->SHeight);
  124. cw = FIX_LENGTH(desc->Left, desc->Width, gif->SWidth);
  125. ch = FIX_LENGTH(desc->Top, desc->Height, gif->SHeight);
  126. if (colorMap) {
  127. if (colorMap->ColorCount <= SPLASH_COLOR_MAP_SIZE) {
  128. colorCount = colorMap->ColorCount;
  129. } else {
  130. colorCount = SPLASH_COLOR_MAP_SIZE;
  131. }
  132. }
  133. /* the code below is loosely based around gif extension processing from win32 libungif sample */
  134. for (i = 0; i < image->ExtensionBlockCount; i++) {
  135. byte_t *pExtension = (byte_t *) image->ExtensionBlocks[i].Bytes;
  136. unsigned size = image->ExtensionBlocks[i].ByteCount;
  137. switch (image->ExtensionBlocks[i].Function) {
  138. case GRAPHICS_EXT_FUNC_CODE:
  139. {
  140. int flag = pExtension[0];
  141. frameDelay = (((int)pExtension[2]) << 8) | pExtension[1];
  142. if (frameDelay < 10)
  143. frameDelay = 10;
  144. if (flag & GIF_TRANSPARENT) {
  145. transparentColor = pExtension[3];
  146. } else {
  147. transparentColor = GIF_NOT_TRANSPARENT;
  148. }
  149. disposeMethod =
  150. (flag >> GIF_DISPOSE_SHIFT) & GIF_DISPOSE_MASK;
  151. break;
  152. }
  153. case APPLICATION_EXT_FUNC_CODE:
  154. {
  155. if (size == sizeof(szNetscape20ext)
  156. && memcmp(pExtension, szNetscape20ext, size) == 0) {
  157. int iSubCode;
  158. if (++i >= image->ExtensionBlockCount)
  159. break;
  160. pExtension = (byte_t *) image->ExtensionBlocks[i].Bytes;
  161. if (image->ExtensionBlocks[i].ByteCount != 3)
  162. break;
  163. iSubCode = pExtension[0] & 0x07;
  164. if (iSubCode == NSEXT_LOOP) {
  165. splash->loopCount =
  166. (pExtension[1] | (((int)pExtension[2]) << 8)) - 1;
  167. }
  168. }
  169. break;
  170. }
  171. default:
  172. break;
  173. }
  174. }
  175. if (colorMap) {
  176. for (i = 0; i < colorCount; i++) {
  177. colorMapBuf[i] = MAKE_QUAD_GIF(colorMap->Colors[i], 0xff);
  178. }
  179. }
  180. {
  181. byte_t *pSrc = image->RasterBits;
  182. ImageFormat srcFormat;
  183. ImageRect srcRect, dstRect;
  184. int pass, npass;
  185. if (desc->Interlace) {
  186. pass = 0;
  187. npass = 4;
  188. }
  189. else {
  190. pass = 4;
  191. npass = 5;
  192. }
  193. srcFormat.colorMap = colorMapBuf;
  194. srcFormat.depthBytes = 1;
  195. srcFormat.byteOrder = BYTE_ORDER_NATIVE;
  196. srcFormat.transparentColor = transparentColor;
  197. srcFormat.fixedBits = QUAD_ALPHA_MASK; // fixed 100% alpha
  198. srcFormat.premultiplied = 0;
  199. for (; pass < npass; ++pass) {
  200. int jump = interlacedJumps[pass];
  201. int ofs = interlacedOffset[pass];
  202. /* Number of source lines for current pass */
  203. int numPassLines = (desc->Height + jump - ofs - 1) / jump;
  204. /* Number of lines that fits to dest buffer */
  205. int numLines = (ch + jump - ofs - 1) / jump;
  206. initRect(&srcRect, 0, 0, desc->Width, numLines, 1,
  207. desc->Width, pSrc, &srcFormat);
  208. if (numLines > 0) {
  209. initRect(&dstRect, cx, cy + ofs, cw,
  210. numLines , jump, stride, pBitmapBits, &splash->imageFormat);
  211. pSrc += convertRect(&srcRect, &dstRect, CVT_ALPHATEST);
  212. }
  213. // skip extra source data
  214. pSrc += (numPassLines - numLines) * srcRect.stride;
  215. }
  216. }
  217. // now dispose of the previous frame correctly
  218. splash->frames[imageIndex].bitmapBits =
  219. (rgbquad_t *) malloc(bufferSize);
  220. if (!splash->frames[imageIndex].bitmapBits) {
  221. free(pBitmapBits);
  222. free(pOldBitmapBits);
  223. /* Assuming that callee will take care of splash frames we have already allocated */
  224. return 0;
  225. }
  226. memcpy(splash->frames[imageIndex].bitmapBits, pBitmapBits, bufferSize);
  227. SplashInitFrameShape(splash, imageIndex);
  228. splash->frames[imageIndex].delay = frameDelay * 10; // 100ths of second to milliseconds
  229. switch (disposeMethod) {
  230. case GIF_DISPOSE_LEAVE:
  231. memcpy(pOldBitmapBits, pBitmapBits, bufferSize);
  232. break;
  233. case GIF_DISPOSE_NONE:
  234. break;
  235. case GIF_DISPOSE_BACKGND:
  236. {
  237. ImageRect dstRect;
  238. rgbquad_t fillColor = 0; // 0 is transparent
  239. if (transparentColor < 0) {
  240. fillColor= MAKE_QUAD_GIF(
  241. colorMap->Colors[gif->SBackGroundColor], 0xff);
  242. }
  243. initRect(&dstRect,
  244. cx, cy, cw, ch,
  245. 1, stride,
  246. pBitmapBits, &splash->imageFormat);
  247. fillRect(fillColor, &dstRect);
  248. }
  249. break;
  250. case GIF_DISPOSE_RESTORE:
  251. {
  252. int lineSize = cw * splash->imageFormat.depthBytes;
  253. if (lineSize > 0) {
  254. int lineOffset = cx * splash->imageFormat.depthBytes;
  255. int lineIndex = cy * stride + lineOffset;
  256. for (j=0; j<ch; j++) {
  257. memcpy(pBitmapBits + lineIndex, pOldBitmapBits + lineIndex,
  258. lineSize);
  259. lineIndex += stride;
  260. }
  261. }
  262. }
  263. break;
  264. }
  265. }
  266. free(pBitmapBits);
  267. free(pOldBitmapBits);
  268. DGifCloseFile(gif);
  269. return 1;
  270. }
  271. int
  272. SplashDecodeGifStream(Splash * splash, SplashStream * stream)
  273. {
  274. GifFileType *gif = DGifOpen((void *) stream, SplashStreamGifInputFunc);
  275. if (!gif)
  276. return 0;
  277. return SplashDecodeGif(splash, gif);
  278. }