📄 gifimagereader.cpp
字号:
for (int i = 0; i < frame_reader->clear_code; i++) frame_reader->suffix[i] = i; if (!frame_reader->stack) frame_reader->stack = new unsigned char[MAX_BITS]; frame_reader->stackp = frame_reader->stack; } GETN(1, gif_sub_block); } break; /* All GIF files begin with "GIF87a" or "GIF89a" */ case gif_type: { if (!strncmp((char*)q, "GIF89a", 6)) { version = 89; } else if (!strncmp((char*)q, "GIF87a", 6)) { version = 87; } else { state = gif_error; break; } GETN(7, gif_global_header); } break; case gif_global_header: { /* This is the height and width of the "screen" or * frame into which images are rendered. The * individual images can be smaller than the * screen size and located with an origin anywhere * within the screen. */ screen_width = GETINT16(q); screen_height = GETINT16(q + 2); // CALLBACK: Inform the decoderplugin of our size. if (clientptr) clientptr->sizeNowAvailable(screen_width, screen_height); screen_bgcolor = q[5]; global_colormap_size = 2<<(q[4]&0x07); if ((q[4] & 0x80) && global_colormap_size > 0) { /* global map */ // Get the global colormap const unsigned size = 3*global_colormap_size; // Malloc the color map, but only if we're not just counting frames. if (query != GIFImageDecoder::GIFFrameCountQuery) global_colormap = new unsigned char[size]; if (len < size) { // Use 'hold' pattern to get the global colormap GETN(size, gif_global_colormap); break; } // Copy everything and go directly to gif_image_start. if (global_colormap) memcpy(global_colormap, buf, size); buf += size; len -= size; } GETN(1, gif_image_start); // q[6] = Pixel Aspect Ratio // Not used // float aspect = (float)((q[6] + 15) / 64.0); } break; case gif_global_colormap: // Everything is already copied into global_colormap GETN(1, gif_image_start); break; case gif_image_start: { if (*q == ';') { /* terminator */ state = gif_done; break; } if (*q == '!') { /* extension */ GETN(2, gif_extension); break; } /* If we get anything other than ',' (image separator), '!' * (extension), or ';' (trailer), there is extraneous data * between blocks. The GIF87a spec tells us to keep reading * until we find an image separator, but GIF89a says such * a file is corrupt. We follow GIF89a and bail out. */ if (*q != ',') { if (images_decoded > 0) { /* The file is corrupt, but one or more images have * been decoded correctly. In this case, we proceed * as if the file were correctly terminated and set * the state to gif_done, so the GIF will display. */ state = gif_done; } else { /* No images decoded, there is nothing to display. */ state = gif_error; } break; } else GETN(9, gif_image_header); } break; case gif_extension: { int len = count = q[1]; gstate es = gif_skip_block; switch (*q) { case 0xf9: es = gif_control_extension; break; case 0x01: // ignoring plain text extension break; case 0xff: es = gif_application_extension; break; case 0xfe: es = gif_consume_comment; break; } if (len) GETN(len, es); else GETN(1, gif_image_start); } break; case gif_consume_block: if (!*q) GETN(1, gif_image_start); else GETN(*q, gif_skip_block); break; case gif_skip_block: GETN(1, gif_consume_block); break; case gif_control_extension: { if (query != GIFImageDecoder::GIFFrameCountQuery) { if (!frame_reader) frame_reader = new GIFFrameReader(); } if (frame_reader) { if (*q & 0x1) { frame_reader->tpixel = q[3]; frame_reader->is_transparent = true; } else { frame_reader->is_transparent = false; // ignoring gfx control extension } // NOTE: This relies on the values in the FrameDisposalMethod enum // matching those in the GIF spec! frame_reader->disposal_method = (WebCore::RGBA32Buffer::FrameDisposalMethod)(((*q) >> 2) & 0x7); // Some specs say 3rd bit (value 4), other specs say value 3 // Let's choose 3 (the more popular) if (frame_reader->disposal_method == 4) frame_reader->disposal_method = WebCore::RGBA32Buffer::DisposeOverwritePrevious; frame_reader->delay_time = GETINT16(q + 1) * 10; } GETN(1, gif_consume_block); } break; case gif_comment_extension: { if (*q) GETN(*q, gif_consume_comment); else GETN(1, gif_image_start); } break; case gif_consume_comment: GETN(1, gif_comment_extension); break; case gif_application_extension: /* Check for netscape application extension */ if (!strncmp((char*)q, "NETSCAPE2.0", 11) || !strncmp((char*)q, "ANIMEXTS1.0", 11)) GETN(1, gif_netscape_extension_block); else GETN(1, gif_consume_block); break; /* Netscape-specific GIF extension: animation looping */ case gif_netscape_extension_block: if (*q) GETN(*q, gif_consume_netscape_extension); else GETN(1, gif_image_start); break; /* Parse netscape-specific application extensions */ case gif_consume_netscape_extension: { int netscape_extension = q[0] & 7; /* Loop entire animation specified # of times. Only read the loop count during the first iteration. */ if (netscape_extension == 1) { loop_count = GETINT16(q + 1); GETN(1, gif_netscape_extension_block); } /* Wait for specified # of bytes to enter buffer */ else if (netscape_extension == 2) { // Don't do this, this extension doesn't exist (isn't used at all) // and doesn't do anything, as our streaming/buffering takes care of it all... // See: http://semmix.pl/color/exgraf/eeg24.htm GETN(1, gif_netscape_extension_block); } else state = gif_error; // 0,3-7 are yet to be defined netscape // extension codes break; } case gif_image_header: { unsigned height, width, x_offset, y_offset; /* Get image offsets, with respect to the screen origin */ x_offset = GETINT16(q); y_offset = GETINT16(q + 2); /* Get image width and height. */ width = GETINT16(q + 4); height = GETINT16(q + 6); /* Work around broken GIF files where the logical screen * size has weird width or height. We assume that GIF87a * files don't contain animations. */ if ((images_decoded == 0) && ((screen_height < height) || (screen_width < width) || (version == 87))) { screen_height = height; screen_width = width; x_offset = 0; y_offset = 0; // CALLBACK: Inform the decoderplugin of our size. if (clientptr) clientptr->sizeNowAvailable(screen_width, screen_height); } /* Work around more broken GIF files that have zero image width or height */ if (!height || !width) { height = screen_height; width = screen_width; if (!height || !width) { state = gif_error; break; } } if (query == GIFImageDecoder::GIFSizeQuery || haltAtFrame == images_decoded) { // The decoder needs to stop. Hand back the number of bytes we consumed from // buffer minus 9 (the amount we consumed to read the header). if (clientptr) clientptr->decodingHalted(len + 9); GETN(9, gif_image_header); return true; } images_count = images_decoded + 1; if (query == GIFImageDecoder::GIFFullQuery && !frame_reader) frame_reader = new GIFFrameReader(); if (frame_reader) { frame_reader->x_offset = x_offset; frame_reader->y_offset = y_offset; frame_reader->height = height; frame_reader->width = width; /* This case will never be taken if this is the first image */ /* being decoded. If any of the later images are larger */ /* than the screen size, we need to reallocate buffers. */ if (screen_width < width) { /* XXX Deviant! */ delete []frame_reader->rowbuf; screen_width = width; frame_reader->rowbuf = new unsigned char[screen_width]; } else if (!frame_reader->rowbuf) { frame_reader->rowbuf = new unsigned char[screen_width]; } if (!frame_reader->rowbuf) { state = gif_oom; break; } if (screen_height < height) screen_height = height; if (q[8] & 0x40) { frame_reader->interlaced = true; frame_reader->ipass = 1; } else { frame_reader->interlaced = false; frame_reader->ipass = 0; } if (images_decoded == 0) { frame_reader->progressive_display = true; } else { /* Overlaying interlaced, transparent GIFs over existing image data using the Haeberli display hack requires saving the underlying image in order to avoid jaggies at the transparency edges. We are unprepared to deal with that, so don't display such images progressively */ frame_reader->progressive_display = false; } /* Clear state from last image */ frame_reader->irow = 0; frame_reader->rows_remaining = frame_reader->height; frame_reader->rowend = frame_reader->rowbuf + frame_reader->width; frame_reader->rowp = frame_reader->rowbuf; /* bits per pixel is q[8]&0x07 */ } if (q[8] & 0x80) /* has a local colormap? */ { int num_colors = 2 << (q[8] & 0x7); const unsigned size = 3*num_colors; unsigned char *map = frame_reader ? frame_reader->local_colormap : 0; if (frame_reader && (!map || (num_colors > frame_reader->local_colormap_size))) { delete []map; map = new unsigned char[size]; if (!map) { state = gif_oom; break; } } /* Switch to the new local palette after it loads */ if (frame_reader) { frame_reader->local_colormap = map; frame_reader->local_colormap_size = num_colors; frame_reader->is_local_colormap_defined = true; } if (len < size) { // Use 'hold' pattern to get the image colormap GETN(size, gif_image_colormap); break; } // Copy everything and directly go to gif_lzw_start if (frame_reader) memcpy(frame_reader->local_colormap, buf, size); buf += size; len -= size; } else if (frame_reader) { /* Switch back to the global palette */ frame_reader->is_local_colormap_defined = false; } GETN(1, gif_lzw_start); } break; case gif_image_colormap: // Everything is already copied into local_colormap GETN(1, gif_lzw_start); break; case gif_sub_block: { if ((count = *q) != 0) /* Still working on the same image: Process next LZW data block */ { /* Make sure there are still rows left. If the GIF data */ /* is corrupt, we may not get an explicit terminator. */ if (frame_reader && frame_reader->rows_remaining == 0) { /* This is an illegal GIF, but we remain tolerant. */ GETN(1, gif_sub_block); } GETN(count, gif_lzw); } else /* See if there are any more images in this sequence. */ { images_decoded++; // CALLBACK: The frame is now complete. if (clientptr && frame_reader) clientptr->frameComplete(images_decoded - 1, frame_reader->delay_time, frame_reader->disposal_method); /* Clear state from this image */ if (frame_reader) { frame_reader->is_local_colormap_defined = false; frame_reader->is_transparent = false; } GETN(1, gif_image_start); } } break; case gif_done: // When the GIF is done, we can stop. if (clientptr) clientptr->gifComplete(); return true; // Handle out of memory errors case gif_oom: return false; // Handle general errors case gif_error: // nsGIFDecoder2::EndGIF(gs->clientptr, gs->loop_count); return false; // We shouldn't ever get here. default: break; } } // Copy the leftover into gs->hold bytes_in_hold = len; if (len) { // Add what we have sofar to the block unsigned char* p; if (state == gif_global_colormap) p = global_colormap; else if (state == gif_image_colormap) p = frame_reader ? frame_reader->local_colormap : 0; else p = hold; if (p) memcpy(p, buf, len); bytes_to_consume -= len; } if (clientptr) clientptr->decodingHalted(0); return true;}#endif // PLATFORM(CAIRO)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -