changeset 480:0b47756eeb13

Started code cleanup of the image code mainly in dicache.c. part#1
author Jorge Arellano Cid <jcid@dillo.org>
date Thu, 25 Dec 2008 09:57:55 -0300
parents cd066a3f0405
children 77b7a8656fc9 c661fbf968ad 04a873b19301
files ChangeLog src/dicache.c src/dicache.h src/image.cc src/image.hh src/nav.c src/png.c
diffstat 7 files changed, 88 insertions(+), 83 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Dec 22 08:45:53 2008 -0300
+++ b/ChangeLog	Thu Dec 25 09:57:55 2008 -0300
@@ -46,6 +46,7 @@
  ? Trying a new iconv() test in configure.in.
  - Allowed the rc parser to skip whitespace around the equal sign.
  - Fixed the parser not to call Html_tag_close_* functions twice.
+!- Started code cleanup of the image code mainly in dicache.c.
    Patches: Jorge Arellano Cid
 
 dw
--- a/src/dicache.c	Mon Dec 22 08:45:53 2008 -0300
+++ b/src/dicache.c	Thu Dec 25 09:57:55 2008 -0300
@@ -33,9 +33,9 @@
  */
 static Dlist *CachedIMGs = NULL;
 
-static int dicache_size_total; /* invariant: dicache_size_total is
-                                * the sum of the image sizes (3*w*h)
-                                * of all the images in the dicache. */
+static uint_t dicache_size_total; /* invariant: dicache_size_total is
+                                   * the sum of the image sizes (3*w*h)
+                                   * of all the images in the dicache. */
 
 /*
  * Compare two dicache nodes
@@ -87,6 +87,7 @@
    entry->BitVec = NULL;
    entry->State = DIC_Empty;
    entry->version = 0;
+
    entry->next = NULL;
 
    return entry;
@@ -223,7 +224,6 @@
 /*
  * Refs the counter of a dicache entry.
  */
-
 DICacheEntry* a_Dicache_ref(const DilloUrl *Url, int version)
 {
    DICacheEntry *entry;
@@ -255,7 +255,6 @@
  */
 void a_Dicache_callback(int Op, CacheClient_t *Client)
 {
-   /* TODO: Handle Op = CA_Abort (to show what was got)  --Jcid */
    uint_t i;
    DilloWeb *Web = Client->Web;
    DilloImage *Image = Web->Image;
@@ -294,7 +293,7 @@
          }
       }
    } else if (Op == CA_Close || Op == CA_Abort) {
-      a_Image_close(Web->Image);
+      a_Image_close(Image);
       a_Bw_close_client(Web->bw, Client->Key);
    }
 }
@@ -309,12 +308,13 @@
                          uint_t width, uint_t height, DilloImgType type)
 {
    DICacheEntry *DicEntry;
-   size_t Size = width * height * 3;
 
    dReturn_if_fail ( Image != NULL && width && height );
    /* Find the DicEntry for this Image */
    DicEntry = Dicache_get_entry_version(url, version);
    dReturn_if_fail ( DicEntry != NULL );
+   /* Parameters already set? */
+   dReturn_if_fail ( DicEntry->State < DIC_SetParms );
 
    /* Initialize the DicEntry */
    DicEntry->linebuf = dNew(uchar_t, width * 3);
@@ -328,14 +328,14 @@
     * Extra code is necessary in Imgbuf to be able to free it */
    //a_Image_imgbuf_ref(DicEntry->v_imgbuf);
 
-   DicEntry->TotalSize = Size;
+   DicEntry->TotalSize = width * height * 3;
    DicEntry->width = width;
    DicEntry->height = height;
    DicEntry->type = type;
    DicEntry->BitVec = a_Bitvec_new((int)height);
    DicEntry->State = DIC_SetParms;
 
-   dicache_size_total += Size;
+   dicache_size_total += DicEntry->TotalSize;
 
    /* Allocate and initialize this image */
    a_Image_set_parms(Image, DicEntry->v_imgbuf, url, version,
--- a/src/dicache.h	Mon Dec 22 08:45:53 2008 -0300
+++ b/src/dicache.h	Thu Dec 25 09:57:55 2008 -0300
@@ -29,7 +29,7 @@
    uchar_t *cmap;          /* Color map */
    uchar_t *linebuf;       /* Decompressed RGB buffer for one line */
    void *v_imgbuf;         /* Void pointer to an Imgbuf object */
-   size_t TotalSize;       /* Amount of memory the image takes up */
+   uint_t TotalSize;       /* Amount of memory the image takes up */
    int Y;                  /* Current decoding row */
    uint_t ScanNumber;      /* Current decoding scan */
    bitvec_t *BitVec;       /* Bit vector for decoded rows */
@@ -37,7 +37,6 @@
    int RefCount;           /* Reference Counter */
    int version;            /* Version number, used for different
                               versions of the same URL image */
-
    DICacheEntry *next;     /* Link to the next "newer" version */
 };
 
--- a/src/image.cc	Mon Dec 22 08:45:53 2008 -0300
+++ b/src/image.cc	Thu Dec 25 09:57:55 2008 -0300
@@ -54,7 +54,6 @@
    Image->cmap = NULL;
    Image->in_type = DILLO_IMG_TYPE_NOTSET;
    Image->bg_color = bg_color;
-   Image->ProcessedBytes = 0;
    Image->ScanNumber = 0;
    Image->BitVec = NULL;
    Image->State = IMG_Empty;
@@ -172,7 +171,7 @@
 void a_Image_write(DilloImage *Image, void *v_imgbuf,
                    const uchar_t *buf, uint_t y, int decode)
 {
-   uchar_t *newbuf;
+   const uchar_t *newbuf;
 
    dReturn_if_fail ( y < Image->height );
 
--- a/src/image.hh	Mon Dec 22 08:45:53 2008 -0300
+++ b/src/image.hh	Thu Dec 25 09:57:55 2008 -0300
@@ -42,7 +42,6 @@
    DilloImgType in_type;    /* Image Type */
    int32_t bg_color;        /* Background color */
 
-   int ProcessedBytes;      /* Amount of bytes already decoded */
    bitvec_t *BitVec;        /* Bit vector for decoded rows */
    uint_t ScanNumber;       /* Current decoding scan */
    ImageState State;        /* Processing status */
--- a/src/nav.c	Mon Dec 22 08:45:53 2008 -0300
+++ b/src/nav.c	Thu Dec 25 09:57:55 2008 -0300
@@ -194,12 +194,13 @@
 static void Nav_open_url(BrowserWindow *bw, const DilloUrl *url, int offset)
 {
    DilloUrl *old_url;
-   bool_t MustLoad, ForceReload;
+   bool_t MustLoad, ForceReload, Repush;
    int x, y, idx, ClientKey;
    DilloWeb *Web;
 
    MSG("Nav_open_url: new url='%s'\n", URL_STR_(url));
 
+   Repush = (URL_FLAGS(url) & URL_ReloadFromCache) != 0;
    ForceReload = (URL_FLAGS(url) & (URL_E2EQuery + URL_ReloadFromCache)) != 0;
 
    /* Get the url of the current page */
@@ -207,8 +208,8 @@
    old_url = a_History_get_url(NAV_UIDX(bw, idx));
    _MSG("Nav_open_url:  old_url='%s' idx=%d\n", URL_STR(old_url), idx);
    /* Record current scrolling position */
-   if (URL_FLAGS(url) & URL_ReloadFromCache) {
-      /* Repush operation, don't change scroll position */
+   if (Repush) {
+      /* Don't change scroll position */
    } else if (old_url) {
       a_UIcmd_get_scroll_xy(bw, &x, &y);
       Nav_save_scroll_pos(bw, idx, x, y);
--- a/src/png.c	Mon Dec 22 08:45:53 2008 -0300
+++ b/src/png.c	Thu Dec 25 09:57:55 2008 -0300
@@ -4,6 +4,7 @@
  *
  * Geoff Lane nov 1999 zzassgl@twirl.mcc.ac.uk
  * Luca Rota, Jorge Arellano Cid, Eric Gaudet 2000
+ * Jorge Arellano Cid 2009
  *
  * "PNG: The Definitive Guide" by Greg Roelofs, O'Reilly
  * ISBN 1-56592-542-4
@@ -67,19 +68,19 @@
 struct _DilloPng {
    DilloImage *Image;           /* Image meta data */
    DilloUrl *url;               /* Primary Key for the dicache */
-   int version;                /* Secondary Key for the dicache */
+   int version;                 /* Secondary Key for the dicache */
 
    double display_exponent;     /* gamma correction */
-   ulong_t width;                /* png image width */
-   ulong_t height;               /* png image height */
+   ulong_t width;               /* png image width */
+   ulong_t height;              /* png image height */
    png_structp png_ptr;         /* libpng private data */
    png_infop info_ptr;          /* libpng private info */
-   uchar_t *image_data;          /* decoded image data    */
-   uchar_t **row_pointers;       /* pntr to row starts    */
+   uchar_t *image_data;         /* decoded image data    */
+   uchar_t **row_pointers;      /* pntr to row starts    */
    jmp_buf jmpbuf;              /* png error processing */
-   int error;                  /* error flag */
+   int error;                   /* error flag */
    png_uint_32 previous_row;
-   int rowbytes;               /* No. bytes in image row */
+   int rowbytes;                /* No. bytes in image row */
    short passes;
    short channels;              /* No. image channels */
 
@@ -92,13 +93,13 @@
  * ipbuf    ipbufstart                            ipbufsize
  */
 
-   uchar_t *ipbuf;               /* image data in buffer */
-   int ipbufstart;             /* first valid image byte */
-   int ipbufsize;              /* size of valid data in */
+   uchar_t *ipbuf;              /* image data in buffer */
+   int ipbufstart;              /* first valid image byte */
+   int ipbufsize;               /* size of valid data in */
 
    enum prog_state state;       /* FSM current state  */
 
-   uchar_t *linebuf;             /* o/p raster data */
+   uchar_t *linebuf;            /* o/p raster data */
 
 } DilloPng;
 
@@ -297,57 +298,37 @@
    png->state = IS_finished;
 }
 
+/*
+ * Finish the decoding process (and free the memory)
+ */
+static void Png_close(DilloPng *png, CacheClient_t *Client)
+{
+   /* Free up the resources for this image */
+   a_Dicache_close(png->url, png->version, Client);
+   dFree(png->image_data);
+   dFree(png->row_pointers);
+   dFree(png->linebuf);
+
+   if (setjmp(png->jmpbuf))
+      MSG_WARN("PNG: can't destroy read structure\n");
+   else if (png->png_ptr)
+      png_destroy_read_struct(&png->png_ptr, &png->info_ptr, NULL);
+   dFree(png);
+}
 
 /*
- * Op:  Operation to perform.
- *   If (Op == 0)
- *      start or continue processing an image if image data exists.
- *   else
- *       terminate processing, cleanup any allocated memory,
- *       close down the decoding process.
- *
- * Client->CbData  : pointer to previously allocated DilloPng work area.
- *  This holds the current state of the image processing and is saved
- *  across calls to this routine.
- * Client->Buf     : Pointer to data start.
- * Client->BufSize : the size of the data buffer.
- *
- * You have to keep track of where you are in the image data and
- * how much has been processed.
- *
- * It's entirely possible that you will not see the end of the data.  The
- * user may terminate transfer via a Stop button or there may be a network
- * failure.  This means that you can't just wait for all the data to be
- * presented before starting conversion and display.
+ * Receive and process new chunks of PNG image data
  */
-static void Png_callback(int Op, CacheClient_t *Client)
+static void Png_write(DilloPng *png, void *Buf, uint_t BufSize)
 {
-   DilloPng *png = Client->CbData;
-
-   if (Op) {
-      /* finished - free up the resources for this image */
-      a_Dicache_close(png->url, png->version, Client);
-      dFree(png->image_data);
-      dFree(png->row_pointers);
-      dFree(png->linebuf);
+   dReturn_if_fail ( Buf != NULL && BufSize > 0 );
 
-      if (setjmp(png->jmpbuf))
-         MSG_WARN("PNG: can't destroy read structure\n");
-      else if (png->png_ptr)
-         png_destroy_read_struct(&png->png_ptr, &png->info_ptr, NULL);
-      dFree(png);
-      return;
-   }
-
-   /* Let's make some sound if we have been called with no data */
-   dReturn_if_fail ( Client->Buf != NULL && Client->BufSize > 0 );
-
-   _MSG("Png_callback BufSize = %d\n", Client->BufSize);
+   _MSG("Png_callback BufSize = %d\n", BufSize);
 
    /* Keep local copies so we don't have to pass multiple args to
     * a number of functions. */
-   png->ipbuf = Client->Buf;
-   png->ipbufsize = Client->BufSize;
+   png->ipbuf = Buf;
+   png->ipbufsize = BufSize;
 
    /* start/resume the FSM here */
    while (png->state != IS_finished && DATASIZE) {
@@ -408,6 +389,37 @@
 }
 
 /*
+ * Op:  Operation to perform.
+ *   If (Op == 0)
+ *      start or continue processing an image if image data exists.
+ *   else
+ *       terminate processing, cleanup any allocated memory,
+ *       close down the decoding process.
+ *
+ * Client->CbData  : pointer to previously allocated DilloPng work area.
+ *  This holds the current state of the image processing and is kept
+ *  across calls to this routine.
+ * Client->Buf     : Pointer to data start.
+ * Client->BufSize : the size of the data buffer.
+ *
+ * You have to keep track of where you are in the image data and
+ * how much has been processed.
+ *
+ * It's entirely possible that you will not see the end of the data.  The
+ * user may terminate transfer via a Stop button or there may be a network
+ * failure.  This means that you can't just wait for all the data to be
+ * presented before starting conversion and display.
+ */
+static void Png_callback(int Op, CacheClient_t *Client)
+{
+   if (Op) { /* EOF */
+      Png_close(Client->CbData, Client);
+   } else {
+      Png_write(Client->CbData, Client->Buf, Client->BufSize);
+   }
+}
+
+/*
  * Create the image state data that must be kept between calls
  */
 static DilloPng *Png_new(DilloImage *Image, DilloUrl *url, int version)
@@ -433,25 +445,19 @@
 /*
  * MIME handler for "image/png" type
  * (Sets Png_callback or a_Dicache_callback as the cache-client)
+ *
+ * Parameters:
+ *   Type: MIME type
+ *   Ptr:  points to a Web structure
+ *   Call: Dillo calls this with more data/eod
+ *   Data: Decoding data structure
  */
 void *a_Png_image(const char *Type, void *Ptr, CA_Callback_t *Call,
                   void **Data)
 {
-/*
- * Type: MIME type
- * Ptr:  points to a Web structure
- * Call: Dillo calls this with more data/eod
- * Data: raw image data
- */
-
    DilloWeb *web = Ptr;
    DICacheEntry *DicEntry;
 
-   _MSG("a_Png_image: Type = %s\n"
-        "a_Png_image: libpng - Compiled %s; using %s.\n"
-        "a_Png_image: zlib   - Compiled %s; using %s.\n",
-        Type, PNG_LIBPNG_VER_STRING, png_libpng_ver,ZLIB_VERSION,zlib_version);
-
    if (!web->Image)
       web->Image = a_Image_new(0, 0, NULL, prefs.bg_color);