| 6 |
| Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | of this software and associated documentation files (the "Software"), to deal |
| 8 | in the Software without restriction, including without limitation the rights |
| 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | copies of the Software, and to permit persons to whom the Software is |
| 11 | furnished to do so, subject to the following conditions: |
| 12 |
| 13 | The above copyright notice and this permission notice shall be included in |
| 14 | all copies or substantial portions of the Software. |
| 15 |
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | THE SOFTWARE. |
| 23 |
| 24 | // Copyright (c) 2011 Google Inc. All rights reserved. |
| 25 | // |
| 26 | // Redistribution and use in source and binary forms, with or without |
| 27 | // modification, are permitted provided that the following conditions are |
| 28 | // met: |
| 29 | // |
| 30 | // * Redistributions of source code must retain the above copyright |
| 31 | // notice, this list of conditions and the following disclaimer. |
| 32 | // * Redistributions in binary form must reproduce the above |
| 33 | // copyright notice, this list of conditions and the following disclaimer |
| 34 | // in the documentation and/or other materials provided with the |
| 35 | // distribution. |
| 36 | // * Neither the name of Google Inc. nor the names of its |
| 37 | // contributors may be used to endorse or promote products derived from |
| 38 | // this software without specific prior written permission. |
| 39 | // |
| 40 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 41 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 42 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 43 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 44 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 45 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 46 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 47 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 48 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 49 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 50 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 51 |
| 52 | The GIFLIB distribution is Copyright (c) 1997 Eric S. Raymond |
| 53 | */ |
| 54 |
| 55 | #include <jni.h> |
| 56 | #include <stdio.h> |
| 57 | #include <time.h> |
| 58 | #include <limits.h> |
| 59 | #include "gif.h" |
| 60 | #include "giflib/gif_lib.h" |
| 61 |
| 62 | #define D_GIF_ERR_NO_FRAMES 1000 |
| 63 | #define D_GIF_ERR_INVALID_SCR_DIMS 1001 |
| 64 | #define D_GIF_ERR_INVALID_IMG_DIMS 1002 |
| 65 | #define D_GIF_ERR_IMG_NOT_CONFINED 1003 |
| 66 |
| 67 | typedef struct { |
| 68 | uint8_t blue; |
| 69 | uint8_t green; |
| 70 | uint8_t red; |
| 71 | uint8_t alpha; |
| 72 | } argb; |
| 73 |
| 74 | typedef struct { |
| 75 | unsigned int duration; |
| 76 | short transpIndex; |
| 77 | unsigned char disposalMethod; |
| 78 | } FrameInfo; |
| 79 |
| 80 | typedef struct { |
| 81 | GifFileType *gifFilePtr; |
| 82 | unsigned long lastFrameReaminder; |
| 83 | unsigned long nextStartTime; |
| 84 | int currentIndex; |
| 85 | unsigned int lastDrawIndex; |
| 86 | FrameInfo *infos; |
| 87 | argb *backupPtr; |
| 88 | int startPos; |
| 89 | unsigned char *rasterBits; |
| 90 | char *comment; |
| 91 | unsigned short loopCount; |
| 92 | int currentLoop; |
| 93 | jfloat speedFactor; |
| 94 | } GifInfo; |
| 95 |
| 96 | static ColorMapObject *defaultCmap = NULL; |
| 97 |
| 98 | static ColorMapObject *genDefColorMap(void) { |
| 99 | ColorMapObject *cmap = GifMakeMapObject(256, NULL); |
| 100 | if (cmap != NULL) { |
| 101 | int iColor; |
| 102 | for (iColor = 0; iColor < 256; iColor++) { |
| 103 | cmap->Colors[iColor].Red = (GifByteType) iColor; |
| 104 | cmap->Colors[iColor].Green = (GifByteType) iColor; |
| 105 | cmap->Colors[iColor].Blue = (GifByteType) iColor; |
| 106 | } |
| 107 | } |
| 108 | return cmap; |
| 109 | } |
| 110 |
| 111 | jint gifOnJNILoad(JavaVM *vm, void *reserved, JNIEnv *env) { |
| 112 | defaultCmap = genDefColorMap(); |
| 113 | if (defaultCmap == NULL) { |
| 114 | return -1; |
| 115 | } |
| 116 | return JNI_VERSION_1_4; |
| 117 | } |
| 118 |
| 119 | void gifOnJNIUnload(JavaVM *vm, void *reserved) { |
| 120 | GifFreeMapObject(defaultCmap); |
| 121 | } |
| 122 |
| 123 | static int fileReadFunc(GifFileType *gif, GifByteType *bytes, int size) { |
| 124 | FILE *file = (FILE *)gif->UserData; |
| 125 | return fread(bytes, 1, size, file); |
| 126 | } |
| 127 |
| 128 | static int fileRewindFun(GifInfo *info) { |
| 129 | return fseek(info->gifFilePtr->UserData, info->startPos, SEEK_SET); |
| 130 | } |
| 131 |
| 132 | static unsigned long getRealTime() { |
| 133 | struct timespec ts; |
| 134 | const clockid_t id = CLOCK_MONOTONIC; |
| 135 | if (id != (clockid_t) - 1 && clock_gettime(id, &ts) != -1) |
| 136 | return ts.tv_sec * 1000 + ts.tv_nsec / 1000000; |
| 137 | return -1; |
| 138 | } |
| 139 |
| 140 | static void cleanUp(GifInfo *info) { |
| 141 | if (info->backupPtr) { |
| 142 | free(info->backupPtr); |
| 143 | info->backupPtr = NULL; |
| 144 | } |
| 145 | if (info->infos) { |
| 146 | free(info->infos); |
| 147 | info->infos = NULL; |
| 148 | } |
| 149 | if (info->rasterBits) { |
| 150 | free(info->rasterBits); |
| 151 | info->rasterBits = NULL; |
| 152 | } |
| 153 | if (info->comment) { |
| 154 | free(info->comment); |
| 155 | info->comment = NULL; |
| 156 | } |
| 157 |
| 158 | GifFileType *GifFile = info->gifFilePtr; |
| 159 | if (GifFile->SColorMap == defaultCmap) { |
| 160 | GifFile->SColorMap = NULL; |
| 161 | } |
| 162 | if (GifFile->SavedImages != NULL) { |
| 163 | SavedImage *sp; |
| 164 | for (sp = GifFile->SavedImages; sp < GifFile->SavedImages + GifFile->ImageCount; sp++) { |
| 165 | if (sp->ImageDesc.ColorMap != NULL) { |
| 166 | GifFreeMapObject(sp->ImageDesc.ColorMap); |
| 167 | sp->ImageDesc.ColorMap = NULL; |
| 168 | } |
| 169 | } |
| 170 | free(GifFile->SavedImages); |
| 171 | GifFile->SavedImages = NULL; |
| 172 | } |
| 173 | DGifCloseFile(GifFile); |
| 174 | free(info); |
| 175 | } |
| 176 |
| 177 | static int getComment(GifByteType *Bytes, char **cmt) { |
| 178 | unsigned int len = (unsigned int) Bytes[0]; |
| 179 | unsigned int offset = *cmt != NULL ? strlen(*cmt) : 0; |
| 180 | char *ret = realloc(*cmt, (len + offset + 1) * sizeof(char)); |
| 181 | if (ret != NULL) { |
| 182 | memcpy(ret + offset, &Bytes[1], len); |
| 183 | ret[len + offset] = 0; |
| 184 | *cmt = ret; |
| 185 | return GIF_OK; |
| 186 | } |
| 187 | return GIF_ERROR; |
| 188 | } |
| 189 |
| 190 | static void packARGB32(argb *pixel, GifByteType alpha, GifByteType red, GifByteType green, GifByteType blue) { |
| 191 | pixel->alpha = alpha; |
| 192 | pixel->red = red; |
| 193 | pixel->green = green; |
| 194 | pixel->blue = blue; |
| 195 | } |
| 196 |
| 197 | static void getColorFromTable(int idx, argb *dst, const ColorMapObject *cmap) { |
| 198 | char colIdx = idx >= cmap->ColorCount ? 0 : idx; |
| 199 | GifColorType *col = &cmap->Colors[colIdx]; |
| 200 | packARGB32(dst, 0xFF, col->Red, col->Green, col->Blue); |
| 201 | } |
| 202 |
| 203 | static void eraseColor(argb *bm, int w, int h, argb color) { |
| 204 | int i; |
| 205 | for (i = 0; i < w * h; i++) { |
| 206 | *(bm + i) = color; |
| 207 | } |
| 208 | } |
| 209 |
| 210 | static inline bool setupBackupBmp(GifInfo *info, short transpIndex) { |
| 211 | GifFileType *fGIF = info->gifFilePtr; |
| 212 | info->backupPtr = calloc(fGIF->SWidth * fGIF->SHeight, sizeof(argb)); |
| 213 | if (!info->backupPtr) { |
| 214 | info->gifFilePtr->Error = D_GIF_ERR_NOT_ENOUGH_MEM; |
| 215 | return false; |
| 216 | } |
| 217 | argb paintingColor; |
| 218 | if (transpIndex == -1) { |
| 219 | getColorFromTable(fGIF->SBackGroundColor, &paintingColor, fGIF->SColorMap); |
| 220 | } else { |
| 221 | packARGB32(&paintingColor,0,0,0,0); |
| 222 | } |
| 223 | eraseColor(info->backupPtr, fGIF->SWidth, fGIF->SHeight, paintingColor); |
| 224 | return true; |
| 225 | } |
| 226 |
| 227 | static int readExtensions(int ExtFunction, GifByteType *ExtData, GifInfo *info) { |
| 228 | if (ExtData == NULL) { |
| 229 | return GIF_OK; |
| 230 | } |
| 231 | if (ExtFunction == GRAPHICS_EXT_FUNC_CODE && ExtData[0] == 4) { |
| 232 | FrameInfo *fi = &info->infos[info->gifFilePtr->ImageCount]; |
| 233 | fi->transpIndex = -1; |
| 234 | char *b = (char *)ExtData + 1; |
| 235 | short delay = ((b[2] << 8) | b[1]); |
| 236 | fi->duration = delay > 1 ? delay * 10 : 100; |
| 237 | fi->disposalMethod = ((b[0] >> 2) & 7); |
| 238 | if (ExtData[1] & 1) |
| 239 | fi->transpIndex = (short) b[3]; |
| 240 | if (fi->disposalMethod == 3 && info->backupPtr == NULL) { |
| 241 | if (!setupBackupBmp(info,fi->transpIndex)) { |
| 242 | return GIF_ERROR; |
| 243 | } |
| 244 | } |
| 245 | } else if (ExtFunction == COMMENT_EXT_FUNC_CODE) { |
| 246 | if (getComment(ExtData, &info->comment) == GIF_ERROR) { |
| 247 | info->gifFilePtr->Error = D_GIF_ERR_NOT_ENOUGH_MEM; |
| 248 | return GIF_ERROR; |
| 249 | } |
| 250 | } else if (ExtFunction == APPLICATION_EXT_FUNC_CODE && ExtData[0] == 11) { |
| 251 | if (strncmp("NETSCAPE2.0", &ExtData[1], 11) == 0 || strncmp("ANIMEXTS1.0", &ExtData[1], 11) == 0) { |
| 252 | if (DGifGetExtensionNext(info->gifFilePtr, &ExtData, &ExtFunction) == GIF_ERROR) { |
| 253 | return GIF_ERROR; |
| 254 | } |
| 255 | if (ExtFunction == APPLICATION_EXT_FUNC_CODE && ExtData[0] == 3 && ExtData[1] == 1) { |
| 256 | info->loopCount = (unsigned short) (ExtData[2] + (ExtData[3] << 8)); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | return GIF_OK; |
| 261 | } |
| 262 |
| 263 | static int DDGifSlurp(GifFileType *GifFile, GifInfo *info, bool shouldDecode) { |
| 264 | GifRecordType RecordType; |
| 265 | GifByteType *ExtData; |
| 266 | int codeSize; |
| 267 | int ExtFunction; |
| 268 | size_t ImageSize; |
| 269 | do { |
| 270 | if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR) { |
| 271 | return GIF_ERROR; |
| 272 | } |
| 273 | switch (RecordType) { |
| 274 | case IMAGE_DESC_RECORD_TYPE: { |
| 275 | if (DGifGetImageDesc(GifFile, !shouldDecode) == GIF_ERROR) { |
| 276 | return GIF_ERROR; |
| 277 | } |
| 278 | int i = shouldDecode ? info->currentIndex : GifFile->ImageCount - 1; |
| 279 | SavedImage *sp = &GifFile->SavedImages[i]; |
| 280 | ImageSize = sp->ImageDesc.Width * sp->ImageDesc.Height; |
| 281 |
| 282 | if (sp->ImageDesc.Width < 1 || sp->ImageDesc.Height < 1 || ImageSize > (SIZE_MAX / sizeof(GifPixelType))) { |
| 283 | GifFile->Error = D_GIF_ERR_INVALID_IMG_DIMS; |
| 284 | return GIF_ERROR; |
| 285 | } |
| 286 | if (sp->ImageDesc.Width > GifFile->SWidth || sp->ImageDesc.Height > GifFile->SHeight) { |
| 287 | GifFile->Error = D_GIF_ERR_IMG_NOT_CONFINED; |
| 288 | return GIF_ERROR; |
| 289 | } |
| 290 | if (shouldDecode) { |
| 291 | sp->RasterBits = info->rasterBits; |
| 292 | if (sp->ImageDesc.Interlace) { |
| 293 | int i, j; |
| 294 | int InterlacedOffset[] = { 0, 4, 2, 1 }; |
| 295 | int InterlacedJumps[] = { 8, 8, 4, 2 }; |
| 296 | for (i = 0; i < 4; i++) { |
| 297 | for (j = InterlacedOffset[i]; j < sp->ImageDesc.Height; j += InterlacedJumps[i]) { |
| 298 | if (DGifGetLine(GifFile, sp->RasterBits + j * sp->ImageDesc.Width, sp->ImageDesc.Width) == GIF_ERROR) { |
| 299 | return GIF_ERROR; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } else { |
| 304 | if (DGifGetLine(GifFile, sp->RasterBits, ImageSize) == GIF_ERROR) { |
| 305 | return GIF_ERROR; |
| 306 | } |
| 307 | } |
| 308 | if (info->currentIndex >= GifFile->ImageCount - 1) { |
| 309 | if (info->loopCount > 0) |
| 310 | info->currentLoop++; |
| 311 | if (fileRewindFun(info) != 0) { |
| 312 | info->gifFilePtr->Error = D_GIF_ERR_READ_FAILED; |
| 313 | return GIF_ERROR; |
| 314 | } |
| 315 | } |
| 316 | return GIF_OK; |
| 317 | } else { |
| 318 | if (DGifGetCode(GifFile, &codeSize, &ExtData) == GIF_ERROR) { |
| 319 | return GIF_ERROR; |
| 320 | } |
| 321 | while (ExtData) { |
| 322 | if (DGifGetCodeNext(GifFile, &ExtData) == GIF_ERROR) { |
| 323 | return GIF_ERROR; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | break; |
| 328 | } |
| 329 |
| 330 | case EXTENSION_RECORD_TYPE: { |
| 331 | if (DGifGetExtension(GifFile, &ExtFunction, &ExtData) == GIF_ERROR) { |
| 332 | return GIF_ERROR; |
| 333 | } |
| 334 |
| 335 | if (!shouldDecode) { |
| 336 | info->infos = realloc(info->infos, (GifFile->ImageCount + 1) * sizeof(FrameInfo)); |
| 337 | if (readExtensions(ExtFunction, ExtData, info) == GIF_ERROR) { |
| 338 | return GIF_ERROR; |
| 339 | } |
| 340 | } |
| 341 | while (ExtData) { |
| 342 | if (DGifGetExtensionNext(GifFile, &ExtData, &ExtFunction) == GIF_ERROR) { |
| 343 | return GIF_ERROR; |
| 344 | } |
| 345 | if (!shouldDecode) { |
| 346 | if (readExtensions(ExtFunction, ExtData, info) == GIF_ERROR) { |
| 347 | return GIF_ERROR; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | break; |
| 352 | } |
| 353 | case TERMINATE_RECORD_TYPE: |
| 354 | default: |
| 355 | break; |
| 356 | } |
| 357 | } while (RecordType != TERMINATE_RECORD_TYPE); |
| 358 |
| 359 | bool ok = true; |
| 360 | if (shouldDecode) { |
| 361 | ok = (fileRewindFun(info) == 0); |
| 362 | } |
| 363 | if (ok) { |
| 364 | return GIF_OK; |
| 365 | } else { |
| 366 | info->gifFilePtr->Error = D_GIF_ERR_READ_FAILED; |
| 367 | return GIF_ERROR; |
| 368 | } |
| 369 | } |
| 370 |
| 371 | static void copyLine(argb *dst, const unsigned char *src, const ColorMapObject *cmap, int transparent, int width) { |
| 372 | for (; width > 0; width--, src++, dst++) { |
| 373 | if (*src != transparent) { |
| 374 | getColorFromTable(*src, dst, cmap); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 |
| 379 | static argb *getAddr(argb *bm, int width, int left, int top) { |
| 380 | return bm + top * width + left; |
| 381 | } |
| 382 |
| 383 | static void blitNormal(argb *bm, int width, int height, const SavedImage *frame, const ColorMapObject *cmap, int transparent) { |
| 384 | const unsigned char *src = (unsigned char *)frame->RasterBits; |
| 385 | argb *dst = getAddr(bm, width, frame->ImageDesc.Left, frame->ImageDesc.Top); |
| 386 | GifWord copyWidth = frame->ImageDesc.Width; |
| 387 | if (frame->ImageDesc.Left + copyWidth > width) { |
| 388 | copyWidth = width - frame->ImageDesc.Left; |
| 389 | } |
| 390 |
| 391 | GifWord copyHeight = frame->ImageDesc.Height; |
| 392 | if (frame->ImageDesc.Top + copyHeight > height) { |
| 393 | copyHeight = height - frame->ImageDesc.Top; |
| 394 | } |
| 395 |
| 396 | int srcPad, dstPad; |
| 397 | dstPad = width - copyWidth; |
| 398 | srcPad = frame->ImageDesc.Width - copyWidth; |
| 399 | for (; copyHeight > 0; copyHeight--) { |
| 400 | copyLine(dst, src, cmap, transparent, copyWidth); |
| 401 | src += frame->ImageDesc.Width; |
| 402 | dst += width; |
| 403 | } |
| 404 | } |
| 405 |
| 406 | static void fillRect(argb *bm, int bmWidth, int bmHeight, GifWord left, GifWord top, GifWord width, GifWord height, argb col) { |
| 407 | uint32_t *dst = (uint32_t *)getAddr(bm, bmWidth, left, top); |
| 408 | GifWord copyWidth = width; |
| 409 | if (left + copyWidth > bmWidth) { |
| 410 | copyWidth = bmWidth - left; |
| 411 | } |
| 412 |
| 413 | GifWord copyHeight = height; |
| 414 | if (top + copyHeight > bmHeight) { |
| 415 | copyHeight = bmHeight - top; |
| 416 | } |
| 417 | uint32_t *pColor = (uint32_t *)(&col); |
| 418 | for (; copyHeight > 0; copyHeight--) { |
| 419 | memset(dst, *pColor, copyWidth * sizeof(argb)); |
| 420 | dst += bmWidth; |
| 421 | } |
| 422 | } |
| 423 |
| 424 | static void drawFrame(argb *bm, int bmWidth, int bmHeight, const SavedImage *frame, const ColorMapObject *cmap, short transpIndex) { |
| 425 | if (frame->ImageDesc.ColorMap != NULL) { |
| 426 | cmap = frame->ImageDesc.ColorMap; |
| 427 | if (cmap == NULL || cmap->ColorCount != (1 << cmap->BitsPerPixel)) { |
| 428 | cmap = defaultCmap; |
| 429 | } |
| 430 | } |
| 431 |
| 432 | blitNormal(bm, bmWidth, bmHeight, frame, cmap, (int) transpIndex); |
| 433 | } |
| 434 |
| 435 | static bool checkIfCover(const SavedImage *target, const SavedImage *covered) { |
| 436 | if (target->ImageDesc.Left <= covered->ImageDesc.Left |
| 437 | && covered->ImageDesc.Left + covered->ImageDesc.Width |
| 438 | <= target->ImageDesc.Left + target->ImageDesc.Width |
| 439 | && target->ImageDesc.Top <= covered->ImageDesc.Top |
| 440 | && covered->ImageDesc.Top + covered->ImageDesc.Height |
| 441 | <= target->ImageDesc.Top + target->ImageDesc.Height) { |
| 442 | return true; |
| 443 | } |
| 444 | return false; |
| 445 | } |
| 446 |
| 447 | static inline void disposeFrameIfNeeded(argb *bm, GifInfo *info, unsigned int idx) { |
| 448 | argb *backup = info->backupPtr; |
| 449 | argb color; |
| 450 | packARGB32(&color, 0, 0, 0, 0); |
| 451 | GifFileType *fGif = info->gifFilePtr; |
| 452 | SavedImage *cur = &fGif->SavedImages[idx - 1]; |
| 453 | SavedImage *next = &fGif->SavedImages[idx]; |
| 454 | bool curTrans = info->infos[idx - 1].transpIndex != -1; |
| 455 | int curDisposal = info->infos[idx - 1].disposalMethod; |
| 456 | bool nextTrans = info->infos[idx].transpIndex != -1; |
| 457 | int nextDisposal = info->infos[idx].disposalMethod; |
| 458 |
| 459 | argb *tmp; |
| 460 | if ((curDisposal == 2 || curDisposal == 3) && (nextTrans || !checkIfCover(next, cur))) { |
| 461 | switch (curDisposal) { |
| 462 | case 2: { |
| 463 | fillRect(bm, fGif->SWidth, fGif->SHeight, cur->ImageDesc.Left, cur->ImageDesc.Top, cur->ImageDesc.Width, cur->ImageDesc.Height, color); |
| 464 | } |
| 465 | break; |
| 466 |
| 467 | case 3: { |
| 468 | tmp = bm; |
| 469 | bm = backup; |
| 470 | backup = tmp; |
| 471 | } |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 |
| 476 | if (nextDisposal == 3) { |
| 477 | memcpy(backup, bm, fGif->SWidth * fGif->SHeight * sizeof(argb)); |
| 478 | } |
| 479 | } |
| 480 |
| 481 | static jboolean reset(GifInfo *info) { |
| 482 | if (fileRewindFun(info) != 0) { |
| 483 | return JNI_FALSE; |
| 484 | } |
| 485 | info->nextStartTime = 0; |
| 486 | info->currentLoop = -1; |
| 487 | info->currentIndex = -1; |
| 488 | return JNI_TRUE; |
| 489 | } |
| 490 |
| 491 | static void getBitmap(argb *bm, GifInfo *info) { |
| 492 | GifFileType *fGIF = info->gifFilePtr; |
| 493 |
| 494 | argb paintingColor; |
| 495 | int i = info->currentIndex; |
| 496 | if (DDGifSlurp(fGIF, info, true) == GIF_ERROR) { |
| 497 | return; //TODO add leniency support |
| 498 | } |
| 499 | SavedImage *cur = &fGIF->SavedImages[i]; |
| 500 |
| 501 | short transpIndex = info->infos[i].transpIndex; |
| 502 | if (i == 0) { |
| 503 | if (transpIndex == -1) { |
| 504 | getColorFromTable(fGIF->SBackGroundColor, &paintingColor, fGIF->SColorMap); |
| 505 | } else { |
| 506 | packARGB32(&paintingColor, 0, 0, 0, 0); |
| 507 | } |
| 508 | eraseColor(bm, fGIF->SWidth, fGIF->SHeight, paintingColor); |
| 509 | } else { |
| 510 | disposeFrameIfNeeded(bm, info, i); |
| 511 | } |
| 512 | drawFrame(bm, fGIF->SWidth, fGIF->SHeight, cur, fGIF->SColorMap, transpIndex); |
| 513 | } |
| 514 |
| 515 | static void setMetaData(int width, int height, int ImageCount, int errorCode, JNIEnv *env, jintArray metaData) { |
| 516 | jint *ints = (*env)->GetIntArrayElements(env, metaData, 0); |
| 517 | *ints++ = width; |
| 518 | *ints++ = height; |
| 519 | *ints++ = ImageCount; |
| 520 | *ints = errorCode; |
| 521 | (*env)->ReleaseIntArrayElements(env, metaData, ints, 0); |
| 522 | } |
| 523 |
| 524 | static jint open(GifFileType *GifFileIn, int Error, int startPos, JNIEnv *env, jintArray metaData) { |
| 525 | if (startPos < 0) { |
| 526 | Error = D_GIF_ERR_NOT_READABLE; |
| 527 | DGifCloseFile(GifFileIn); |
| 528 | } |
| 529 | if (Error != 0 || GifFileIn == NULL) { |
| 530 | setMetaData(0, 0, 0, Error, env, metaData); |
| 531 | return (jint) NULL; |
| 532 | } |
| 533 | int width = GifFileIn->SWidth, height = GifFileIn->SHeight; |
| 534 | unsigned int wxh = width * height; |
| 535 | if (wxh < 1 || wxh > INT_MAX) { |
| 536 | DGifCloseFile(GifFileIn); |
| 537 | setMetaData(width, height, 0, D_GIF_ERR_INVALID_SCR_DIMS, env, metaData); |
| 538 | return (jint) NULL; |
| 539 | } |
| 540 | GifInfo *info = malloc(sizeof(GifInfo)); |
| 541 | if (info == NULL) { |
| 542 | DGifCloseFile(GifFileIn); |
| 543 | setMetaData(width, height, 0, D_GIF_ERR_NOT_ENOUGH_MEM, env, metaData); |
| 544 | return (jint) NULL; |
| 545 | } |
| 546 | info->gifFilePtr = GifFileIn; |
| 547 | info->startPos = startPos; |
| 548 | info->currentIndex = -1; |
| 549 | info->nextStartTime = 0; |
| 550 | info->lastFrameReaminder = ULONG_MAX; |
| 551 | info->comment = NULL; |
| 552 | info->loopCount = 0; |
| 553 | info->currentLoop = -1; |
| 554 | info->speedFactor = 1.0; |
| 555 | info->rasterBits = calloc(GifFileIn->SHeight * GifFileIn->SWidth, sizeof(GifPixelType)); |
| 556 | info->infos = malloc(sizeof(FrameInfo)); |
| 557 | info->infos->duration = 0; |
| 558 | info->infos->disposalMethod = 0; |
| 559 | info->infos->transpIndex = -1; |
| 560 | info->backupPtr = NULL; |
| 561 |
| 562 | if (info->rasterBits == NULL || info->infos == NULL) { |
| 563 | cleanUp(info); |
| 564 | setMetaData(width, height, 0, D_GIF_ERR_NOT_ENOUGH_MEM, env, metaData); |
| 565 | return (jint) NULL; |
| 566 | } |
| 567 |
| 568 | if (DDGifSlurp(GifFileIn, info, false) == GIF_ERROR) { |
| 569 | Error = GifFileIn->Error; |
| 570 | } |
| 571 | if (GifFileIn->SColorMap == NULL || GifFileIn->SColorMap->ColorCount != (1 << GifFileIn->SColorMap->BitsPerPixel)) { |
| 572 | GifFreeMapObject(GifFileIn->SColorMap); |
| 573 | GifFileIn->SColorMap = defaultCmap; |
| 574 | } |
| 575 | int imgCount = GifFileIn->ImageCount; |
| 576 | if (imgCount < 1) { |
| 577 | Error = D_GIF_ERR_NO_FRAMES; |
| 578 | } |
| 579 | if (fileRewindFun(info) != 0) { |
| 580 | Error = D_GIF_ERR_READ_FAILED; |
| 581 | } |
| 582 | if (Error != 0) { |
| 583 | cleanUp(info); |
| 584 | } |
| 585 | setMetaData(width, height, imgCount, Error, env, metaData); |
| 586 |
| 587 | return (jint)(Error == 0 ? info : NULL); |
| 588 | } |
| 589 |
| 590 | JNIEXPORT jlong JNICALL Java_org_telegram_ui_Views_GifDrawable_getAllocationByteCount(JNIEnv *env, jclass class, jobject gifInfo) { |
| 591 | GifInfo *info = (GifInfo *)gifInfo; |
| 592 | if (info == NULL) { |
| 593 | return 0; |
| 594 | } |
| 595 | unsigned int pxCount = info->gifFilePtr->SWidth + info->gifFilePtr->SHeight; |
| 596 | jlong sum = pxCount * sizeof(char); |
| 597 | if (info->backupPtr != NULL) { |
| 598 | sum += pxCount * sizeof(argb); |
| 599 | } |
| 600 | return sum; |
| 601 | } |
| 602 |
| 603 | JNIEXPORT jboolean JNICALL Java_org_telegram_ui_Views_GifDrawable_reset(JNIEnv *env, jclass class, jobject gifInfo) { |
| 604 | GifInfo *info = (GifInfo *)gifInfo; |
| 605 | if (info == NULL) { |
| 606 | return JNI_FALSE; |
| 607 | } |
| 608 | return reset(info); |
| 609 | } |
| 610 |
| 611 | JNIEXPORT void JNICALL Java_org_telegram_ui_Views_GifDrawable_setSpeedFactor(JNIEnv *env, jclass class, jobject gifInfo, jfloat factor) { |
| 612 | GifInfo *info = (GifInfo *)gifInfo; |
| 613 | if (info == NULL) { |
| 614 | return; |
| 615 | } |
| 616 | info->speedFactor = factor; |
| 617 | } |
| 618 |
| 619 | JNIEXPORT void JNICALL Java_org_telegram_ui_Views_GifDrawable_seekToTime(JNIEnv *env, jclass class, jobject gifInfo, jint desiredPos, jintArray jPixels) { |
| 620 | GifInfo *info = (GifInfo *)gifInfo; |
| 621 | if (info == NULL) { |
| 622 | return; |
| 623 | } |
| 624 | int imgCount = info->gifFilePtr->ImageCount; |
| 625 | if (imgCount <= 1) { |
| 626 | return; |
| 627 | } |
| 628 |
| 629 | unsigned long sum = 0; |
| 630 | int i; |
| 631 | for (i = 0; i < imgCount; i++) { |
| 632 | unsigned long newSum = sum + info->infos[i].duration; |
| 633 | if (newSum >= desiredPos) { |
| 634 | break; |
| 635 | } |
| 636 | sum = newSum; |
| 637 | } |
| 638 | if (i < info->currentIndex) { |
| 639 | return; |
| 640 | } |
| 641 |
| 642 | unsigned long lastFrameRemainder = desiredPos - sum; |
| 643 | if (i == imgCount - 1 && lastFrameRemainder > info->infos[i].duration) { |
| 644 | lastFrameRemainder = info->infos[i].duration; |
| 645 | } |
| 646 | info->lastFrameReaminder = lastFrameRemainder; |
| 647 | if (i > info->currentIndex) { |
| 648 | jint *pixels = (*env)->GetIntArrayElements(env, jPixels, 0); |
| 649 | while (info->currentIndex <= i) { |
| 650 | info->currentIndex++; |
| 651 | getBitmap((argb *)pixels, info); |
| 652 | } |
| 653 | (*env)->ReleaseIntArrayElements(env, jPixels, pixels, 0); |
| 654 | } |
| 655 | if (info->speedFactor == 1.0) { |
| 656 | info->nextStartTime = getRealTime() + lastFrameRemainder; |
| 657 | } else { |
| 658 | info->nextStartTime = getRealTime() + lastFrameRemainder * info->speedFactor; |
| 659 | } |
| 660 | } |
| 661 |
| 662 | JNIEXPORT void JNICALL Java_org_telegram_ui_Views_GifDrawable_seekToFrame(JNIEnv *env, jclass class, jobject gifInfo, jint desiredIdx, jintArray jPixels) { |
| 663 | GifInfo *info = (GifInfo *)gifInfo; |
| 664 | if (info == NULL) { |
| 665 | return; |
| 666 | } |
| 667 | if (desiredIdx <= info->currentIndex) { |
| 668 | return; |
| 669 | } |
| 670 |
| 671 | int imgCount = info->gifFilePtr->ImageCount; |
| 672 | if (imgCount <= 1) { |
| 673 | return; |
| 674 | } |
| 675 |
| 676 | if (desiredIdx >= imgCount) { |
| 677 | desiredIdx = imgCount - 1; |
| 678 | } |
| 679 |
| 680 | info->lastFrameReaminder = 0; |
| 681 | jint *pixels = (*env)->GetIntArrayElements(env, jPixels, 0); |
| 682 | while (info->currentIndex < desiredIdx) { |
| 683 | info->currentIndex++; |
| 684 | getBitmap((argb *)pixels, info); |
| 685 | } |
| 686 | (*env)->ReleaseIntArrayElements(env, jPixels, pixels, 0); |
| 687 | if (info->speedFactor == 1.0) { |
| 688 | info->nextStartTime = getRealTime() + info->infos[info->currentIndex].duration; |
| 689 | } else { |
| 690 | info->nextStartTime = getRealTime() + info->infos[info->currentIndex].duration * info->speedFactor; |
| 691 | } |
| 692 |
| 693 | } |
| 694 |
| 695 | JNIEXPORT void JNICALL Java_org_telegram_ui_Views_GifDrawable_renderFrame(JNIEnv *env, jclass class, jintArray jPixels, jobject gifInfo, jintArray metaData) { |
| 696 |
| 697 | GifInfo *info = (GifInfo *)gifInfo; |
| 698 | if (info == NULL) { |
| 699 | return; |
| 700 | } |
| 701 |
| 702 | bool needRedraw = false; |
| 703 | unsigned long rt = getRealTime(); |
| 704 |
| 705 | if (rt >= info->nextStartTime && info->currentLoop < info->loopCount) { |
| 706 | if (++info->currentIndex >= info->gifFilePtr->ImageCount) { |
| 707 | info->currentIndex = 0; |
| 708 | } |
| 709 | needRedraw = true; |
| 710 | } |
| 711 | jint *rawMetaData = (*env)->GetIntArrayElements(env, metaData, 0); |
| 712 |
| 713 | if (needRedraw) { |
| 714 | jint *pixels = (*env)->GetIntArrayElements(env, jPixels, 0); |
| 715 | getBitmap((argb *)pixels, info); |
| 716 | rawMetaData[3] = info->gifFilePtr->Error; |
| 717 |
| 718 | (*env)->ReleaseIntArrayElements(env, jPixels, pixels, 0); |
| 719 |
| 720 | int scaledDuration = info->infos[info->currentIndex].duration; |
| 721 | if (info->speedFactor != 1.0) { |
| 722 | scaledDuration /= info->speedFactor; |
| 723 | } |
| 724 | info->nextStartTime = rt + scaledDuration; |
| 725 | rawMetaData[4] = scaledDuration; |
| 726 | } else { |
| 727 | rawMetaData[4] = (int) (rt - info->nextStartTime); |
| 728 | } |
| 729 | (*env)->ReleaseIntArrayElements(env, metaData, rawMetaData, 0); |
| 730 | } |
| 731 |
| 732 | JNIEXPORT void JNICALL Java_org_telegram_ui_Views_GifDrawable_free(JNIEnv *env, jclass class, jobject gifInfo) { |
| 733 | if (gifInfo == NULL) { |
| 734 | return; |
| 735 | } |
| 736 | GifInfo *info = (GifInfo *)gifInfo; |
| 737 | FILE *file = info->gifFilePtr->UserData; |
| 738 | if (file) { |
| 739 | fclose(file); |
| 740 | } |
| 741 | info->gifFilePtr->UserData = NULL; |
| 742 | cleanUp(info); |
| 743 | } |
| 744 |
| 745 | JNIEXPORT jstring JNICALL Java_org_telegram_ui_Views_GifDrawable_getComment(JNIEnv *env, jclass class, jobject gifInfo) { |
| 746 | if (gifInfo == NULL) { |
| 747 | return NULL; |
| 748 | } |
| 749 | GifInfo *info = (GifInfo *)gifInfo; |
| 750 | return (*env)->NewStringUTF(env, info->comment); |
| 751 | } |
| 752 |
| 753 | JNIEXPORT jint JNICALL Java_org_telegram_ui_Views_GifDrawable_getLoopCount(JNIEnv *env, jclass class, jobject gifInfo) { |
| 754 | if (gifInfo == NULL) { |
| 755 | return 0; |
| 756 | } |
| 757 | return ((GifInfo *)gifInfo)->loopCount; |
| 758 | } |
| 759 |
| 760 | JNIEXPORT jint JNICALL Java_org_telegram_ui_Views_GifDrawable_getDuration(JNIEnv *env, jclass class, jobject gifInfo) { |
| 761 | GifInfo *info = (GifInfo *)gifInfo; |
| 762 | if (info == NULL) { |
| 763 | return 0; |
| 764 | } |
| 765 | int i; |
| 766 | unsigned long sum = 0; |
| 767 | for (i = 0; i < info->gifFilePtr->ImageCount; i++) { |
| 768 | sum += info->infos[i].duration; |
| 769 | } |
| 770 | return sum; |
| 771 | } |
| 772 |
| 773 | JNIEXPORT jint JNICALL Java_org_telegram_ui_Views_GifDrawable_getCurrentPosition(JNIEnv *env, jclass class, jobject gifInfo) { |
| 774 | GifInfo *info = (GifInfo *)gifInfo; |
| 775 | if (info == NULL) { |
| 776 | return 0; |
| 777 | } |
| 778 | int idx = info->currentIndex; |
| 779 | if (idx < 0 || info->gifFilePtr->ImageCount <= 1) { |
| 780 | return 0; |
| 781 | } |
| 782 | int i; |
| 783 | unsigned int sum = 0; |
| 784 | for (i = 0; i < idx; i++) { |
| 785 | sum += info->infos[i].duration; |
| 786 | } |
| 787 | unsigned long remainder = info->lastFrameReaminder == ULONG_MAX ? getRealTime() - info->nextStartTime : info->lastFrameReaminder; |
| 788 | return (int) (sum + remainder); |
| 789 | } |
| 790 |
| 791 | JNIEXPORT void JNICALL Java_org_telegram_ui_Views_GifDrawable_saveRemainder(JNIEnv *env, jclass class, jobject gifInfo) { |
| 792 | GifInfo *info = (GifInfo *)gifInfo; |
| 793 | if (info == NULL) { |
| 794 | return; |
| 795 | } |
| 796 | info->lastFrameReaminder = getRealTime() - info->nextStartTime; |
| 797 | if (info->lastFrameReaminder > 0) { |
| 798 | info->lastFrameReaminder = 0; |
| 799 | } |
| 800 | } |
| 801 |
| 802 | JNIEXPORT void JNICALL Java_org_telegram_ui_Views_GifDrawable_restoreRemainder(JNIEnv *env, jclass class, jobject gifInfo) { |
| 803 | GifInfo *info = (GifInfo *)gifInfo; |
| 804 | if (info == NULL || info->lastFrameReaminder == ULONG_MAX) { |
| 805 | return; |
| 806 | } |
| 807 | info->nextStartTime = getRealTime() + info->lastFrameReaminder; |
| 808 | info->lastFrameReaminder = ULONG_MAX; |
| 809 | } |
| 810 |
| 811 | JNIEXPORT jint JNICALL Java_org_telegram_ui_Views_GifDrawable_openFile(JNIEnv *env, jclass class, jintArray metaData, jstring jfname) { |
| 812 | if (jfname == NULL) { |
| 813 | setMetaData(0, 0, 0, D_GIF_ERR_OPEN_FAILED, env, metaData); |
| 814 | return (jint) NULL; |
| 815 | } |
| 816 |
| 817 | const char *fname = (*env)->GetStringUTFChars(env, jfname, 0); |
| 818 | FILE *file = fopen(fname, "rb"); |
| 819 | (*env)->ReleaseStringUTFChars(env, jfname, fname); |
| 820 | if (file == NULL) { |
| 821 | setMetaData(0, 0, 0, D_GIF_ERR_OPEN_FAILED, env, metaData); |
| 822 | return (jint) NULL; |
| 823 | } |
| 824 | int Error = 0; |
| 825 | GifFileType *GifFileIn = DGifOpen(file, &fileReadFunc, &Error); |
| 826 | return open(GifFileIn, Error, ftell(file), env, metaData); |
| 827 | } |
| 828 |