From 3c788c26cf354252f806885bcf8ca2fbd4b1d20b Mon Sep 17 00:00:00 2001 From: giraffedata Date: Fri, 26 Feb 2010 21:24:36 +0000 Subject: Fix garbage output, use packed PBM routines git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1132 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- generator/pbmpage.c | 47 +++++++++++++++-------------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/generator/pbmpage.c b/generator/pbmpage.c index e10ee6d6..77732df6 100644 --- a/generator/pbmpage.c +++ b/generator/pbmpage.c @@ -33,8 +33,7 @@ struct bitmap { int Width; /* width and height in 600ths of an inch */ int Height; - int Pwidth; /* width in bytes */ - char *bitmap; + bit ** bitmap; }; static struct bitmap bitmap; @@ -46,7 +45,6 @@ setpixel(int const x, int const y, int const c) { - int const charidx = y * bitmap.Pwidth + x/8; char const bitmask = 128 >> (x % 8); if (x < 0 || x >= bitmap.Width) @@ -55,9 +53,9 @@ setpixel(int const x, return; if (c) - bitmap.bitmap[charidx] |= bitmask; + bitmap.bitmap[y][x/8] |= bitmask; else - bitmap.bitmap[charidx] &= ~bitmask; + bitmap.bitmap[y][x/8] &= ~bitmask; } @@ -161,36 +159,15 @@ outputPbm(FILE * const file, Create a pbm file containing the image from the global variable bitmap[]. -----------------------------------------------------------------------------*/ int const forceplain = 0; - bit *pbmrow; int row; - int bitmap_cursor; pbm_writepbminit(file, bitmap.Width, bitmap.Height, forceplain); - - /* We round the allocated row space up to a multiple of 8 so the ugly - fast code below can work. - */ - pbmrow = pbm_allocrow(((bitmap.Width+7)/8)*8); - bitmap_cursor = 0; - for (row = 0; row < bitmap.Height; row++) { - int col; - for (col = 0; col < bitmap.Width;) { - /* A little ugliness makes a big speed difference here. */ - pbmrow[col++] = bitmap.bitmap[bitmap_cursor] & (1<<7); - pbmrow[col++] = bitmap.bitmap[bitmap_cursor] & (1<<6); - pbmrow[col++] = bitmap.bitmap[bitmap_cursor] & (1<<5); - pbmrow[col++] = bitmap.bitmap[bitmap_cursor] & (1<<4); - pbmrow[col++] = bitmap.bitmap[bitmap_cursor] & (1<<3); - pbmrow[col++] = bitmap.bitmap[bitmap_cursor] & (1<<2); - pbmrow[col++] = bitmap.bitmap[bitmap_cursor] & (1<<1); - pbmrow[col++] = bitmap.bitmap[bitmap_cursor] & (1<<0); - - bitmap_cursor++; - } - pbm_writepbmrow(file, pbmrow, bitmap.Width, forceplain); + for (row = 0; row < bitmap.Height; row++ ) { + pbm_writepbmrow_packed(file, bitmap.bitmap[row], + bitmap.Width, forceplain); } - pbm_freerow(pbmrow); + pm_close(file); } @@ -243,8 +220,12 @@ main(int argc,char** argv) { bitmap.Width = Width; bitmap.Height = Height; - bitmap.Pwidth = (Width + 7) / 8; - bitmap.bitmap = malloc(bitmap.Pwidth * bitmap.Height); + bitmap.bitmap = pbm_allocarray_packed(Width, bitmap.Height); + + for (y = 0; y < bitmap.Height; ++y) { + for (x = 0; x < pbm_packed_bytes(bitmap.Width); ++x) + bitmap.bitmap[y][x] = 0x00; + } if (argc>1) TP = atoi(argv[1]); @@ -287,5 +268,7 @@ main(int argc,char** argv) { outputPbm(stdout, bitmap); + pbm_freearray(bitmap.bitmap, Height); + return 0; } -- cgit 1.4.1