| 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 | int 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 | } |
| 138 | return -1; |
| 139 | } |
| 140 |
| 141 | static void cleanUp(GifInfo *info) { |
| 142 | if (info->backupPtr) { |
| 143 | free(info->backupPtr); |
| 144 | info->backupPtr = NULL; |
| 145 | } |
| 146 | if (info->infos) { |
| 147 | free(info->infos); |
| 148 | info->infos = NULL; |
| 149 | } |
| 150 | if (info->rasterBits) { |
| 151 | free(info->rasterBits); |
| 152 | info->rasterBits = NULL; |
| 153 | } |
| 154 | if (info->comment) { |
| 155 | free(info->comment); |
| 156 | info->comment = NULL; |
| 157 | } |
| 158 |
| 159 | GifFileType *GifFile = info->gifFilePtr; |
| 160 | if (GifFile->SColorMap == defaultCmap) { |
| 161 | GifFile->SColorMap = NULL; |
| 162 | } |
| 163 | if (GifFile->SavedImages != NULL) { |
| 164 | SavedImage *sp; |
| 165 | for (sp = GifFile->SavedImages; sp < GifFile->SavedImages + GifFile->ImageCount; sp++) { |
| 166 | if (sp->ImageDesc.ColorMap != NULL) { |
| 167 | GifFreeMapObject(sp->ImageDesc.ColorMap); |
| 168 | sp->ImageDesc.ColorMap = NULL; |
| 169 | } |
| 170 | } |
| 171 | free(GifFile->SavedImages); |
| 172 | GifFile->SavedImages = NULL; |
| 173 | } |
| 174 | DGifCloseFile(GifFile); |
| 175 | free(info); |
| 176 | } |
| 177 |
| 178 | static int getComment(GifByteType *Bytes, char **cmt) { |
| 179 | unsigned int len = (unsigned int) Bytes[0]; |
| 180 | unsigned int offset = *cmt != NULL ? strlen(*cmt) : 0; |
| 181 | char *ret = realloc(*cmt, (len + offset + 1) * sizeof(char)); |
| 182 | if (ret != NULL) { |
| 183 | memcpy(ret + offset, &Bytes[1], len); |
| 184 | ret[len + offset] = 0; |
| 185 | *cmt = ret; |
| 186 | return GIF_OK; |
| 187 | } |
| 188 | return GIF_ERROR; |
| 189 | } |
| 190 |
| 191 | static void packARGB32(argb *pixel, GifByteType alpha, GifByteType red, GifByteType green, GifByteType blue) { |
| 192 | pixel->alpha = alpha; |
| 193 | pixel->red = red; |
| 194 | pixel->green = green; |
| 195 | pixel->blue = blue; |
| 196 | } |
| 197 |
| 198 | static void getColorFromTable(int idx, argb *dst, const ColorMapObject *cmap) { |
| 199 | int colIdx = (idx >= cmap->ColorCount) ? 0 : idx; |
| 200 | GifColorType *col = &cmap->Colors[colIdx]; |
| 201 | packARGB32(dst, 0xFF, col->Red, col->Green, col->Blue); |
| 202 | } |
| 203 |
| 204 | static void eraseColor(argb *bm, int w, int h, argb color) { |
| 205 | int i; |
| 206 | for (i = 0; i < w * h; i++) { |
| 207 | *(bm + i) = color; |
| 208 | } |
| 209 | } |
| 210 |
| 211 | static inline bool setupBackupBmp(GifInfo *info, short transpIndex) { |
| 212 | GifFileType *fGIF = info->gifFilePtr; |
| 213 | info->backupPtr = calloc(fGIF->SWidth * fGIF->SHeight, sizeof(argb)); |
| 214 | if (!info->backupPtr) { |
| 215 | info->gifFilePtr->Error = D_GIF_ERR_NOT_ENOUGH_MEM; |
| 216 | return false; |
| 217 | } |
| 218 | argb paintingColor; |
| 219 | if (transpIndex == -1) { |
| 220 | getColorFromTable(fGIF->SBackGroundColor, &paintingColor, fGIF->SColorMap); |
| 221 | } else { |
| 222 | packARGB32(&paintingColor, 0, 0, 0, 0); |
| 223 | } |
| 224 | eraseColor(info->backupPtr, fGIF->SWidth, fGIF->SHeight, paintingColor); |
| 225 | return true; |
| 226 | } |
| 227 |
| 228 | static int readExtensions(int ExtFunction, GifByteType *ExtData, GifInfo *info) { |
| 229 | if (ExtData == NULL) { |
| 230 | return GIF_OK; |
| 231 | } |
| 232 | if (ExtFunction == GRAPHICS_EXT_FUNC_CODE && ExtData[0] == 4) { |
| 233 | FrameInfo *fi = &info->infos[info->gifFilePtr->ImageCount]; |
| 234 | fi->transpIndex = -1; |
| 235 | char *b = (char*) ExtData + 1; |
| 236 | short delay = ((b[2] << 8) | b[1]); |
| 237 | fi->duration = delay > 1 ? delay * 10 : 100; |
| 238 | fi->disposalMethod = ((b[0] >> 2) & 7); |
| 239 | if (ExtData[1] & 1) { |
| 240 | fi->transpIndex = 0xff & b[3]; |
| 241 | } |
| 242 | if (fi->disposalMethod == 3 && info->backupPtr == NULL) { |
| 243 | if (!setupBackupBmp(info, fi->transpIndex)) { |
| 244 | return GIF_ERROR; |
| 245 | } |
| 246 | } |
| 247 | } else if (ExtFunction == COMMENT_EXT_FUNC_CODE) { |
| 248 | if (getComment(ExtData, &info->comment) == GIF_ERROR) { |
| 249 | info->gifFilePtr->Error = D_GIF_ERR_NOT_ENOUGH_MEM; |
| 250 | return GIF_ERROR; |
| 251 | } |
| 252 | } else if (ExtFunction == APPLICATION_EXT_FUNC_CODE && ExtData[0] == 11) { |
| 253 | if (strncmp("NETSCAPE2.0", &ExtData[1], 11) == 0 || strncmp("ANIMEXTS1.0", &ExtData[1], 11) == 0) { |
| 254 | if (DGifGetExtensionNext(info->gifFilePtr, &ExtData, &ExtFunction) == GIF_ERROR) { |
| 255 | return GIF_ERROR; |
| 256 | } |
| 257 | if (ExtFunction == APPLICATION_EXT_FUNC_CODE && ExtData[0] == 3 && ExtData[1] == 1) { |
| 258 | info->loopCount = (unsigned short) (ExtData[2] + (ExtData[3] << 8)); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | return GIF_OK; |
| 263 | } |
| 264 |
| 265 | static int DDGifSlurp(GifFileType *GifFile, GifInfo* info, bool shouldDecode) { |
| 266 | GifRecordType RecordType; |
| 267 | GifByteType *ExtData; |
| 268 | int codeSize; |
| 269 | int ExtFunction; |
| 270 | size_t ImageSize; |
| 271 | do { |
| 272 | if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR) { |
| 273 | return (GIF_ERROR); |
| 274 | } |
| 275 | switch (RecordType) { |
| 276 | case IMAGE_DESC_RECORD_TYPE: |
| 277 |
| 278 | if (DGifGetImageDesc(GifFile, !shouldDecode) == GIF_ERROR) { |
| 279 | return (GIF_ERROR); |
| 280 | } |
| 281 | int i = shouldDecode ? info->currentIndex : GifFile->ImageCount - 1; |
| 282 | SavedImage *sp = &GifFile->SavedImages[i]; |
| 283 | ImageSize = sp->ImageDesc.Width * sp->ImageDesc.Height; |
| 284 |
| 285 | if (sp->ImageDesc.Width < 1 || sp->ImageDesc.Height < 1 || ImageSize > (SIZE_MAX / sizeof(GifPixelType))) { |
| 286 | GifFile->Error = D_GIF_ERR_INVALID_IMG_DIMS; |
| 287 | return GIF_ERROR; |
| 288 | } |
| 289 | if (sp->ImageDesc.Width > GifFile->SWidth || sp->ImageDesc.Height > GifFile->SHeight) { |
| 290 | GifFile->Error = D_GIF_ERR_IMG_NOT_CONFINED; |
| 291 | return GIF_ERROR; |
| 292 | } |
| 293 | if (shouldDecode) { |
| 294 | sp->RasterBits = info->rasterBits; |
| 295 | if (sp->ImageDesc.Interlace) { |
| 296 | int i, j; |
| 297 | int InterlacedOffset[] = { 0, 4, 2, 1 }; |
| 298 | int InterlacedJumps[] = { 8, 8, 4, 2 }; |
| 299 | for (i = 0; i < 4; i++) { |
| 300 | for (j = InterlacedOffset[i]; j < sp->ImageDesc.Height; j += InterlacedJumps[i]) { |
| 301 | if (DGifGetLine(GifFile, sp->RasterBits + j * sp->ImageDesc.Width, sp->ImageDesc.Width) == GIF_ERROR) { |
| 302 | return GIF_ERROR; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } else { |
| 307 | if (DGifGetLine(GifFile, sp->RasterBits, ImageSize) == GIF_ERROR) { |
| 308 | return (GIF_ERROR); |
| 309 | } |
| 310 | } |
| 311 | if (info->currentIndex >= GifFile->ImageCount - 1) { |
| 312 | if (info->loopCount > 0) { |
| 313 | info->currentLoop++; |
| 314 | } |
| 315 | if (fileRewindFun(info) != 0) { |
| 316 | info->gifFilePtr->Error = D_GIF_ERR_READ_FAILED; |
| 317 | return GIF_ERROR; |
| 318 | } |
| 319 | } |
| 320 | return GIF_OK; |
| 321 | } else { |
| 322 | if (DGifGetCode(GifFile, &codeSize, &ExtData) == GIF_ERROR) { |
| 323 | return (GIF_ERROR); |
| 324 | } |
| 325 | while (ExtData != NULL) { |
| 326 | if (DGifGetCodeNext(GifFile, &ExtData) == GIF_ERROR) { |
| 327 | return (GIF_ERROR); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | break; |
| 332 |
| 333 | case EXTENSION_RECORD_TYPE: |
| 334 | if (DGifGetExtension(GifFile, &ExtFunction, &ExtData) == GIF_ERROR) { |
| 335 | return (GIF_ERROR); |
| 336 | } |
| 337 |
| 338 | if (!shouldDecode) { |
| 339 | FrameInfo *tmpInfos = realloc(info->infos, (GifFile->ImageCount + 1) * sizeof(FrameInfo)); |
| 340 | if (tmpInfos == NULL) { |
| 341 | return GIF_ERROR; |
| 342 | } |
| 343 | info->infos = tmpInfos; |
| 344 | if (readExtensions(ExtFunction, ExtData, info) == GIF_ERROR) { |
| 345 | return GIF_ERROR; |
| 346 | } |
| 347 | } |
| 348 | while (ExtData != NULL) { |
| 349 | if (DGifGetExtensionNext(GifFile, &ExtData, &ExtFunction) == GIF_ERROR) { |
| 350 | return (GIF_ERROR); |
| 351 | } |
| 352 | if (!shouldDecode) { |
| 353 | if (readExtensions(ExtFunction, ExtData, info) == GIF_ERROR) { |
| 354 | return GIF_ERROR; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | break; |
| 359 |
| 360 | case TERMINATE_RECORD_TYPE: |
| 361 | break; |
| 362 |
| 363 | default: |
| 364 | break; |
| 365 | } |
| 366 | } while (RecordType != TERMINATE_RECORD_TYPE); |
| 367 | bool ok = true; |
| 368 | if (shouldDecode) { |
| 369 | ok = (fileRewindFun(info) == 0); |
| 370 | } |
| 371 | if (ok) { |
| 372 | return (GIF_OK); |
| 373 | } else { |
| 374 | info->gifFilePtr->Error = D_GIF_ERR_READ_FAILED; |
| 375 | return (GIF_ERROR); |
| 376 | } |
| 377 | } |
| 378 |
| 379 | static void copyLine(argb *dst, const unsigned char *src, const ColorMapObject *cmap, int transparent, int width) { |
| 380 | for (; width > 0; width--, src++, dst++) { |
| 381 | if (*src != transparent) { |
| 382 | getColorFromTable(*src, dst, cmap); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 |
| 387 | static argb *getAddr(argb *bm, int width, int left, int top) { |
| 388 | return bm + top * width + left; |
| 389 | } |
| 390 |
| 391 | static void blitNormal(argb *bm, int width, int height, const SavedImage *frame, const ColorMapObject *cmap, int transparent) { |
| 392 | const unsigned char* src = (unsigned char*) frame->RasterBits; |
| 393 | argb *dst = getAddr(bm, width, frame->ImageDesc.Left, frame->ImageDesc.Top); |
| 394 | GifWord copyWidth = frame->ImageDesc.Width; |
| 395 | if (frame->ImageDesc.Left + copyWidth > width) { |
| 396 | copyWidth = width - frame->ImageDesc.Left; |
| 397 | } |
| 398 |
| 399 | GifWord copyHeight = frame->ImageDesc.Height; |
| 400 | if (frame->ImageDesc.Top + copyHeight > height) { |
| 401 | copyHeight = height - frame->ImageDesc.Top; |
| 402 | } |
| 403 |
| 404 | for (; copyHeight > 0; copyHeight--) { |
| 405 | copyLine(dst, src, cmap, transparent, copyWidth); |
| 406 | src += frame->ImageDesc.Width; |
| 407 | dst += width; |
| 408 | } |
| 409 | } |
| 410 |
| 411 | static void fillRect(argb *bm, int bmWidth, int bmHeight, GifWord left, GifWord top, GifWord width, GifWord height, argb col) { |
| 412 | uint32_t* dst = (uint32_t*) getAddr(bm, bmWidth, left, top); |
| 413 | GifWord copyWidth = width; |
| 414 | if (left + copyWidth > bmWidth) { |
| 415 | copyWidth = bmWidth - left; |
| 416 | } |
| 417 |
| 418 | GifWord copyHeight = height; |
| 419 | if (top + copyHeight > bmHeight) { |
| 420 | copyHeight = bmHeight - top; |
| 421 | } |
| 422 | uint32_t* pColor = (uint32_t *) (&col); |
| 423 | for (; copyHeight > 0; copyHeight--) { |
| 424 | memset(dst, *pColor, copyWidth * sizeof(argb)); |
| 425 | dst += bmWidth; |
| 426 | } |
| 427 | } |
| 428 |
| 429 | static void drawFrame(argb *bm, int bmWidth, int bmHeight, const SavedImage *frame, const ColorMapObject *cmap, short transpIndex) { |
| 430 | if (frame->ImageDesc.ColorMap != NULL) { |
| 431 | cmap = frame->ImageDesc.ColorMap; |
| 432 | if (cmap->ColorCount != (1 << cmap->BitsPerPixel)) { |
| 433 | cmap = defaultCmap; |
| 434 | } |
| 435 | } |
| 436 | blitNormal(bm, bmWidth, bmHeight, frame, cmap, transpIndex); |
| 437 | } |
| 438 |
| 439 | static bool checkIfCover(const SavedImage *target, const SavedImage *covered) { |
| 440 | if (target->ImageDesc.Left <= covered->ImageDesc.Left |
| 441 | && covered->ImageDesc.Left + covered->ImageDesc.Width |
| 442 | <= target->ImageDesc.Left + target->ImageDesc.Width |
| 443 | && target->ImageDesc.Top <= covered->ImageDesc.Top |
| 444 | && covered->ImageDesc.Top + covered->ImageDesc.Height |
| 445 | <= target->ImageDesc.Top + target->ImageDesc.Height) { |
| 446 | return true; |
| 447 | } |
| 448 | return false; |
| 449 | } |
| 450 |
| 451 | static inline void disposeFrameIfNeeded(argb *bm, GifInfo *info, unsigned int idx) { |
| 452 | argb* backup = info->backupPtr; |
| 453 | argb color; |
| 454 | packARGB32(&color, 0, 0, 0, 0); |
| 455 | GifFileType *fGif = info->gifFilePtr; |
| 456 | SavedImage* cur = &fGif->SavedImages[idx - 1]; |
| 457 | SavedImage* next = &fGif->SavedImages[idx]; |
| 458 | bool curTrans = info->infos[idx - 1].transpIndex != -1; |
| 459 | int curDisposal = info->infos[idx - 1].disposalMethod; |
| 460 | bool nextTrans = info->infos[idx].transpIndex != -1; |
| 461 | int nextDisposal = info->infos[idx].disposalMethod; |
| 462 | argb *tmp; |
| 463 | if ((curDisposal == 2 || curDisposal == 3) && (nextTrans || !checkIfCover(next, cur))) { |
| 464 | switch (curDisposal) { |
| 465 | case 2: |
| 466 |
| 467 | fillRect(bm, fGif->SWidth, fGif->SHeight, cur->ImageDesc.Left, cur->ImageDesc.Top, cur->ImageDesc.Width, cur->ImageDesc.Height, color); |
| 468 | break; |
| 469 |
| 470 | case 3: |
| 471 | tmp = bm; |
| 472 | bm = backup; |
| 473 | backup = tmp; |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 |
| 478 | if (nextDisposal == 3) { |
| 479 | memcpy(backup, bm, fGif->SWidth * fGif->SHeight * sizeof(argb)); |
| 480 | } |
| 481 | } |
| 482 |
| 483 | static void reset(GifInfo *info) { |
| 484 | if (fileRewindFun(info) != 0) { |
| 485 | return; |
| 486 | } |
| 487 | info->nextStartTime = 0; |
| 488 | info->currentLoop = -1; |
| 489 | info->currentIndex = -1; |
| 490 | } |
| 491 |
| 492 | static void getBitmap(argb *bm, GifInfo *info) { |
| 493 | GifFileType* fGIF = info->gifFilePtr; |
| 494 |
| 495 | argb paintingColor; |
| 496 | int i = info->currentIndex; |
| 497 | if (DDGifSlurp(fGIF, info, true) == GIF_ERROR) { |
| 498 | return; |
| 499 | } |
| 500 | SavedImage* cur = &fGIF->SavedImages[i]; |
| 501 | int 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 *const ints = (*env)->GetIntArrayElements(env, metaData, 0); |
| 517 | if (ints == NULL) { |
| 518 | return; |
| 519 | } |
| 520 | ints[0] = width; |
| 521 | ints[1] = height; |
| 522 | ints[2] = ImageCount; |
| 523 | ints[3] = errorCode; |
| 524 | (*env)->ReleaseIntArrayElements(env, metaData, ints, 0); |
| 525 | } |
| 526 |
| 527 | static jint open(GifFileType *GifFileIn, int Error, int startPos, JNIEnv *env, jintArray metaData) { |
| 528 | if (startPos < 0) { |
| 529 | Error = D_GIF_ERR_NOT_READABLE; |
| 530 | DGifCloseFile(GifFileIn); |
| 531 | } |
| 532 | if (Error != 0 || GifFileIn == NULL) { |
| 533 | setMetaData(0, 0, 0, Error, env, metaData); |
| 534 | return (jint) NULL; |
| 535 | } |
| 536 | int width = GifFileIn->SWidth, height = GifFileIn->SHeight; |
| 537 | unsigned int wxh = width * height; |
| 538 | if (wxh < 1 || wxh > INT_MAX) { |
| 539 | DGifCloseFile(GifFileIn); |
| 540 | setMetaData(width, height, 0, D_GIF_ERR_INVALID_SCR_DIMS, env, metaData); |
| 541 | return (jint) NULL; |
| 542 | } |
| 543 | GifInfo *info = malloc(sizeof(GifInfo)); |
| 544 | if (info == NULL) { |
| 545 | DGifCloseFile(GifFileIn); |
| 546 | setMetaData(width, height, 0, D_GIF_ERR_NOT_ENOUGH_MEM, env, metaData); |
| 547 | return (jint) NULL; |
| 548 | } |
| 549 | info->gifFilePtr = GifFileIn; |
| 550 | info->startPos = startPos; |
| 551 | info->currentIndex = -1; |
| 552 | info->nextStartTime = 0; |
| 553 | info->lastFrameReaminder = ULONG_MAX; |
| 554 | info->comment = NULL; |
| 555 | info->loopCount = 0; |
| 556 | info->currentLoop = -1; |
| 557 | info->speedFactor = 1.0; |
| 558 | info->rasterBits = calloc(GifFileIn->SHeight * GifFileIn->SWidth, sizeof(GifPixelType)); |
| 559 | info->infos = malloc(sizeof(FrameInfo)); |
| 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 | info->infos->duration = 0; |
| 568 | info->infos->disposalMethod = 0; |
| 569 | info->infos->transpIndex = -1; |
| 570 | if (GifFileIn->SColorMap == NULL || GifFileIn->SColorMap->ColorCount != (1 << GifFileIn->SColorMap->BitsPerPixel)) { |
| 571 | GifFreeMapObject(GifFileIn->SColorMap); |
| 572 | GifFileIn->SColorMap = defaultCmap; |
| 573 | } |
| 574 |
| 575 | DDGifSlurp(GifFileIn, info, false); |
| 576 |
| 577 | int imgCount = GifFileIn->ImageCount; |
| 578 |
| 579 | if (imgCount < 1) { |
| 580 | Error = D_GIF_ERR_NO_FRAMES; |
| 581 | } |
| 582 | if (fileRewindFun(info) != 0) { |
| 583 | Error = D_GIF_ERR_READ_FAILED; |
| 584 | } |
| 585 | if (Error != 0) { |
| 586 | cleanUp(info); |
| 587 | } |
| 588 | setMetaData(width, height, imgCount, Error, env, metaData); |
| 589 | return (jint)(Error == 0 ? info : NULL); |
| 590 | } |
| 591 |
| 592 | JNIEXPORT jlong JNICALL Java_org_telegram_ui_Views_GifDrawable_getAllocationByteCount(JNIEnv *env, jclass class, jobject gifInfo) { |
| 593 | GifInfo *info = (GifInfo *)gifInfo; |
| 594 | if (info == NULL) { |
| 595 | return 0; |
| 596 | } |
| 597 | unsigned int pxCount = info->gifFilePtr->SWidth + info->gifFilePtr->SHeight; |
| 598 | jlong sum = pxCount * sizeof(char); |
| 599 | if (info->backupPtr != NULL) { |
| 600 | sum += pxCount * sizeof(argb); |
| 601 | } |
| 602 | return sum; |
| 603 | } |
| 604 |
| 605 | JNIEXPORT void JNICALL Java_org_telegram_ui_Views_GifDrawable_reset(JNIEnv *env, jclass class, jobject gifInfo) { |
| 606 | GifInfo *info = (GifInfo *)gifInfo; |
| 607 | if (info == NULL) { |
| 608 | return; |
| 609 | } |
| 610 | reset(info); |
| 611 | } |
| 612 |
| 613 | JNIEXPORT void JNICALL Java_org_telegram_ui_Views_GifDrawable_setSpeedFactor(JNIEnv *env, jclass class, jobject gifInfo, jfloat factor) { |
| 614 | GifInfo *info = (GifInfo *)gifInfo; |
| 615 | if (info == NULL) { |
| 616 | return; |
| 617 | } |
| 618 | info->speedFactor = factor; |
| 619 | } |
| 620 |
| 621 | JNIEXPORT void JNICALL Java_org_telegram_ui_Views_GifDrawable_seekToTime(JNIEnv *env, jclass class, jobject gifInfo, jint desiredPos, jintArray jPixels) { |
| 622 | GifInfo *info = (GifInfo *)gifInfo; |
| 623 | if (info == NULL || jPixels == NULL) { |
| 624 | return; |
| 625 | } |
| 626 | int imgCount = info->gifFilePtr->ImageCount; |
| 627 | if (imgCount <= 1) { |
| 628 | return; |
| 629 | } |
| 630 |
| 631 | unsigned long sum = 0; |
| 632 | int i; |
| 633 | for (i = 0; i < imgCount; i++) { |
| 634 | unsigned long newSum = sum + info->infos[i].duration; |
| 635 | if (newSum >= desiredPos) { |
| 636 | break; |
| 637 | } |
| 638 | sum = newSum; |
| 639 | } |
| 640 | if (i < info->currentIndex) { |
| 641 | return; |
| 642 | } |
| 643 |
| 644 | unsigned long lastFrameRemainder = desiredPos - sum; |
| 645 | if (i == imgCount - 1 && lastFrameRemainder > info->infos[i].duration) { |
| 646 | lastFrameRemainder = info->infos[i].duration; |
| 647 | } |
| 648 | if (i > info->currentIndex) { |
| 649 | jint *const pixels = (*env)->GetIntArrayElements(env, jPixels, 0); |
| 650 | if (pixels == NULL) { |
| 651 | return; |
| 652 | } |
| 653 | while (info->currentIndex <= i) { |
| 654 | info->currentIndex++; |
| 655 | getBitmap((argb*) pixels, info); |
| 656 | } |
| 657 | (*env)->ReleaseIntArrayElements(env, jPixels, pixels, 0); |
| 658 | } |
| 659 | info->lastFrameReaminder = lastFrameRemainder; |
| 660 |
| 661 | if (info->speedFactor == 1.0) { |
| 662 | info->nextStartTime = getRealTime() + lastFrameRemainder; |
| 663 | } else { |
| 664 | info->nextStartTime = getRealTime() + lastFrameRemainder * info->speedFactor; |
| 665 | } |
| 666 | } |
| 667 |
| 668 | JNIEXPORT void JNICALL Java_org_telegram_ui_Views_GifDrawable_seekToFrame(JNIEnv *env, jclass class, jobject gifInfo, jint desiredIdx, jintArray jPixels) { |
| 669 | GifInfo *info = (GifInfo *)gifInfo; |
| 670 | if (info == NULL|| jPixels==NULL) { |
| 671 | return; |
| 672 | } |
| 673 | if (desiredIdx <= info->currentIndex) { |
| 674 | return; |
| 675 | } |
| 676 |
| 677 | int imgCount = info->gifFilePtr->ImageCount; |
| 678 | if (imgCount <= 1) { |
| 679 | return; |
| 680 | } |
| 681 |
| 682 | jint *const pixels = (*env)->GetIntArrayElements(env, jPixels, 0); |
| 683 | if (pixels == NULL) { |
| 684 | return; |
| 685 | } |
| 686 |
| 687 | info->lastFrameReaminder = 0; |
| 688 | if (desiredIdx >= imgCount) { |
| 689 | desiredIdx = imgCount - 1; |
| 690 | } |
| 691 |
| 692 | while (info->currentIndex < desiredIdx) { |
| 693 | info->currentIndex++; |
| 694 | getBitmap((argb *) pixels, info); |
| 695 | } |
| 696 | (*env)->ReleaseIntArrayElements(env, jPixels, pixels, 0); |
| 697 | if (info->speedFactor == 1.0) { |
| 698 | info->nextStartTime = getRealTime() + info->infos[info->currentIndex].duration; |
| 699 | } else { |
| 700 | info->nextStartTime = getRealTime() + info->infos[info->currentIndex].duration * info->speedFactor; |
| 701 | } |
| 702 | } |
| 703 |
| 704 | JNIEXPORT void JNICALL Java_org_telegram_ui_Views_GifDrawable_renderFrame(JNIEnv *env, jclass class, jintArray jPixels, jobject gifInfo, jintArray metaData) { |
| 705 | GifInfo *info = (GifInfo *)gifInfo; |
| 706 | if (info == NULL || jPixels == NULL) { |
| 707 | return; |
| 708 | } |
| 709 | bool needRedraw = false; |
| 710 | unsigned long rt = getRealTime(); |
| 711 |
| 712 | if (rt >= info->nextStartTime && info->currentLoop < info->loopCount) { |
| 713 | if (++info->currentIndex >= info->gifFilePtr->ImageCount) { |
| 714 | info->currentIndex = 0; |
| 715 | } |
| 716 | needRedraw = true; |
| 717 | } |
| 718 | jint *const rawMetaData = (*env)->GetIntArrayElements(env, metaData, 0); |
| 719 | if (rawMetaData == NULL) { |
| 720 | return; |
| 721 | } |
| 722 |
| 723 | if (needRedraw) { |
| 724 | jint *const pixels = (*env)->GetIntArrayElements(env, jPixels, 0); |
| 725 | if (pixels == NULL) { |
| 726 | (*env)->ReleaseIntArrayElements(env, metaData, rawMetaData, 0); |
| 727 | return; |
| 728 | } |
| 729 | getBitmap((argb *)pixels, info); |
| 730 | rawMetaData[3] = info->gifFilePtr->Error; |
| 731 |
| 732 | (*env)->ReleaseIntArrayElements(env, jPixels, pixels, 0); |
| 733 | unsigned int scaledDuration = info->infos[info->currentIndex].duration; |
| 734 | if (info->speedFactor != 1.0) { |
| 735 | scaledDuration /= info->speedFactor; |
| 736 | if (scaledDuration<=0) { |
| 737 | scaledDuration=1; |
| 738 | } else if (scaledDuration > INT_MAX) { |
| 739 | scaledDuration = INT_MAX; |
| 740 | } |
| 741 | } |
| 742 | info->nextStartTime = rt + scaledDuration; |
| 743 | rawMetaData[4] = scaledDuration; |
| 744 | } else { |
| 745 | long delay = info->nextStartTime-rt; |
| 746 | if (delay < 0) { |
| 747 | rawMetaData[4] = -1; |
| 748 | } else { |
| 749 | rawMetaData[4] = (int) delay; |
| 750 | } |
| 751 | } |
| 752 | (*env)->ReleaseIntArrayElements(env, metaData, rawMetaData, 0); |
| 753 | } |
| 754 |
| 755 | JNIEXPORT void JNICALL Java_org_telegram_ui_Views_GifDrawable_free(JNIEnv *env, jclass class, jobject gifInfo) { |
| 756 | if (gifInfo == NULL) { |
| 757 | return; |
| 758 | } |
| 759 | GifInfo *info = (GifInfo *)gifInfo; |
| 760 | FILE *file = info->gifFilePtr->UserData; |
| 761 | if (file) { |
| 762 | fclose(file); |
| 763 | } |
| 764 | info->gifFilePtr->UserData = NULL; |
| 765 | cleanUp(info); |
| 766 | } |
| 767 |
| 768 | JNIEXPORT jstring JNICALL Java_org_telegram_ui_Views_GifDrawable_getComment(JNIEnv *env, jclass class, jobject gifInfo) { |
| 769 | if (gifInfo == NULL) { |
| 770 | return NULL; |
| 771 | } |
| 772 | GifInfo *info = (GifInfo *)gifInfo; |
| 773 | return (*env)->NewStringUTF(env, info->comment); |
| 774 | } |
| 775 |
| 776 | JNIEXPORT jint JNICALL Java_org_telegram_ui_Views_GifDrawable_getLoopCount(JNIEnv *env, jclass class, jobject gifInfo) { |
| 777 | if (gifInfo == NULL) { |
| 778 | return 0; |
| 779 | } |
| 780 | return ((GifInfo *)gifInfo)->loopCount; |
| 781 | } |
| 782 |
| 783 | JNIEXPORT jint JNICALL Java_org_telegram_ui_Views_GifDrawable_getDuration(JNIEnv *env, jclass class, jobject gifInfo) { |
| 784 | GifInfo *info = (GifInfo *)gifInfo; |
| 785 | if (info == NULL) { |
| 786 | return 0; |
| 787 | } |
| 788 | int i; |
| 789 | unsigned long sum = 0; |
| 790 | for (i = 0; i < info->gifFilePtr->ImageCount; i++) { |
| 791 | sum += info->infos[i].duration; |
| 792 | } |
| 793 | return sum; |
| 794 | } |
| 795 |
| 796 | JNIEXPORT jint JNICALL Java_org_telegram_ui_Views_GifDrawable_getCurrentPosition(JNIEnv *env, jclass class, jobject gifInfo) { |
| 797 | GifInfo *info = (GifInfo *)gifInfo; |
| 798 | if (info == NULL) { |
| 799 | return 0; |
| 800 | } |
| 801 | int idx = info->currentIndex; |
| 802 | if (idx < 0 || info->gifFilePtr->ImageCount <= 1) { |
| 803 | return 0; |
| 804 | } |
| 805 | int i; |
| 806 | unsigned int sum = 0; |
| 807 | for (i = 0; i < idx; i++) { |
| 808 | sum += info->infos[i].duration; |
| 809 | } |
| 810 | unsigned long remainder = info->lastFrameReaminder == ULONG_MAX ? getRealTime() - info->nextStartTime : info->lastFrameReaminder; |
| 811 | return (int) (sum + remainder); |
| 812 | } |
| 813 |
| 814 | JNIEXPORT void JNICALL Java_org_telegram_ui_Views_GifDrawable_saveRemainder(JNIEnv *env, jclass class, jobject gifInfo) { |
| 815 | GifInfo *info = (GifInfo *)gifInfo; |
| 816 | if (info == NULL) { |
| 817 | return; |
| 818 | } |
| 819 | info->lastFrameReaminder = getRealTime() - info->nextStartTime; |
| 820 | } |
| 821 |
| 822 | JNIEXPORT void JNICALL Java_org_telegram_ui_Views_GifDrawable_restoreRemainder(JNIEnv *env, jclass class, jobject gifInfo) { |
| 823 | GifInfo *info = (GifInfo *)gifInfo; |
| 824 | if (info == NULL || info->lastFrameReaminder == ULONG_MAX) { |
| 825 | return; |
| 826 | } |
| 827 | info->nextStartTime = getRealTime() + info->lastFrameReaminder; |
| 828 | info->lastFrameReaminder = ULONG_MAX; |
| 829 | } |
| 830 |
| 831 | JNIEXPORT jint JNICALL Java_org_telegram_ui_Views_GifDrawable_openFile(JNIEnv *env, jclass class, jintArray metaData, jstring jfname) { |
| 832 | if (jfname == NULL) { |
| 833 | setMetaData(0, 0, 0, D_GIF_ERR_OPEN_FAILED, env, metaData); |
| 834 | return (jint) NULL; |
| 835 | } |
| 836 |
| 837 | const char *const fname = (*env)->GetStringUTFChars(env, jfname, 0); |
| 838 | FILE *file = fopen(fname, "rb"); |
| 839 | (*env)->ReleaseStringUTFChars(env, jfname, fname); |
| 840 | if (file == NULL) { |
| 841 | setMetaData(0, 0, 0, D_GIF_ERR_OPEN_FAILED, env, metaData); |
| 842 | return (jint) NULL; |
| 843 | } |
| 844 | int Error = 0; |
| 845 | GifFileType *GifFileIn = DGifOpen(file, &fileReadFunc, &Error); |
| 846 | return open(GifFileIn, Error, ftell(file), env, metaData); |
| 847 | } |
| 848 |