[xiph-cvs] cvs commit: w3d/tools Makefile ppmdiff.c

Holger Waechtler holger at xiph.org
Sun Jul 8 23:03:13 PDT 2001



holger      01/07/08 23:03:12

  Modified:    .        Makefile TODO _test_bitcoder.c _test_huffman.c
                        _test_rle.c bitcoder.h rle.h tarkin-io.c tarkin.c
                        tarkin_dec.c tarkin_enc.c wavelet.c wavelet_xform.c
               tools    Makefile ppmdiff.c
  Added:       .        mem.c mem.h pnm.c pnm.h
  Removed:     .        ppm.c ppm.h
  Log:
   - replaced ppm code by Simon's pnm functions
   - implemented debugging functions for MALLOC/CALLOC/REALLOC/FREE in mem.[hc]
      add -DDBG_MEMLEAKS to CFLAGS to enable refcounting
   - fixed some memory leaks

Revision  Changes    Path
1.8       +3 -3      w3d/Makefile

Index: Makefile
===================================================================
RCS file: /usr/local/cvsroot/w3d/Makefile,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- Makefile	2001/07/02 08:28:45	1.7
+++ Makefile	2001/07/09 06:03:09	1.8
@@ -1,10 +1,10 @@
 CC = gcc
 RM = rm -rf
 
-CFLAGS = -g -O0 -Wall -DTYPE=int16_t -DRLECODER -DDBG_XFORM
-LFLAGS = -g
+CFLAGS = -pg -O0 -Wall -DTYPE=int16_t -DRLECODER -DDBG_XFORM -DDBG_MEMLEAKS
+LFLAGS = -pg -lefence
 
-OBJS = ppm.o wavelet.o wavelet_xform.o yuv.o tarkin.o tarkin-io.o
+OBJS = mem.o pnm.o wavelet.o wavelet_xform.o yuv.o tarkin.o tarkin-io.o
 
 TEST_TARGETS = _test_bitcoder _test_rle _test_huffman
 

1.6       +0 -3      w3d/TODO

Index: TODO
===================================================================
RCS file: /usr/local/cvsroot/w3d/TODO,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- TODO	2001/07/02 08:28:45	1.5
+++ TODO	2001/07/09 06:03:09	1.6
@@ -1,12 +1,9 @@
 Open bugs and stuff required to fix them:
 
  - there is something really wrong in the truncation table code 
- - segfaults when bitstream limit is very high
-    (we better should check malloc return codes)
  - there are strange errors accumulating over time ...
     (probably incomplete initialisation)
  - add the pnsr tools
- - use Simon's pnm routines, support grayscale images, ppmdiff->pnmdiff
 
 
 Wavelet-related TODO's:

1.2       +6 -3      w3d/_test_bitcoder.c

Index: _test_bitcoder.c
===================================================================
RCS file: /usr/local/cvsroot/w3d/_test_bitcoder.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- _test_bitcoder.c	2001/06/25 07:56:07	1.1
+++ _test_bitcoder.c	2001/07/09 06:03:09	1.2
@@ -15,6 +15,8 @@
 
 #include <time.h>
 #include "bitcoder.h"
+#include "mem.h"
+#include "mem.c"       /* FIXME !!  */
 
 
 
@@ -43,7 +45,7 @@
       while (limit == 0)
          limit = rand() * 1.0 * MAX_COUNT / RAND_MAX;
 
-      bit = (uint8_t*) malloc (limit);
+      bit = (uint8_t*) MALLOC (limit);
 
      /**
       *   check encoding ...
@@ -63,7 +65,7 @@
 
          TEST(count == limit/8 || count == limit/8 + 1);
 
-         bitstream = (uint8_t*) malloc (count);
+         bitstream = (uint8_t*) MALLOC (count);
          memcpy (bitstream, encoder.bitstream, count);
 
          bitcoder_encoder_done (&encoder);
@@ -83,7 +85,8 @@
          }
       }
 
-      free (bit);
+      FREE (bit);
+      FREE (bitstream);
    }
 
    fprintf (stdout, "\ndone.\n\n");

1.2       +6 -3      w3d/_test_huffman.c

Index: _test_huffman.c
===================================================================
RCS file: /usr/local/cvsroot/w3d/_test_huffman.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- _test_huffman.c	2001/06/25 07:56:07	1.1
+++ _test_huffman.c	2001/07/09 06:03:09	1.2
@@ -15,6 +15,8 @@
 
 #include <time.h>
 #include "rle.h"
+#include "mem.h"
+#include "mem.c"    /*  FIXME !!! */
 
 
 
@@ -44,7 +46,7 @@
       while (limit == 0)
          limit = rand() * 1.0 * MAX_COUNT / RAND_MAX;
 
-      val = (uint32_t*) malloc (limit * sizeof(uint32_t));
+      val = (uint32_t*) MALLOC (limit * sizeof(uint32_t));
 
      /**
       *   check encoding ...
@@ -62,7 +64,7 @@
 
          count = ENTROPY_ENCODER_FLUSH(&encoder);
 
-         bitstream = (uint8_t*) malloc (count * sizeof(uint32_t));
+         bitstream = (uint8_t*) MALLOC (count * sizeof(uint32_t));
          memcpy (bitstream, ENTROPY_CODER_BITSTREAM(&encoder), count);
 
          ENTROPY_ENCODER_DONE(&encoder);
@@ -83,7 +85,8 @@
          }
       }
 
-      free (val);
+      FREE (val);
+      FREE (bitstream);
    }
 
    fprintf (stdout, "\ndone.\n\n");

1.3       +6 -3      w3d/_test_rle.c

Index: _test_rle.c
===================================================================
RCS file: /usr/local/cvsroot/w3d/_test_rle.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- _test_rle.c	2001/07/02 08:28:45	1.2
+++ _test_rle.c	2001/07/09 06:03:09	1.3
@@ -14,6 +14,8 @@
 
 #include <time.h>
 #include "rle.h"
+#include "mem.h"
+#include "mem.c"      /*  FIXME !!  */
 
 
 
@@ -42,7 +44,7 @@
       while (limit == 0)
          limit = rand() * 1.0 * MAX_COUNT / RAND_MAX;
 
-      bit = (uint8_t*) malloc (limit);
+      bit = (uint8_t*) MALLOC (limit);
 
      /**
       *   check encoding ...
@@ -60,7 +62,7 @@
 
          count = ENTROPY_ENCODER_FLUSH(&encoder);
 
-         bitstream = (uint8_t*) malloc (count);
+         bitstream = (uint8_t*) MALLOC (count);
          memcpy (bitstream, ENTROPY_CODER_BITSTREAM(&encoder), count);
 
          ENTROPY_ENCODER_DONE(&encoder);
@@ -81,7 +83,8 @@
          }
       }
 
-      free (bit);
+      FREE (bit);
+      FREE (bitstream);
    }
 
    fprintf (stdout, "\ndone.\n\n");

1.4       +3 -7      w3d/bitcoder.h

Index: bitcoder.h
===================================================================
RCS file: /usr/local/cvsroot/w3d/bitcoder.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- bitcoder.h	2001/07/02 08:28:45	1.3
+++ bitcoder.h	2001/07/09 06:03:09	1.4
@@ -1,11 +1,8 @@
 #ifndef __BITCODER_H
 #define __BITCODER_H
 
-#include <stdlib.h>
-#include <stdint.h>
-#include <stdio.h>
+#include "mem.h"
 
-
 #if defined(BITCODER)
 
 #define OUTPUT_BIT(coder,bit)             bitcoder_write_bit(coder,bit)
@@ -40,7 +37,7 @@
    s->bit_count = 0;
    s->byte = 0;
    s->byte_count = 0;
-   s->bitstream = (uint8_t*) malloc (limit);
+   s->bitstream = (uint8_t*) MALLOC (limit);
    s->limit = limit;
 }
 
@@ -48,8 +45,7 @@
 static inline
 void bitcoder_encoder_done (BitCoderState *s)
 {
-//  XXX FIXME !!!
-//   free (s->bitstream);
+   FREE (s->bitstream);
 }
 
 

1.5       +1 -0      w3d/rle.h

Index: rle.h
===================================================================
RCS file: /usr/local/cvsroot/w3d/rle.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- rle.h	2001/07/02 08:28:45	1.4
+++ rle.h	2001/07/09 06:03:09	1.5
@@ -2,6 +2,7 @@
 #define __RLE_H
 
 #include <string.h>
+#include "mem.h"
 #include "bitcoder.h"
 
 #if defined(RLECODER)

1.3       +1 -5      w3d/tarkin-io.c

Index: tarkin-io.c
===================================================================
RCS file: /usr/local/cvsroot/w3d/tarkin-io.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- tarkin-io.c	2001/06/29 12:21:43	1.2
+++ tarkin-io.c	2001/07/09 06:03:09	1.3
@@ -6,11 +6,9 @@
  *   binary.
  */
 
-#include <stdlib.h>
 #include <unistd.h>
 #include <ctype.h>
-#include <string.h>
-
+#include "mem.h"
 #include "tarkin-io.h"
 
 
@@ -107,8 +105,6 @@
 int read_layer_descs (int fd, TarkinStream *s)
 {
    int i;
-
-   s->layer = (TarkinVideoLayer*) calloc (1, s->n_layers * sizeof(TarkinVideoLayer));
 
    for (i=0; i<s->n_layers; i++) {
       if (read (fd, &s->layer[i], sizeof(TarkinVideoLayer)) < sizeof(TarkinVideoLayer)) {

1.6       +19 -15    w3d/tarkin.c

Index: tarkin.c
===================================================================
RCS file: /usr/local/cvsroot/w3d/tarkin.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- tarkin.c	2001/07/02 08:28:45	1.5
+++ tarkin.c	2001/07/09 06:03:09	1.6
@@ -3,9 +3,7 @@
  *   (this one has to be rewritten to write ogg streams ...)
  */
 
-#include <stdlib.h>
-#include <string.h>
-
+#include "mem.h"
 #include "tarkin.h"
 #include "tarkin-io.h"
 #include "yuv.h"
@@ -17,7 +15,7 @@
 
 TarkinStream* tarkin_stream_new (int fd)
 {
-   TarkinStream *s = (TarkinStream*) calloc (1, sizeof(TarkinStream));
+   TarkinStream *s = (TarkinStream*) CALLOC (1, sizeof(TarkinStream));
 
    if (!s)
       return NULL;
@@ -36,18 +34,22 @@
    if (!s)
       return;
 
-   for (i=0; i<s->n_layers; i++)
-      if (s->layer[i].waveletbuf)
-         for (j=0; j<s->layer[i].n_comp; j++)
+   for (i=0; i<s->n_layers; i++) {
+      if (s->layer[i].waveletbuf) {
+         for (j=0; j<s->layer[i].n_comp; j++) {
             wavelet_3d_buf_destroy (s->layer[i].waveletbuf[j]);
+         }
+         FREE(s->layer[i].waveletbuf);
+      }
+   }
 
    if (s->layer)
-      free(s->layer);
+      FREE(s->layer);
 
    if (s->bitstream);
-      free(s->bitstream);
+      FREE(s->bitstream);
 
-   free(s);
+   FREE(s);
 }
 
 
@@ -61,7 +63,7 @@
    uint32_t i, j;
 
    s->n_layers = n_layers;
-   s->layer = (TarkinVideoLayer*) calloc (n_layers, sizeof(TarkinVideoLayer));
+   s->layer = (TarkinVideoLayer*) CALLOC (n_layers, sizeof(TarkinVideoLayer));
 
    for (i=0; i<n_layers; i++) {
       TarkinVideoLayer *layer = &s->layer[i];
@@ -94,7 +96,7 @@
          break;
       };
 
-      layer->waveletbuf = (Wavelet3DBuf**) calloc (layer->n_comp,
+      layer->waveletbuf = (Wavelet3DBuf**) CALLOC (layer->n_comp,
                                                    sizeof(Wavelet3DBuf*));
       for (j=0; j<layer->n_comp; j++)
          layer->waveletbuf[j] = wavelet_3d_buf_new (desc[i].width,
@@ -112,7 +114,7 @@
 
    err = write_layer_descs(s->fd, s);
 
-   s->bitstream = (uint8_t*) malloc (max_bitstream_len);
+   s->bitstream = (uint8_t*) MALLOC (max_bitstream_len);
 
    return err;
 }
@@ -182,6 +184,8 @@
    if (read_tarkin_header(s->fd, s) != TARKIN_OK)
       return 0;
 
+   s->layer = (TarkinVideoLayer*) CALLOC (1, s->n_layers * sizeof(TarkinVideoLayer));
+
    if (read_layer_descs(s->fd, s) != TARKIN_OK)
       return 0;
 
@@ -216,7 +220,7 @@
          BUG("unknown color format");
       };
 
-      layer->waveletbuf = (Wavelet3DBuf**) calloc (layer->n_comp,
+      layer->waveletbuf = (Wavelet3DBuf**) CALLOC (layer->n_comp,
                                                    sizeof(Wavelet3DBuf*));
       for (j=0; j<layer->n_comp; j++) {
          layer->waveletbuf[j] = wavelet_3d_buf_new (layer->desc.width,
@@ -232,7 +236,7 @@
           + 2 * 9 * sizeof(uint32_t) * layer->n_comp;
    }
 
-   s->bitstream = (uint8_t*) malloc (max_bitstream_len);
+   s->bitstream = (uint8_t*) MALLOC (max_bitstream_len);
 
    return s->n_layers;
 }

1.2       +5 -7      w3d/tarkin_dec.c

Index: tarkin_dec.c
===================================================================
RCS file: /usr/local/cvsroot/w3d/tarkin_dec.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- tarkin_dec.c	2001/06/29 09:19:25	1.1
+++ tarkin_dec.c	2001/07/09 06:03:09	1.2
@@ -2,12 +2,10 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
 #include <stdint.h>
-#include <string.h>
+#include "mem.h"
 #include "tarkin.h"
-#include "ppm.h"
+#include "pnm.h"
 
 
 
@@ -44,17 +42,17 @@
 
    tarkin_stream_get_layer_desc (tarkin_stream, 0, &layer);
 
-   rgb  = malloc (layer.width * layer.height * 3);
+   rgb  = MALLOC (layer.width * layer.height * 3);
 
    while (tarkin_stream_read_frame (tarkin_stream, &rgb) != 0xffffffff) {
       snprintf(ofname, 11, "out%03d.ppm", frame);
       printf ("write '%s'\n", ofname);
-      write_ppm (ofname, rgb, layer.width, layer.height);
+      write_pnm (ofname, rgb, layer.width, layer.height);
 
       frame ++;
    };
 
-   free (rgb);
+   FREE (rgb);
    tarkin_stream_destroy (tarkin_stream);
    close (fd);
 

1.5       +11 -8     w3d/tarkin_enc.c

Index: tarkin_enc.c
===================================================================
RCS file: /usr/local/cvsroot/w3d/tarkin_enc.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- tarkin_enc.c	2001/07/02 08:28:45	1.4
+++ tarkin_enc.c	2001/07/09 06:03:09	1.5
@@ -2,12 +2,10 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
 #include <stdint.h>
-#include <string.h>
+#include "mem.h"
 #include "tarkin.h"
-#include "ppm.h"
+#include "pnm.h"
 
 
 static
@@ -37,6 +35,7 @@
    int fd;
    TarkinStream *tarkin_stream;
    TarkinVideoLayerDesc layer [] = { { 0, 0, 1, 5000, TARKIN_RGB24 } };
+   TarkinColorFormat type;
 
    if (argc == 1) {
       layer[0].bitrate = 1000;
@@ -52,10 +51,14 @@
    }
 
    snprintf (fname, 256, fmt, 0);
-   if (read_ppm_info (fname, &layer[0].width, &layer[0].height) < 0)
+   type = read_pnm_header (fname, &layer[0].width, &layer[0].height);
+
+   if (type < 0)
       exit (-1);
+
+   layer[0].format = (type == 3) ? TARKIN_RGB24 : TARKIN_GRAYSCALE;
 
-   rgb  = malloc (layer[0].width * layer[0].height * 3);
+   rgb  = (uint8_t*) MALLOC (layer[0].width * layer[0].height * type);
 
    if ((fd = open ("stream.tarkin", O_CREAT | O_RDWR | O_TRUNC, 0644)) < 0) {
       printf ("error opening '%s' for writing !\n", "stream.tarkin");
@@ -71,7 +74,7 @@
       printf (fname, frame);
       printf ("'");
 
-      if (read_ppm (fname, rgb, layer[0].width, layer[0].height) < 0)
+      if (read_pnm (fname, rgb) < 0)
       {
          printf (" failed.\n");
          break;
@@ -82,7 +85,7 @@
       frame++;
    } while (1);
 
-   free (rgb);
+   FREE (rgb);
    tarkin_stream_destroy (tarkin_stream);
    close (fd);
 

1.8       +22 -23    w3d/wavelet.c

Index: wavelet.c
===================================================================
RCS file: /usr/local/cvsroot/w3d/wavelet.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- wavelet.c	2001/07/02 08:28:45	1.7
+++ wavelet.c	2001/07/09 06:03:09	1.8
@@ -1,5 +1,4 @@
-#include <stdlib.h>
-#include <string.h>
+#include "mem.h"
 #include "wavelet.h"
 #include "rle.h"
 
@@ -16,7 +15,7 @@
 Wavelet3DBuf* wavelet_3d_buf_new (uint32_t width, uint32_t height,
                                   uint32_t frames)
 {
-   Wavelet3DBuf* buf = (Wavelet3DBuf*) malloc (sizeof (Wavelet3DBuf));
+   Wavelet3DBuf* buf = (Wavelet3DBuf*) MALLOC (sizeof (Wavelet3DBuf));
    uint32_t _w = width;
    uint32_t _h = height;
    uint32_t _f = frames;
@@ -25,7 +24,7 @@
    if (!buf)
       return NULL;
 
-   buf->data = (TYPE*) malloc (width * height * frames * sizeof (TYPE));
+   buf->data = (TYPE*) MALLOC (width * height * frames * sizeof (TYPE));
 
    if (!buf->data) {
       wavelet_3d_buf_destroy (buf);
@@ -44,12 +43,12 @@
       _f = (_f+1)/2;
    }
 
-   buf->w = (uint32_t*) malloc (buf->scales * sizeof (uint32_t));
-   buf->h = (uint32_t*) malloc (buf->scales * sizeof (uint32_t));
-   buf->f = (uint32_t*) malloc (buf->scales * sizeof (uint32_t));
-   buf->offset = (uint32_t (*) [8]) malloc (8 * buf->scales * sizeof (uint32_t));
+   buf->w = (uint32_t*) MALLOC (buf->scales * sizeof (uint32_t));
+   buf->h = (uint32_t*) MALLOC (buf->scales * sizeof (uint32_t));
+   buf->f = (uint32_t*) MALLOC (buf->scales * sizeof (uint32_t));
+   buf->offset = (uint32_t (*) [8]) MALLOC (8 * buf->scales * sizeof (uint32_t));
 
-   buf->scratchbuf = (TYPE*) malloc (MAX3(width, height, frames) * sizeof (TYPE));
+   buf->scratchbuf = (TYPE*) MALLOC (MAX3(width, height, frames) * sizeof (TYPE));
 
    if (!buf->w || !buf->h || !buf->f || !buf->offset || !buf->scratchbuf) {
       wavelet_3d_buf_destroy (buf);
@@ -82,18 +81,18 @@
 {
    if (buf) {
       if (buf->data)
-         free (buf->data);
+         FREE (buf->data);
       if (buf->w)
-         free (buf->w);
+         FREE (buf->w);
       if (buf->h)
-         free (buf->h);
+         FREE (buf->h);
       if (buf->f)
-         free (buf->f);
+         FREE (buf->f);
       if (buf->offset)
-         free (buf->offset);
+         FREE (buf->offset);
       if (buf->scratchbuf)
-         free (buf->scratchbuf);
-      free (buf);
+         FREE (buf->scratchbuf);
+      FREE (buf);
    }
 }
 
@@ -135,13 +134,13 @@
 
    do {
       i--;
-      significance = INPUT_BIT(&significand_bitstream[i]) << i;
+      significance = 0; //INPUT_BIT(&significand_bitstream[i]) << i;
    } while (!significance && i > 0);
 
-   sign = INPUT_BIT(&significand_bitstream[i]);
+   sign = 0; //INPUT_BIT(&significand_bitstream[i]);
 
    while (--i >= 0) {
-      significance |= INPUT_BIT(&insignificand_bitstream[i]) << i;
+      significance |= 0; //INPUT_BIT(&insignificand_bitstream[i]) << i;
    };
 
    return (significance ^ mask[sign]);
@@ -256,7 +255,7 @@
          decode_quadrant (buf,level,7,w1,h1,f1,s_stream,i_stream);
    }
 }
-#endif
+#else
 
 static
 void encode_coefficients (const Wavelet3DBuf* buf,
@@ -279,7 +278,7 @@
    for (i=0; i<buf->width*buf->height*buf->frames; i++)
       buf->data[i] = decode_coeff(s_stream, i_stream);
 }
-
+#endif
 
 
 static
@@ -510,7 +509,7 @@
 
 #if defined(DBG_XFORM)
 
-#include "ppm.h"
+#include "pnm.h"
 
 void wavelet_3d_buf_dump (char *fmt,
                           uint32_t first_frame_in_buf,
@@ -523,7 +522,7 @@
    for (f=0; f<buf->frames; f++) {
       snprintf (fname, 256, fmt, id, first_frame_in_buf + f);
 
-      write_ppm16 (fname, buf->data + f * buf->width * buf->height,
+      write_pgm16 (fname, buf->data + f * buf->width * buf->height,
                    buf->width, buf->height);
    }
 }

1.3       +1 -1      w3d/wavelet_xform.c

Index: wavelet_xform.c
===================================================================
RCS file: /usr/local/cvsroot/w3d/wavelet_xform.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- wavelet_xform.c	2001/07/02 08:28:45	1.2
+++ wavelet_xform.c	2001/07/09 06:03:10	1.3
@@ -1,4 +1,4 @@
-#include <stdlib.h>
+#include "mem.h"
 #include <assert.h>
 #include "wavelet.h"
 

1.1                  w3d/mem.c

Index: mem.c
===================================================================
/*
 *   Debugging implementation of MALLOC and friends
 */

#include "mem.h"

#if defined(DBG_MEMLEAKS)

typedef struct {
   void *mem;
   char *allocated_in_func;
   char *allocated_in_file;
   unsigned int allocated_in_line;
} MemDesc;

static int initialized = 0;
static int alloc_count = 0;
static MemDesc *alloc_list = NULL;

static
void dbg_memleaks_done (int exitcode, void *dummy)
{
   unsigned int i;
   (void) dummy;

   if (exitcode == 0 && alloc_count != 0) {
      fprintf (stderr, "\nmemory leak detected !!!\n");
      fprintf (stderr, "\nalloc_count == %i\n\n", alloc_count);
      for (i=0; i<alloc_count; i++) {
         MemDesc *d = &alloc_list[i];
         fprintf (stderr, "chunk %p allocated in %s (%s: %u) not free'd !!\n",
                  d->mem, d->allocated_in_func, d->allocated_in_file,
                  d->allocated_in_line);
      }
      free(alloc_list);
   }
   fprintf (stderr, "\n");
}

static
void dbg_memleaks_init (void)
{
   on_exit (dbg_memleaks_done, NULL);
   initialized = 1;
}

void* dbg_malloc (char* file, int line, char *func, size_t bytes)
{
   void *mem = (void*) malloc (bytes);
   MemDesc *d;

   if (!initialized)
      dbg_memleaks_init();

   alloc_count++;
   alloc_list = realloc (alloc_list, alloc_count * sizeof(MemDesc));

   d = &alloc_list[alloc_count-1];
   d->mem = mem;
   d->allocated_in_func = func;
   d->allocated_in_file = file;
   d->allocated_in_line = line;

   return mem;
}

void* dbg_calloc (char* file, int line, char *func, size_t count, size_t bytes)
{
   void *mem = (void*) calloc (count, bytes);
   MemDesc *d;

   if (!initialized)
      dbg_memleaks_init();

   alloc_count++;
   alloc_list = realloc (alloc_list, alloc_count * sizeof(MemDesc));

   d = &alloc_list[alloc_count-1];
   d->mem = mem;
   d->allocated_in_func = func;
   d->allocated_in_file = file;
   d->allocated_in_line = line;

   return mem;
}

void* dbg_realloc (char *file, int line, char *func, char *what,
                   void *mem, size_t bytes)
{
   unsigned int i;

   for (i=0; i<alloc_count; i++) {
      if (alloc_list[i].mem == mem) {
         alloc_list[i].mem = (void*) realloc (mem, bytes);
         return alloc_list[i].mem;
      }
   }

   if (mem != NULL) {
      fprintf (stderr,
               "%s: trying to reallocate unknown chunk %p (%s)\n"
               "          in %s (%s: %u) !!!\n",
               __FUNCTION__, mem, what, func, file, line);
      exit (-1);
   }

   return dbg_malloc(file, line, func, bytes);
}

void dbg_free (char *file, int line, char *func, char *what, void *mem)
{
   unsigned int i;

   if (!initialized)
      dbg_memleaks_init();

   for (i=0; i<alloc_count; i++) {
      if (alloc_list[i].mem == mem) {
         free (mem);
         alloc_count--;
         memmove (&alloc_list[i], &alloc_list[i+1],
                  (alloc_count - i) * sizeof(MemDesc));
         return;
      }
   }

   fprintf (stderr, "%s: trying to free unknown chunk %p (%s)\n"
            "          in %s (%s: %u) !!!\n",
            __FUNCTION__, mem, what, func, file, line);
   exit (-1);
}

#endif

1.1                  w3d/mem.h

Index: mem.h
===================================================================
#ifndef __MEM_H
#define __MEM_H

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#if defined(DBG_MEMLEAKS)

extern void* dbg_malloc (char *file, int line, char *func, size_t bytes);
extern void* dbg_calloc (char *file, int line, char *func, size_t count, size_t bytes);
extern void* dbg_realloc (char *file, int line, char *func, char *what, void *mem, size_t bytes);
extern void dbg_free (char *file, int line, char *func, char *what, void *mem);

#define MALLOC(bytes)        dbg_malloc(__FILE__,__LINE__,__FUNCTION__,bytes)
#define CALLOC(count,bytes)  dbg_calloc(__FILE__,__LINE__,__FUNCTION__,count,bytes)
#define FREE(mem)            dbg_free(__FILE__,__LINE__,__FUNCTION__,#mem,mem)
#define REALLOC(mem,bytes)   dbg_realloc(__FILE__,__LINE__,__FUNCTION__,#mem,mem,bytes)

#else

#define MALLOC malloc
#define CALLOC calloc
#define REALLOC realloc
#define FREE free

#endif

#endif

1.1                  w3d/pnm.c

Index: pnm.c
===================================================================
/**
 *   Thanks to Simon who contributed the code to make these functions 
 *   CR/LF safe ! 
 */

#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include "mem.h"

static
int eat_ws_comments(FILE *fp)
{
   int c;

   while ((c = fgetc(fp)) != EOF) {
      if (c == '#') {                    /* eat the comment */
         do {
            c = fgetc(fp);
            if (c == EOF)                /* but bail if we hit EOF */
               return -1;
         } while (c != '\n');
      }

      if (!isspace(c)) {                 /* we are done */
         return ungetc(c,fp);
      }
   }

   return -1;  /* fell out of the loop */
}

/* this is a kludge, but raw pnm's are not well defined... */
static
int eat_ws_to_eol (FILE *fp)
{
   int c;

   do {
      c = fgetc(fp);

      if (c == '\n' || c == '\r') /* some dos/win pnms generated with */
         return c;                /* just '\r' after maxval */
   } while (isspace(c));

   return ungetc(c,fp);
}

static
int read_pnm_header_internal (FILE *fp, int *w, int *h)
{
   int type, maxval;

   if (fgetc(fp) != 'P')
      return -1;

   type = fgetc(fp) - '0';

   if (type < 0 || type > 6)
      return -1;               /* invalid type */

   eat_ws_comments(fp);
   fscanf(fp, "%d", w);

   eat_ws_comments(fp);
   fscanf(fp, "%d", h);

   eat_ws_comments(fp);
   fscanf(fp, "%d", &maxval);
   eat_ws_to_eol(fp);

   return type;
}

int read_pnm_header (char *fname, int *w, int *h)
{
   int type;
   FILE *fp = fopen(fname, "r");

   if (!fp)
      return -1;

   type = read_pnm_header_internal (fp, w, h);

   fclose (fp);

   return (type == 3 || type == 6) ? 3 : 1;
}

int read_pnm (char *fname, uint8_t *buf)
{
   FILE *fp = fopen(fname, "r");
   int type;
   int w, h;

   if (!fp)
      return -1;

   type = read_pnm_header_internal (fp, &w, &h);

   switch (type) {
   case 5:
      if (fread (buf, 1, w*h, fp) != w*h)
         return -1;
      break;
   case 6:
      if (fread (buf, 3, w*h, fp) != w*h)
         return -1;
      break;
   default:
      fprintf (stderr, "%s: unimplemented image format !!!\n", __FUNCTION__);
   };

   fclose (fp);

   return 0;
}

void write_pnm (char *fname, uint8_t *rgb, int w, int h)
{
   FILE *outfile;

   outfile = fopen (fname, "w");

   if (!outfile) {
      fprintf (stderr, "error opening '%s' for writing !!!\n", fname);
      exit (-1);
   }

   if (strstr(fname, ".ppm") != fname + strlen(fname)-4) {
      fprintf (outfile, "P6\n%d %d\n%d\n", w, h, 255);
      fwrite (rgb, 3, w*h, outfile);
   } else if (strstr(fname, ".pgm") != fname + strlen(fname)-4) {
      fprintf (outfile, "P5\n%d %d\n%d\n", w, h, 255);
      fwrite (rgb, 1, w*h, outfile);
   } else {
      fprintf (stderr, 
               "%s: can't write anything else than .ppm's and .pgm's !\n",
               __FUNCTION__);
      exit (-1);
   }

   fclose (outfile);
}

static inline
uint8_t CLAMP(int16_t x)
{
   x *= 4;
   x += 128;
   return  ((x > 255) ? 255 : (x < 0) ? 0 : x);
}

void write_pgm16 (char *fname, int16_t *rgb, int w, int h)
{
   int i;
   FILE *outfile;

   outfile = fopen (fname, "w");

   if (!outfile) {
      printf ("error opening '%s' for writing !!!\n", fname);
      exit (-1);
   }

   fprintf (outfile, "P5\n%d %d\n%d\n", w, h, 255);
   for (i=0; i<w*h; i++) {
      uint8_t c = CLAMP(rgb[i]);
      fwrite (&c, 1, 1, outfile);
   }
   fclose (outfile);
}

1.1                  w3d/pnm.h

Index: pnm.h
===================================================================
#ifndef __PPM_H
#define __PPM_H

/**
 *   Returns number of color channels (1 == grayscale, 3 == rgb)
 *   or -1 on error.
 */
extern int read_pnm_header (char *fname, int *w, int *h);

/**
 *   Read pnm into buf. Assumes you have called read_pnm_header()
 *   and allocated buf before.
 */
extern int read_pnm (char *fname, uint8_t *buf);

/**
 *   Write buf into pnm file. Depending of the suffix of fname we
 *   assume 1 channel for '.png' and 3 channels for '.ppm'
 */
extern void write_pnm (char *fname, uint8_t *buf, int w, int h);

/**
 *   Write a int16_t buf into pgm file
 */
extern void write_pgm16 (char *fname, int16_t *buf, int w, int h);

#endif

1.2       +1 -1      w3d/tools/Makefile

Index: Makefile
===================================================================
RCS file: /usr/local/cvsroot/w3d/tools/Makefile,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Makefile	2001/07/02 08:28:46	1.1
+++ Makefile	2001/07/09 06:03:12	1.2
@@ -6,7 +6,7 @@
 
 TARGETS = ppmdiff
 
-OBJS = $(TARGETS:=.o) ../ppm.o
+OBJS = $(TARGETS:=.o) ../pnm.o
 SRCS = $(OBJS:.o=.c)
 
 

1.2       +15 -14    w3d/tools/ppmdiff.c

Index: ppmdiff.c
===================================================================
RCS file: /usr/local/cvsroot/w3d/tools/ppmdiff.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ppmdiff.c	2001/07/02 08:28:46	1.1
+++ ppmdiff.c	2001/07/09 06:03:12	1.2
@@ -11,7 +11,8 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <string.h>
-#include "ppm.h"
+#include "mem.h"
+#include "pnm.h"
 #include "bitcoder.h"
 
 
@@ -29,34 +30,34 @@
 int main (int argc, char **argv)
 {
    uint8_t *rgb1, *rgb2, *diff;
-   uint32_t width, height, width1, height1;
+   uint32_t width, height, width1, height1, n_chan1, n_chan;
    uint32_t i;
 
    if (argc != 3)
       usage (argv[0]);
 
-   if (read_ppm_info (argv[1], &width1, &height1) < 0)
+   if ((n_chan1 = read_pnm_header (argv[1], &width1, &height1)) < 0)
       exit (-1);
 
-   if (read_ppm_info (argv[2], &width, &height) < 0)
+   if ((n_chan = read_pnm_header (argv[2], &width, &height)) < 0)
       exit (-1);
 
-   if (!(width1 == width && height1 == height)) {
+   if (!(width1 == width && height1 == height && n_chan1 == n_chan)) {
       printf ("image sizes differ !!\n");
       exit (-1);
    }
 
-   rgb1  = malloc (width * height * 3);
-   rgb2  = malloc (width * height * 3);
-   diff  = malloc (width * height * 3);
+   rgb1  = MALLOC (width * height * n_chan);
+   rgb2  = MALLOC (width * height * n_chan);
+   diff  = MALLOC (width * height * n_chan);
 
-   if (read_ppm (argv[1], rgb1, width, height) < 0)
+   if (read_pnm (argv[1], rgb1) < 0)
    {
       printf ("error opening '%s' !\n", argv[1]);
       exit(-1);
    }
 
-   if (read_ppm (argv[2], rgb2, width, height) < 0)
+   if (read_pnm (argv[2], rgb2) < 0)
    {
       printf ("error opening '%s' !\n", argv[2]);
       exit(-1);
@@ -71,11 +72,11 @@
       }
    }
 
-   write_ppm ("diff.ppm", diff, width, height);
+   write_pnm ("diff.ppm", diff, width, height);
 
-   free (rgb1);
-   free (rgb2);
-   free (diff);
+   FREE (rgb1);
+   FREE (rgb2);
+   FREE (diff);
 
    return 0;
 }

--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list