Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Typedefs | Enumerations | Functions
GIF2.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  gif_struct

Defines

#define MAX_LZW_BITS   12
#define MAX_BITS   4097 /* 2^MAX_LZW_BITS+1 */
#define MINIMUM_DELAY_TIME   100
#define MAX_COLORS   256

Typedefs

typedef struct gif_struct gif_struct

Enumerations

enum  gstate {
  gif_gather, gif_init, gif_type, gif_version,
  gif_global_header, gif_global_colormap, gif_image_start, gif_image_header,
  gif_image_colormap, gif_image_body, gif_lzw_start, gif_lzw,
  gif_sub_block, gif_extension, gif_control_extension, gif_consume_block,
  gif_skip_block, gif_done, gif_oom, gif_error,
  gif_comment_extension, gif_application_extension, gif_netscape_extension_block, gif_consume_netscape_extension,
  gif_consume_comment, gif_delay, gif_stop_animating
}
enum  gdispose { DISPOSE_NOT_SPECIFIED = 0, DISPOSE_KEEP = 1, DISPOSE_OVERWRITE_BGCOLOR = 2, DISPOSE_OVERWRITE_PREVIOUS = 3 }

Functions

PRBool GIFInit (gif_struct *gs, void *aClientData)
void gif_destroy (gif_struct *aGIFStruct)
PRStatus gif_write (gif_struct *aGIFStruct, const PRUint8 *buf, PRUint32 numbytes)
PRBool gif_write_ready (const gif_struct *aGIFStruct)

Class Documentation

struct gif_struct

Definition at line 90 of file GIF2.h.

Collaboration diagram for gif_struct:
Class Members
int avail
int bits
int clear_code
void * clientptr
int codemask
int codesize
int count
int datasize
int32 datum
PRUint32 delay_time
gdispose disposal_method
PRUint8 firstchar
PRUint8 * gather_head
int32 gather_request_size
int32 gathered
PRUint8 global_colormap
int global_colormap_size
PRUintn height
PRUint8 * hold
int images_decoded
PRPackedBool interlaced
int ipass
PRUintn irow
PRPackedBool is_local_colormap_defined
PRPackedBool is_transparent
PRUint8 * local_colormap
int local_colormap_size
int loop_count
int oldcode
gstate post_gather_state
PRUint16 prefix
PRPackedBool progressive_display
PRUint8 * rowbuf
PRUint8 * rowend
PRUint8 * rowp
PRUintn rows_remaining
int screen_bgcolor
PRUintn screen_height
PRUintn screen_width
PRUint8 stack
PRUint8 * stackp
gstate state
PRUint8 suffix
int tpixel
int version
PRUintn width
PRUintn x_offset
PRUintn y_offset

Define Documentation

#define MAX_BITS   4097 /* 2^MAX_LZW_BITS+1 */

Definition at line 41 of file GIF2.h.

#define MAX_COLORS   256

Definition at line 43 of file GIF2.h.

#define MAX_LZW_BITS   12

Definition at line 40 of file GIF2.h.

Definition at line 42 of file GIF2.h.


Typedef Documentation

typedef struct gif_struct gif_struct

Enumeration Type Documentation

enum gdispose
Enumerator:
DISPOSE_NOT_SPECIFIED 
DISPOSE_KEEP 
DISPOSE_OVERWRITE_BGCOLOR 
DISPOSE_OVERWRITE_PREVIOUS 

Definition at line 81 of file GIF2.h.

{
    DISPOSE_NOT_SPECIFIED      = 0,
    DISPOSE_KEEP               = 1, /* Leave it in the framebuffer */
    DISPOSE_OVERWRITE_BGCOLOR  = 2, /* Overwrite with background color */
    DISPOSE_OVERWRITE_PREVIOUS = 3  /* Save-under */
} gdispose;
enum gstate
Enumerator:
gif_gather 
gif_init 
gif_type 
gif_version 
gif_global_header 
gif_global_colormap 
gif_image_start 
gif_image_header 
gif_image_colormap 
gif_image_body 
gif_lzw_start 
gif_lzw 
gif_sub_block 
gif_extension 
gif_control_extension 
gif_consume_block 
gif_skip_block 
gif_done 
gif_oom 
gif_error 
gif_comment_extension 
gif_application_extension 
gif_netscape_extension_block 
gif_consume_netscape_extension 
gif_consume_comment 
gif_delay 
gif_stop_animating 

Definition at line 49 of file GIF2.h.


Function Documentation

void gif_destroy ( gif_struct aGIFStruct)

Definition at line 996 of file GIF2.cpp.

{
  if (!gs)
    return;

  /* Clear any pending timeouts */
  if (gs->delay_time)
    gs->delay_time = 0;

  PR_FREEIF(gs->rowbuf);
  PR_FREEIF(gs->hold);

  PR_FREEIF(gs->local_colormap);
}
PRStatus gif_write ( gif_struct aGIFStruct,
const PRUint8 buf,
PRUint32  numbytes 
)

Definition at line 441 of file GIF2.cpp.

{
  if (!gs)
    return PR_FAILURE;

  /* If we fail, some upstream data provider ignored the
     zero return value from il_gif_write_ready() which says not to
     send any more data to this stream until the delay timeout fires. */
  if ((len != 0) && (gs->gathered >= MAX_READ_AHEAD))
    return PR_FAILURE;

  const PRUint8 *q, *p = buf, *ep = buf + len;

  q = nsnull;                   /* Initialize to shut up gcc warnings */

  while (p <= ep) {
    switch (gs->state)
    {
    case gif_lzw:
      if (do_lzw(gs, q) < 0) {
        gs->state = gif_error;
        break;
      }
      GETN(1, gif_sub_block);
      break;

    case gif_lzw_start:
    {
      /* Initialize LZW parser/decoder */
      gs->datasize = *q;
      if (gs->datasize >= MAX_LZW_BITS) {
        gs->state = gif_error;
        break;
      }

      gs->clear_code = 1 << gs->datasize;
      gs->avail = gs->clear_code + 2;
      gs->oldcode = -1;
      gs->codesize = gs->datasize + 1;
      gs->codemask = (1 << gs->codesize) - 1;

      gs->datum = gs->bits = 0;

      if (gs->clear_code >= MAX_BITS) {
        gs->state = gif_error;
        break;
      }

      /* init the tables */
      for (int i = 0; i < gs->clear_code; i++)
        gs->suffix[i] = i;

      gs->stackp = gs->stack;

      GETN(1, gif_sub_block);
    }
    break;

    /* We're positioned at the very start of the file. */
    case gif_init:
    {
      GETN(3, gif_type);
      break;
    }

    /* All GIF files begin with "GIF87a" or "GIF89a" */
    case gif_type:
    {
      if (strncmp((char*)q, "GIF", 3)) {
        gs->state = gif_error;
        break;
      }
      GETN(3, gif_version);
    }
    break;

    case gif_version:
    {
      if (!strncmp((char*)q, "89a", 3)) {
        gs->version = 89;
      } else if (!strncmp((char*)q, "87a", 3)) {
        gs->version = 87;
      } else {
        gs->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.
       */

      gs->screen_width = GETINT16(q);
      gs->screen_height = GETINT16(q + 2);

      gs->screen_bgcolor = q[5];

      gs->global_colormap_size = 2<<(q[4]&0x07);

      // XXX make callback
      nsGIFDecoder2::BeginGIF(
        gs->clientptr,
        gs->screen_width,
        gs->screen_height,
        gs->screen_bgcolor);

      if (q[4] & 0x80) /* global map */
        /* 3 bytes for each entry in the global colormap */
        GETN(gs->global_colormap_size*3, gif_global_colormap);
      else
        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:
    {
      memcpy(gs->global_colormap, q, 3 * gs->global_colormap_size);

      GETN(1, gif_image_start);
    }
    break;

    case gif_image_start:
    {
      if (*q == ';') { /* terminator */
        gs->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 (gs->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.
           */
          gs->state = gif_done;
        } else {
          /* No images decoded, there is nothing to display. */
          gs->state = gif_error;
        }
        break;
      } else
        GETN(9, gif_image_header);
    }
    break;

    case gif_extension:
    {
      int len = gs->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 (*q & 0x1) {
        gs->tpixel = q[3];
        gs->is_transparent = PR_TRUE;
      } else {
        gs->is_transparent = PR_FALSE;
        // ignoring gfx control extension
      }
      gs->disposal_method = (gdispose)(((*q) >> 2) & 0x7);
      // Some specs say 3rd bit (value 4), other specs say value 3
      // Let's choose 3 (the more popular)
      if (gs->disposal_method == 4)
        gs->disposal_method = (gdispose)3;
      gs->delay_time = GETINT16(q + 1) * 10;
      GETN(1, gif_consume_block);
    }
    break;

    case gif_comment_extension:
    {
      gs->count = *q;
      if (gs->count)
        GETN(gs->count, 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) {
        gs->loop_count = GETINT16(q + 1);

        /* Zero loop count is infinite animation loop request */
        if (gs->loop_count == 0)
          gs->loop_count = -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
        gs->state = gif_error; // 0,3-7 are yet to be defined netscape
                               // extension codes

      break;
    }

    case gif_image_header:
    {
      PRUintn height, width;

      /* Get image offsets, with respect to the screen origin */
      gs->x_offset = GETINT16(q);
      gs->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 ((gs->images_decoded == 0) &&
          ((gs->screen_height < height) || (gs->screen_width < width) ||
           (gs->version == 87)))
      {
        gs->screen_height = height;
        gs->screen_width = width;
        gs->x_offset = 0;
        gs->y_offset = 0;

        nsGIFDecoder2::BeginGIF(gs->clientptr,
                                gs->screen_width,
                                gs->screen_height,
                                gs->screen_bgcolor);
      }

      /* Work around more broken GIF files that have zero image
         width or height */
      if (!height || !width) {
        height = gs->screen_height;
        width = gs->screen_width;
        if (!height || !width) {
          gs->state = gif_error;
          break;
        }
      }

      gs->height = height;
      gs->width = width;

      nsGIFDecoder2::BeginImageFrame(gs->clientptr,
                                     gs->images_decoded + 1,   /* Frame number, 1-n */
                                     gs->x_offset,  /* X offset in logical screen */
                                     gs->y_offset,  /* Y offset in logical screen */
                                     width,
                                     height);

      /* 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 (gs->screen_width < width) {
        /* XXX Deviant! */

        gs->rowbuf = (PRUint8*)PR_REALLOC(gs->rowbuf, width);

        if (!gs->rowbuf) {
          gs->state = gif_oom;
          break;
        }

        gs->screen_width = width;
        if (gs->screen_height < gs->height)
          gs->screen_height = gs->height;

      }
      else {
        if (!gs->rowbuf)
          gs->rowbuf = (PRUint8*)PR_MALLOC(gs->screen_width);
      }

      if (!gs->rowbuf) {
          gs->state = gif_oom;
          break;
      }

      if (q[8] & 0x40) {
        gs->interlaced = PR_TRUE;
        gs->ipass = 1;
      } else {
        gs->interlaced = PR_FALSE;
        gs->ipass = 0;
      }

      if (gs->images_decoded == 0) {
        gs->progressive_display = PR_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 */
        gs->progressive_display = PR_FALSE;
      }

      /* Clear state from last image */
      gs->irow = 0;
      gs->rows_remaining = gs->height;
      gs->rowend = gs->rowbuf + gs->width;
      gs->rowp = gs->rowbuf;

      /* bits per pixel is 1<<((q[8]&0x07) + 1); */

      if (q[8] & 0x80) /* has a local colormap? */
      {
        int num_colors = 2 << (q[8] & 0x7);

        // If current local_colormap is not big enough, force reallocation
        if (num_colors > gs->local_colormap_size)
          PR_FREEIF(gs->local_colormap);
        gs->local_colormap_size = num_colors;

        /* Switch to the new local palette after it loads */
        gs->is_local_colormap_defined = PR_TRUE;
        GETN(gs->local_colormap_size * 3, gif_image_colormap);
      } else {
        /* Switch back to the global palette */
        gs->is_local_colormap_defined = PR_FALSE;
        GETN(1, gif_lzw_start);
      }
    }
    break;

    case gif_image_colormap:
    {
      PRUint8 *map = gs->local_colormap;
      if (!map) {
        map = gs->local_colormap = (PRUint8*)PR_MALLOC(3 * gs->local_colormap_size);
        if (!map) {
          gs->state = gif_oom;
          break;
        }
      }

      memcpy(map, q, 3 * gs->local_colormap_size);

      GETN(1, gif_lzw_start);
    }
    break;

    case gif_sub_block:
    {
      if ((gs->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 (gs->rows_remaining == 0) {
          /* This is an illegal GIF, but we remain tolerant. */
#ifdef DONT_TOLERATE_BROKEN_GIFS
          gs->state = gif_error;
          break;
#else
          GETN(1, gif_sub_block);
#endif
        }
        GETN(gs->count, gif_lzw);
      }
      else
      /* See if there are any more images in this sequence. */
      {
        gs->images_decoded++;

        nsGIFDecoder2::EndImageFrame(gs->clientptr,
                                     gs->images_decoded,
                                     gs->delay_time);

        /* Clear state from this image */
        gs->is_transparent = PR_FALSE;

        /* An image can specify a delay time before which to display
           subsequent images. */
        if (gs->delay_time < MINIMUM_DELAY_TIME)
          gs->delay_time = MINIMUM_DELAY_TIME;

        GETN(1, gif_image_start);
      }
    }
    break;

    case gif_done:
      nsGIFDecoder2::EndGIF(gs->clientptr, gs->loop_count);
      return PR_SUCCESS;
      break;

    case gif_delay:
    case gif_gather:
    {
      PRInt32 gather_remaining;
      PRInt32 request_size = gs->gather_request_size;

      {
        gather_remaining = request_size - gs->gathered;

        /* Do we already have enough data in the accumulation
           buffer to satisfy the request ?  (This can happen
           after we transition from the gif_delay state.) */
        if (gather_remaining <= 0) {
          gs->gathered -= request_size;
          q = gs->gather_head;
          gs->gather_head += request_size;
          gs->state = gs->post_gather_state;
          break;
        }

        /* Shift remaining data to the head of the buffer */
        if (gs->gathered && (gs->gather_head != gs->hold)) {
          memmove(gs->hold, gs->gather_head, gs->gathered);
          gs->gather_head = gs->hold;
        }

        /* If we add the data just handed to us by the netlib
           to what we've already gathered, is there enough to satisfy
           the current request ? */
        if ((ep - p) >= gather_remaining) {
          if (gs->gathered) { /* finish a prior gather */
            char *hold = (char*)gs->hold;
            BlockAllocCat(hold, gs->gathered, (char*)p, gather_remaining);
            gs->hold = (PRUint8*)hold;
            q = gs->gather_head = gs->hold;
            gs->gathered = 0;
          } else
            q = p;

          p += gather_remaining;
          gs->state = gs->post_gather_state;
        } else {
          char *hold = (char*)gs->hold;
          BlockAllocCat(hold, gs->gathered, (char*)p, ep - p);
          gs->hold = (PRUint8*)hold;
          gs->gather_head = gs->hold;
          gs->gathered += ep-p;
          return PR_SUCCESS;
        }
      }
    }
    break;

    // Handle out of memory errors
    case gif_oom:
      return PR_FAILURE;

    // Handle general errors
    case gif_error:
      nsGIFDecoder2::EndGIF(gs->clientptr, gs->loop_count);
      return PR_SUCCESS;

    case gif_stop_animating:
      return PR_SUCCESS;

    // We shouldn't ever get here.
    default:
      break;
    }
  }

  return PR_SUCCESS;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 428 of file GIF2.cpp.

{
  if (!gs)
    return PR_FALSE;

  return (gs->gathered < MAX_READ_AHEAD);
}

Here is the caller graph for this function:

PRBool GIFInit ( gif_struct gs,
void aClientData 
)

Definition at line 404 of file GIF2.cpp.

{
  NS_ASSERTION(gs, "Got null argument");
  if (!gs)
    return PR_FALSE;

  // Clear out the structure, excluding the arrays
  memset(gs, 0, sizeof(gif_struct));
  gs->clientptr = aClientData;

  gs->state = gif_init;
  gs->post_gather_state = gif_error;
  gs->gathered = 0;

  return PR_TRUE;
}

Here is the call graph for this function: