haiku-jdk-jdk /src/share/native/sun/awt/splashscreen/splashscreen_gif.c

Language C Lines 325
MD5 Hash c743ad0de0d9d8cb493477d7bd1f3096 Estimated Cost $5,932 (why?)
Repository https://bitbucket.org/hamishm/haiku-jdk-jdk View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

#include "splashscreen_impl.h"
#include "splashscreen_gfx.h"

#include "../giflib/gif_lib.h"

#define GIF_TRANSPARENT     0x01
#define GIF_USER_INPUT      0x02
#define GIF_DISPOSE_MASK    0x07
#define GIF_DISPOSE_SHIFT   2

#define GIF_NOT_TRANSPARENT -1

#define GIF_DISPOSE_NONE    0   // No disposal specified. The decoder is
                                // not required to take any action.
#define GIF_DISPOSE_LEAVE   1   // Do not dispose. The graphic is to be left
                                // in place.
#define GIF_DISPOSE_BACKGND 2   // Restore to background color. The area used by the
                                // graphic must be restored to the background color.

#define GIF_DISPOSE_RESTORE 3   // Restore to previous. The decoder is required to
                                // restore the area overwritten by the graphic with
                                // what was there prior to rendering the graphic.

static const char szNetscape20ext[11] = "NETSCAPE2.0";

#define NSEXT_LOOP      0x01    // Loop Count field code

// convert libungif samples to our ones
#define MAKE_QUAD_GIF(c,a) MAKE_QUAD((c).Red, (c).Green, (c).Blue, (unsigned)(a))

/* stdio FILE* and memory input functions for libungif */
int
SplashStreamGifInputFunc(GifFileType * gif, GifByteType * buf, int n)
{
    SplashStream* io = (SplashStream*)gif->UserData;
    int rc = io->read(io, buf, n);
    return rc;
}

/* These macro help to ensure that we only take part of frame that fits into
   logical screen. */

/* Ensure that p belongs to [pmin, pmax) interval. Returns fixed point (if fix is needed) */
#define FIX_POINT(p, pmin, pmax) ( ((p) < (pmin)) ? (pmin) : (((p) > (pmax)) ? (pmax) : (p)))
/* Ensures that line starting at point p does not exceed boundary pmax.
   Returns fixed length (if fix is needed) */
#define FIX_LENGTH(p, len, pmax) ( ((p) + (len)) > (pmax) ? ((pmax) - (p)) : (len))

int
SplashDecodeGif(Splash * splash, GifFileType * gif)
{
    int stride;
    int bufferSize;
    byte_t *pBitmapBits, *pOldBitmapBits;
    int i, j;
    int imageIndex;
    int cx, cy, cw, ch; /* clamped coordinates */
    const int interlacedOffset[] = { 0, 4, 2, 1, 0 };   /* The way Interlaced image should. */
    const int interlacedJumps[] = { 8, 8, 4, 2, 1 };    /* be read - offsets and jumps... */

    if (DGifSlurp(gif) == GIF_ERROR) {
        return 0;
    }

    SplashCleanup(splash);

    if (!SAFE_TO_ALLOC(gif->SWidth, splash->imageFormat.depthBytes)) {
        return 0;
    }
    stride = gif->SWidth * splash->imageFormat.depthBytes;
    if (splash->byteAlignment > 1)
        stride =
            (stride + splash->byteAlignment - 1) & ~(splash->byteAlignment - 1);

    if (!SAFE_TO_ALLOC(gif->SHeight, stride)) {
        return 0;
    }

    if (!SAFE_TO_ALLOC(gif->ImageCount, sizeof(SplashImage*))) {
        return 0;
    }
    bufferSize = stride * gif->SHeight;
    pBitmapBits = (byte_t *) malloc(bufferSize);
    if (!pBitmapBits) {
        return 0;
    }
    pOldBitmapBits = (byte_t *) malloc(bufferSize);
    if (!pOldBitmapBits) {
        free(pBitmapBits);
        return 0;
    }
    memset(pBitmapBits, 0, bufferSize);

    splash->width = gif->SWidth;
    splash->height = gif->SHeight;
    splash->frameCount = gif->ImageCount;
    splash->frames = (SplashImage *)
        malloc(sizeof(SplashImage) * gif->ImageCount);
    if (!splash->frames) {
      free(pBitmapBits);
      free(pOldBitmapBits);
      return 0;
    }
    memset(splash->frames, 0, sizeof(SplashImage) * gif->ImageCount);
    splash->loopCount = 1;

    for (imageIndex = 0; imageIndex < gif->ImageCount; imageIndex++) {
        SavedImage *image = &(gif->SavedImages[imageIndex]);
        GifImageDesc *desc = &(image->ImageDesc);
        ColorMapObject *colorMap =
            desc->ColorMap ? desc->ColorMap : gif->SColorMap;

        int transparentColor = -1;
        int frameDelay = 100;
        int disposeMethod = GIF_DISPOSE_RESTORE;
        int colorCount = 0;
        rgbquad_t colorMapBuf[SPLASH_COLOR_MAP_SIZE];

        cx = FIX_POINT(desc->Left, 0, gif->SWidth);
        cy = FIX_POINT(desc->Top, 0, gif->SHeight);
        cw = FIX_LENGTH(desc->Left, desc->Width, gif->SWidth);
        ch = FIX_LENGTH(desc->Top, desc->Height, gif->SHeight);

        if (colorMap) {
            if (colorMap->ColorCount <= SPLASH_COLOR_MAP_SIZE) {
                colorCount = colorMap->ColorCount;
            } else  {
                colorCount = SPLASH_COLOR_MAP_SIZE;
            }
        }

        /* the code below is loosely based around gif extension processing from win32 libungif sample */

        for (i = 0; i < image->ExtensionBlockCount; i++) {
            byte_t *pExtension = (byte_t *) image->ExtensionBlocks[i].Bytes;
            unsigned size = image->ExtensionBlocks[i].ByteCount;

            switch (image->ExtensionBlocks[i].Function) {
            case GRAPHICS_EXT_FUNC_CODE:
                {
                    int flag = pExtension[0];

                    frameDelay = (((int)pExtension[2]) << 8) | pExtension[1];
                    if (frameDelay < 10)
                        frameDelay = 10;
                    if (flag & GIF_TRANSPARENT) {
                        transparentColor = pExtension[3];
                    } else {
                        transparentColor = GIF_NOT_TRANSPARENT;
                    }
                    disposeMethod =
                        (flag >> GIF_DISPOSE_SHIFT) & GIF_DISPOSE_MASK;
                    break;
                }
            case APPLICATION_EXT_FUNC_CODE:
                {
                    if (size == sizeof(szNetscape20ext)
                        && memcmp(pExtension, szNetscape20ext, size) == 0) {
                        int iSubCode;

                        if (++i >= image->ExtensionBlockCount)
                            break;
                        pExtension = (byte_t *) image->ExtensionBlocks[i].Bytes;
                        if (image->ExtensionBlocks[i].ByteCount != 3)
                            break;
                        iSubCode = pExtension[0] & 0x07;
                        if (iSubCode == NSEXT_LOOP) {
                            splash->loopCount =
                                (pExtension[1] | (((int)pExtension[2]) << 8)) - 1;
                        }
                    }
                    break;
                }
            default:
                break;
            }
        }

        if (colorMap) {
            for (i = 0; i < colorCount; i++) {
                colorMapBuf[i] = MAKE_QUAD_GIF(colorMap->Colors[i], 0xff);
            }
        }
        {

            byte_t *pSrc = image->RasterBits;
            ImageFormat srcFormat;
            ImageRect srcRect, dstRect;
            int pass, npass;

            if (desc->Interlace) {
                pass = 0;
                npass = 4;
            }
            else {
                pass = 4;
                npass = 5;
            }

            srcFormat.colorMap = colorMapBuf;
            srcFormat.depthBytes = 1;
            srcFormat.byteOrder = BYTE_ORDER_NATIVE;
            srcFormat.transparentColor = transparentColor;
            srcFormat.fixedBits = QUAD_ALPHA_MASK;      // fixed 100% alpha
            srcFormat.premultiplied = 0;

            for (; pass < npass; ++pass) {
                int jump = interlacedJumps[pass];
                int ofs = interlacedOffset[pass];
                /* Number of source lines for current pass */
                int numPassLines = (desc->Height + jump - ofs - 1) / jump;
                /* Number of lines that fits to dest buffer */
                int numLines = (ch + jump - ofs - 1) / jump;

                initRect(&srcRect, 0, 0, desc->Width, numLines, 1,
                    desc->Width, pSrc, &srcFormat);

                if (numLines > 0) {
                    initRect(&dstRect, cx, cy + ofs, cw,
                             numLines , jump, stride, pBitmapBits, &splash->imageFormat);

                    pSrc += convertRect(&srcRect, &dstRect, CVT_ALPHATEST);
                }
                // skip extra source data
                pSrc += (numPassLines - numLines) * srcRect.stride;
            }
        }

        // now dispose of the previous frame correctly

        splash->frames[imageIndex].bitmapBits =
            (rgbquad_t *) malloc(bufferSize);
        if (!splash->frames[imageIndex].bitmapBits) {
            free(pBitmapBits);
            free(pOldBitmapBits);
            /* Assuming that callee will take care of splash frames we have already allocated */
            return 0;
        }
        memcpy(splash->frames[imageIndex].bitmapBits, pBitmapBits, bufferSize);

        SplashInitFrameShape(splash, imageIndex);

        splash->frames[imageIndex].delay = frameDelay * 10;     // 100ths of second to milliseconds
        switch (disposeMethod) {
        case GIF_DISPOSE_LEAVE:
            memcpy(pOldBitmapBits, pBitmapBits, bufferSize);
            break;
        case GIF_DISPOSE_NONE:
            break;
        case GIF_DISPOSE_BACKGND:
            {
                ImageRect dstRect;
                rgbquad_t fillColor = 0;                        // 0 is transparent

                if (transparentColor < 0) {
                    fillColor= MAKE_QUAD_GIF(
                        colorMap->Colors[gif->SBackGroundColor], 0xff);
                }
                initRect(&dstRect,
                         cx, cy, cw, ch,
                         1, stride,
                         pBitmapBits, &splash->imageFormat);
                fillRect(fillColor, &dstRect);
            }
            break;
        case GIF_DISPOSE_RESTORE:
            {
                int lineSize = cw * splash->imageFormat.depthBytes;
                if (lineSize > 0) {
                    int lineOffset = cx * splash->imageFormat.depthBytes;
                    int lineIndex = cy * stride + lineOffset;
                    for (j=0; j<ch; j++) {
                        memcpy(pBitmapBits + lineIndex, pOldBitmapBits + lineIndex,
                               lineSize);
                        lineIndex += stride;
                    }
                }
            }
            break;
        }
    }

    free(pBitmapBits);
    free(pOldBitmapBits);

    DGifCloseFile(gif);

    return 1;
}

int
SplashDecodeGifStream(Splash * splash, SplashStream * stream)
{
    GifFileType *gif = DGifOpen((void *) stream, SplashStreamGifInputFunc);

    if (!gif)
        return 0;
    return SplashDecodeGif(splash, gif);
}
Back to Top