diff options
Diffstat (limited to 'converter')
-rw-r--r-- | converter/other/ipdb.c | 222 | ||||
-rw-r--r-- | converter/other/ipdb.h | 23 | ||||
-rw-r--r-- | converter/other/pamtopdbimg.c | 76 | ||||
-rw-r--r-- | converter/other/pamtoxvmini.c | 77 | ||||
-rw-r--r-- | converter/other/pdbimgtopam.c | 127 | ||||
-rw-r--r-- | converter/other/pnmtorle.c | 276 | ||||
-rw-r--r-- | converter/other/pnmtosir.c | 35 | ||||
-rw-r--r-- | converter/ppm/picttoppm.c | 1682 | ||||
-rw-r--r-- | converter/ppm/ppmtoapplevol.c | 2 | ||||
-rw-r--r-- | converter/ppm/ppmtoilbm.c | 210 |
10 files changed, 1487 insertions, 1243 deletions
diff --git a/converter/other/ipdb.c b/converter/other/ipdb.c index 5e4dc82e..d524c7e9 100644 --- a/converter/other/ipdb.c +++ b/converter/other/ipdb.c @@ -23,15 +23,12 @@ #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */ #define _BSD_SOURCE /* Ensure strdup() is in <string.h> */ #include <assert.h> -#include <time.h> #include <string.h> #include "mallocvar.h" #include "nstring.h" -#include "ipdb.h" - -typedef uint32_t pilot_time_t; +#include "ipdb.h" @@ -49,7 +46,7 @@ imgPpb(IMAGE * const imgP) { unsigned int -ipdb_img_ppb(IMAGE * const imgP) { +ipdb_imgPpb(IMAGE * const imgP) { /*---------------------------------------------------------------------------- Pixels per byte -----------------------------------------------------------------------------*/ @@ -59,7 +56,7 @@ ipdb_img_ppb(IMAGE * const imgP) { size_t -ipdb_img_size(IMAGE * const imgP) { +ipdb_imgSize(IMAGE * const imgP) { /*---------------------------------------------------------------------------- Size (in bytes) of an image's data. -----------------------------------------------------------------------------*/ @@ -71,115 +68,97 @@ ipdb_img_size(IMAGE * const imgP) { /* * Return the start of row `r'. */ - uint8_t * - ipdb_img_row(IMAGE * const imgP, - unsigned int const row) { - - return &imgP->data[(row) * imgP->width / imgPpb(imgP)]; - } - - +uint8_t * +ipdb_imgRow(IMAGE * const imgP, + unsigned int const row) { - #define img_row(i, r) - - static pilot_time_t const unixepoch = (66*365+17)*24*3600; - /* The unix epoch in Mac time (the Mac epoch is 00:00 UTC 1904.01.01). - The 17 is the number of leap years. - */ - - static const char * const errorDesc[] = { - /* E_BADCOLORS */ - "Invalid palette, only {0x00, 0x55, 0xAA, 0xFF} allowed.", + return &imgP->data[(row) * imgP->width / imgPpb(imgP)]; +} - /* E_NOTIMAGE */ - "Not an image file.", - /* E_IMAGETHERE */ - "Image record already present, logic error.", - /* E_IMAGENOTTHERE */ - "Image record required before text record, logic error.", +static const char * const errorDesc[] = { + /* E_BADCOLORS */ + "Invalid palette, only {0x00, 0x55, 0xAA, 0xFF} allowed.", - /* E_TEXTTHERE */ - "Text record already present, logic error.", + /* E_NOTIMAGE */ + "Not an image file.", - /* E_NOTRECHDR */ - "Invalid record header encountered.", + /* E_IMAGETHERE */ + "Image record already present, logic error.", - /* E_UNKNOWNRECHDR */ - "Unknown record header.", + /* E_IMAGENOTTHERE */ + "Image record required before text record, logic error.", - /* E_TOOBIGG */ - "Image too big, maximum size approx. 640*400 gray pixels.", + /* E_TEXTTHERE */ + "Text record already present, logic error.", - /* E_TOOBIGM */ - "Image too big, maximum size approx. 640*800 monochrome pixels.", - }; + /* E_NOTRECHDR */ + "Invalid record header encountered.", + /* E_UNKNOWNRECHDR */ + "Unknown record header.", + /* E_TOOBIGG */ + "Image too big, maximum size approx. 640*400 gray pixels.", - const char * - ipdb_err(int const e) { + /* E_TOOBIGM */ + "Image too big, maximum size approx. 640*800 monochrome pixels.", +}; - if (e < 0) - return e >= E_LAST ? errorDesc[-e - 1] : "unknown error"; - else - return strerror(e); - } +const char * +ipdb_err(int const e) { - static void - rechdr_free(RECHDR * const recP) { + if (e < 0) + return e >= E_LAST ? errorDesc[-e - 1] : "unknown error"; + else + return strerror(e); +} - if (recP) { - free(recP->extra); - free(recP); - } - } +static void +rechdr_free(RECHDR * const recP) { - void - ipdb_image_free(IMAGE * const imgP) { + if (recP) { + free(recP->extra); + free(recP); + } +} - if (imgP) { - rechdr_free(imgP->r); - free(imgP->data); - free(imgP); - } - } +void +ipdb_imageFree(IMAGE * const imgP) { - void - ipdb_text_free(TEXT * const textP) { + if (imgP) { + rechdr_free(imgP->r); + free(imgP->data); + free(imgP); + } +} - if (textP) { - rechdr_free(textP->r); - free(textP->data); - free(textP); - } - } +void +ipdb_textFree(TEXT * const textP) { - void - ipdb_pdbhead_free(PDBHEAD * const headP) { + if (textP) { + rechdr_free(textP->r); + if (textP->data) + free(textP->data); + free(textP); + } +} - free(headP); - } +void +ipdb_pdbheadFree(PDBHEAD * const headP) { - void - ipdb_clear(IPDB * const pdbP) { - - if (pdbP) { - ipdb_image_free(pdbP->i); - ipdb_text_free(pdbP->t); - ipdb_pdbhead_free(pdbP->p); - } + free(headP); } @@ -187,14 +166,22 @@ ipdb_img_size(IMAGE * const imgP) { void ipdb_free(IPDB * const pdbP) { - ipdb_clear(pdbP); + if (pdbP->i) + ipdb_imageFree(pdbP->i); + + if (pdbP->t) + ipdb_textFree(pdbP->t); + + if (pdbP->p) + ipdb_pdbheadFree(pdbP->p); + free(pdbP); } PDBHEAD * -ipdb_pdbhead_alloc(const char * const name) { +ipdb_pdbheadAlloc() { PDBHEAD * pdbHeadP; @@ -202,20 +189,6 @@ ipdb_pdbhead_alloc(const char * const name) { if (pdbHeadP) { MEMSZERO(pdbHeadP); - - STRSCPY(pdbHeadP->name, name == NULL ? "unnamed" : name); - - /* - * All of the Image Viewer pdb files that I've come across have - * 3510939142U (1997.08.16 14:38:22 UTC) here. I don't know where - * this bizarre date comes from but the real date works fine so - * I'm using it. - */ - pdbHeadP->ctime = - pdbHeadP->mtime = (pilot_time_t)time(NULL) + unixepoch; - - MEMSCPY(&pdbHeadP->type, IPDB_vIMG); - MEMSCPY(&pdbHeadP->id, IPDB_View); } return pdbHeadP; } @@ -223,7 +196,7 @@ ipdb_pdbhead_alloc(const char * const name) { static RECHDR * -rechdr_alloc(int const type, +rechdrCreate(int const type, uint32_t const offset) { /* @@ -234,7 +207,7 @@ rechdr_alloc(int const type, RECHDR * recHdrP; MALLOCVAR(recHdrP); - + if (recHdrP) { MEMSSET(recHdrP, 0); @@ -255,10 +228,10 @@ rechdr_alloc(int const type, IMAGE * -ipdb_image_alloc(const char * const name, - int const type, - int const w, - int const h) { +ipdb_imageCreate(const char * const name, + int const type, + int const w, + int const h) { bool failed; IMAGE * imgP; @@ -277,7 +250,7 @@ ipdb_image_alloc(const char * const name, imgP->width = w; imgP->height = h; - imgP->r = rechdr_alloc(IMG_REC, IMGOFFSET); + imgP->r = rechdrCreate(IMG_REC, IMGOFFSET); if (imgP->r) { if (w != 0 && h != 0) { @@ -292,10 +265,10 @@ ipdb_image_alloc(const char * const name, rechdr_free(imgP->r); } else failed = true; - + if (failed) - ipdb_image_free(imgP); - } else + ipdb_imageFree(imgP); + } else failed = true; return failed ? NULL : imgP; @@ -304,7 +277,7 @@ ipdb_image_alloc(const char * const name, TEXT * -ipdb_text_alloc(const char * const content) { +ipdb_textAlloc(void) { TEXT * textP; bool failed; @@ -320,22 +293,13 @@ ipdb_text_alloc(const char * const content) { if (textP) { MEMSZERO(textP); - textP->r = rechdr_alloc(TEXT_REC, 0); + textP->r = rechdrCreate(TEXT_REC, 0); - if (textP->r) { - if (content) { - textP->data = strdup(content); - - if (!textP->data) - failed = true; - } - if (failed) - rechdr_free(textP->r); - } else + if (textP->r == NULL) failed = true; if (failed) - ipdb_text_free(textP); + free(textP); } else failed = true; @@ -345,7 +309,7 @@ ipdb_text_alloc(const char * const content) { IPDB * -ipdb_alloc(const char * const name) { +ipdb_alloc(void) { IPDB * pdbP; bool failed; @@ -357,12 +321,11 @@ ipdb_alloc(const char * const name) { if (pdbP) { MEMSZERO(pdbP); - if (name) { - pdbP->p = ipdb_pdbhead_alloc(name); + pdbP->p = ipdb_pdbheadAlloc(); + + if (!pdbP->p) + failed = true; - if (!pdbP->p) - failed = true; - } if (failed) ipdb_free(pdbP); } else @@ -383,3 +346,6 @@ ipdb_typeName(uint8_t const type) { default: return "???"; } } + + + diff --git a/converter/other/ipdb.h b/converter/other/ipdb.h index 6af5fc44..59e5d266 100644 --- a/converter/other/ipdb.h +++ b/converter/other/ipdb.h @@ -201,43 +201,40 @@ const char * ipdb_err(int error); size_t -ipdb_img_size(IMAGE * const imgP); +ipdb_imgSize(IMAGE * const imgP); unsigned int -ipdb_img_ppb(IMAGE * const imgP); +ipdb_imgPpb(IMAGE * const imgP); uint8_t * -ipdb_img_row(IMAGE * const imgP, +ipdb_imgRow(IMAGE * const imgP, unsigned int const row); void ipdb_free(IPDB *); IPDB * -ipdb_alloc(const char *); - -void -ipdb_clear(IPDB * const pdbP); +ipdb_alloc(void); PDBHEAD * -ipdb_pdbhead_alloc(const char * const name); +ipdb_pdbheadAlloc(void); void -ipdb_pdbhead_free(PDBHEAD * const headP); +ipdb_pdbheadFree(PDBHEAD * const headP); IMAGE * -ipdb_image_alloc(const char * const name, +ipdb_imageCreate(const char * const name, int const type, int const w, int const h); void -ipdb_image_free(IMAGE * const imgP); +ipdb_imageFree(IMAGE * const imgP); void -ipdb_text_free(TEXT * const textP); +ipdb_textFree(TEXT * const textP); TEXT * -ipdb_text_alloc(const char * const content); +ipdb_textAlloc(void); #endif diff --git a/converter/other/pamtopdbimg.c b/converter/other/pamtopdbimg.c index 4823686f..da0f5064 100644 --- a/converter/other/pamtopdbimg.c +++ b/converter/other/pamtopdbimg.c @@ -32,6 +32,7 @@ #include <stdlib.h> #include <assert.h> #include <string.h> +#include <time.h> #include <sys/stat.h> #include "pm_c_util.h" @@ -45,7 +46,7 @@ enum CompMode {COMPRESSED, MAYBE, UNCOMPRESSED}; -struct cmdlineInfo { +struct CmdlineInfo { /* All the information the user supplied in the command line, in a form easy for the program to use. */ @@ -54,13 +55,14 @@ struct cmdlineInfo { const char * notefile; /* NULL if not specified */ enum CompMode compMode; unsigned int depth4; + unsigned int fixedtime; }; static void parseCommandLine(int argc, const char ** argv, - struct cmdlineInfo * const cmdlineP) { + struct CmdlineInfo * const cmdlineP) { /*---------------------------------------------------------------------------- parse program command line described in Unix standard form by argc and argv. Return the information in the options as *cmdlineP. @@ -96,6 +98,8 @@ parseCommandLine(int argc, const char ** argv, &uncompressed, 0); OPTENT3(0, "4depth", OPT_FLAG, NULL, &cmdlineP->depth4, 0); + OPTENT3(0, "fixedtime", OPT_FLAG, NULL, + &cmdlineP->fixedtime, 0); opt.opt_table = option_def; opt.short_allowed = false; /* We have no short (old-fashioned) options */ @@ -144,6 +148,42 @@ parseCommandLine(int argc, const char ** argv, +static uint32_t const unixepoch = (66*365+17)*24*3600; + /* The unix epoch in Mac time (the Mac epoch is 00:00 UTC 1904.01.01). + The 17 is the number of leap years. + */ + + + +static void +setPdbHeader(PDBHEAD * const pdbHeadP, + const char * const name, + bool const wantFixedTime) { + + STRSCPY(pdbHeadP->name, name); + + { + /* + * All of the Image Viewer pdb files that I've come across have + * 3510939142U (1997.08.16 14:38:22 UTC) here. I don't know where + * this bizarre datetime comes from but the real date works fine so + * I'm using it unless user asked for a fixed time (probably just so he + * gets repeatable output). + */ + + uint32_t const stdTime = 3510939142U; + + uint32_t const hdrDt = + wantFixedTime ? stdTime : (uint32_t)time(NULL) + unixepoch; + + pdbHeadP->ctime = pdbHeadP->mtime = hdrDt; + } + MEMSCPY(&pdbHeadP->type, IPDB_vIMG); + MEMSCPY(&pdbHeadP->id, IPDB_View); +} + + + static int pdbheadWrite(PDBHEAD * const pdbheadP, FILE * const fileP) { @@ -249,9 +289,9 @@ compressIfRequired(IPDB * const pdbP, if (comp == IPDB_NOCOMPRESS) { *compressedDataP = pdbP->i->data; - *compressedSizeP = ipdb_img_size(pdbP->i); + *compressedSizeP = ipdb_imgSize(pdbP->i); } else { - int const uncompressedSz = ipdb_img_size(pdbP->i); + int const uncompressedSz = ipdb_imgSize(pdbP->i); unsigned char * outbuf; size_t compressedSz; @@ -475,7 +515,7 @@ imageInsertInit(IPDB * const pdbP, ipdb_typeName(type), MAX_SIZE(type)); else { pdbP->i = - ipdb_image_alloc(name, type, adjustedWidth, adjustedHeight); + ipdb_imageCreate(name, type, adjustedWidth, adjustedHeight); if (pdbP->i == NULL) pm_message("Could not get memory for %u x %u image", adjustedWidth, adjustedHeight); @@ -582,7 +622,7 @@ insertMimage(IPDB * const pdbP, static int insertText(IPDB * const pdbP, - const char * const s) { + const char * const content) { int retval; @@ -591,17 +631,23 @@ insertText(IPDB * const pdbP, else if (pdbP->p->num_recs == 2) retval = E_TEXTTHERE; else { - pdbP->t = ipdb_text_alloc(s); + pdbP->t = ipdb_textAlloc(); if (pdbP->t == NULL) retval = ENOMEM; else { - pdbP->p->num_recs = 2; + pdbP->t->data = strdup(content); - pdbP->i->r->offset += 8; - pdbP->t->r->offset = - pdbP->i->r->offset + IMAGESIZE + ipdb_img_size(pdbP->i); + if (pdbP->t->data == NULL) + retval = ENOMEM; + else { + pdbP->p->num_recs = 2; - retval = 0; + pdbP->i->r->offset += 8; + pdbP->t->r->offset = + pdbP->i->r->offset + IMAGESIZE + ipdb_imgSize(pdbP->i); + + retval = 0; + } } } return retval; @@ -689,7 +735,7 @@ readtxt(IPDB * const pdbP, int main(int argc, const char **argv) { - struct cmdlineInfo cmdline; + struct CmdlineInfo cmdline; IPDB * pdbP; FILE * ifP; int comp; @@ -709,11 +755,13 @@ main(int argc, const char **argv) { if (strlen(cmdline.title) > 31) pm_error("Title too long. Max length is 31 characters."); - pdbP = ipdb_alloc(cmdline.title); + pdbP = ipdb_alloc(); if (pdbP == NULL) pm_error("Failed to allocate IPDB structure"); + setPdbHeader(pdbP->p, cmdline.title, cmdline.fixedtime); + readimg(pdbP, ifP, cmdline.depth4); if (cmdline.notefile) diff --git a/converter/other/pamtoxvmini.c b/converter/other/pamtoxvmini.c index b57bcc74..047de75a 100644 --- a/converter/other/pamtoxvmini.c +++ b/converter/other/pamtoxvmini.c @@ -23,16 +23,16 @@ typedef struct xvPalette { } xvPalette; -struct cmdlineInfo { +struct CmdlineInfo { const char * inputFileName; }; static void -parseCommandLine(int const argc, - char * argv[], - struct cmdlineInfo * const cmdlineP) { +parseCommandLine(int const argc, + const char * argv[], + struct CmdlineInfo * const cmdlineP) { if (argc-1 < 1) cmdlineP->inputFileName = "-"; @@ -75,7 +75,7 @@ static void writeXvHeader(FILE * const ofP, unsigned int const cols, unsigned int const rows) { - + fprintf(ofP, "P7 332\n"); fprintf(ofP, "# Created by Pamtoxvmini\n"); @@ -94,12 +94,14 @@ writeXvHeader(FILE * const ofP, static void findClosestColor(struct pam * const pamP, - tuple const tuple, + tuple const tuple, const xvPalette * const xvPaletteP, unsigned int * const paletteIndexP) { /*---------------------------------------------------------------------------- Find the color in the palette *xvPaletteP that is closest to the color 'tuple' and return its index in the palette. + + *pamP gives the format of 'tuple', which must be RGB with maxval 255. -----------------------------------------------------------------------------*/ unsigned int paletteIndex; unsigned int bestPaletteIndex; @@ -118,12 +120,12 @@ findClosestColor(struct pam * const pamP, unsigned int const tupleRed = tuple[PAM_RED_PLANE]; unsigned int const tupleGrn = tuple[PAM_GRN_PLANE]; unsigned int const tupleBlu = tuple[PAM_BLU_PLANE]; - + unsigned int const paletteRed = xvPaletteP->red[paletteIndex]; unsigned int const paletteGrn = xvPaletteP->grn[paletteIndex]; unsigned int const paletteBlu = xvPaletteP->blu[paletteIndex]; - unsigned int const distance = + unsigned int const distance = SQR((int)tupleRed - (int)paletteRed) + SQR((int)tupleGrn - (int)paletteGrn) + SQR((int)tupleBlu - (int)paletteBlu); @@ -155,24 +157,29 @@ getPaletteIndexThroughCache(struct pam * const pamP, int found; int paletteIndex; + /* As required by findClosestColor(): */ + assert(pamP->depth >= 3); + assert(pamP->maxval == 255); + pnm_lookuptuple(pamP, paletteHash, tuple, &found, &paletteIndex); if (found) *paletteIndexP = paletteIndex; else { int fits; + findClosestColor(pamP, tuple, xvPaletteP, paletteIndexP); - + pnm_addtotuplehash(pamP, paletteHash, tuple, *paletteIndexP, &fits); - + if (!fits) pm_error("Can't get memory for palette hash."); } } - + static void -writeXvRaster(struct pam * const pamP, +writeXvRaster(struct pam * const inpamP, xvPalette * const xvPaletteP, FILE * const ofP) { /*---------------------------------------------------------------------------- @@ -190,33 +197,40 @@ writeXvRaster(struct pam * const pamP, unsigned int row; unsigned char * xvrow; struct pam scaledPam; + struct pam scaledRgbPam; + + pnm_setminallocationdepth(inpamP, 3); paletteHash = pnm_createtuplehash(); - tuplerow = pnm_allocpamrow(pamP); - xvrow = (unsigned char*)pm_allocrow(pamP->width, 1); + tuplerow = pnm_allocpamrow(inpamP); + xvrow = (unsigned char*)pm_allocrow(inpamP->width, 1); - scaledPam = *pamP; + scaledPam = *inpamP; /* initial value */ scaledPam.maxval = 255; - for (row = 0; row < pamP->height; ++row) { + scaledRgbPam = scaledPam; /* initial value */ + scaledRgbPam.depth = MAX(3, scaledPam.depth); + + for (row = 0; row < inpamP->height; ++row) { unsigned int col; - pnm_readpamrow(pamP, tuplerow); - pnm_scaletuplerow(pamP, tuplerow, tuplerow, scaledPam.maxval); + pnm_readpamrow(inpamP, tuplerow); + pnm_scaletuplerow(inpamP, tuplerow, tuplerow, scaledPam.maxval); pnm_makerowrgb(&scaledPam, tuplerow); - for (col = 0; col < scaledPam.width; ++col) { + for (col = 0; col < scaledRgbPam.width; ++col) { unsigned int paletteIndex; - getPaletteIndexThroughCache(&scaledPam, tuplerow[col], xvPaletteP, - paletteHash, &paletteIndex); + getPaletteIndexThroughCache(&scaledRgbPam, tuplerow[col], + xvPaletteP, paletteHash, + &paletteIndex); assert(paletteIndex < 256); xvrow[col] = paletteIndex; } - fwrite(xvrow, 1, scaledPam.width, ofP); + fwrite(xvrow, 1, scaledRgbPam.width, ofP); } pm_freerow((char*)xvrow); @@ -227,16 +241,16 @@ writeXvRaster(struct pam * const pamP, -int -main(int argc, - char * argv[]) { +int +main(int argc, + const char * argv[]) { - struct cmdlineInfo cmdline; + struct CmdlineInfo cmdline; FILE * ifP; struct pam pam; xvPalette xvPalette; - - pnm_init(&argc, argv); + + pm_proginit(&argc, argv); parseCommandLine(argc, argv, &cmdline); @@ -246,13 +260,14 @@ main(int argc, pnm_readpaminit(ifP, &pam, PAM_STRUCT_SIZE(allocation_depth)); - pnm_setminallocationdepth(&pam, 3); - writeXvHeader(stdout, pam.width, pam.height); - + writeXvRaster(&pam, &xvPalette, stdout); pm_close(ifP); return 0; } + + + diff --git a/converter/other/pdbimgtopam.c b/converter/other/pdbimgtopam.c index 67044109..b191644f 100644 --- a/converter/other/pdbimgtopam.c +++ b/converter/other/pdbimgtopam.c @@ -56,7 +56,7 @@ parseCommandLine(int argc, const char ** argv, struct cmdlineInfo * const cmdlineP) { /*---------------------------------------------------------------------------- parse program command line described in Unix standard form by argc - and argv. Return the information in the options as *cmdlineP. + and argv. Return the information in the options as *cmdlineP. If command line is internally inconsistent (invalid options, etc.), issue error message to stderr and abort program. @@ -90,7 +90,7 @@ parseCommandLine(int argc, const char ** argv, if (!notefileSpec) cmdlineP->notefile = NULL; - + if (argc-1 < 1) cmdlineP->inputFileName = "-"; else if (argc-1 == 1) @@ -152,7 +152,7 @@ decompress(const uint8_t * const compressed, const uint8_t * inP; uint8_t * outP; size_t bytesLeft; - + for (bytesLeft = imageSize, inP = &compressed[0], outP = &uncompressed[0]; bytesLeft > 0; @@ -224,7 +224,7 @@ readCompressed(IMAGE * const imgP, * this extra byte and ignore it by paying attention to * the image dimensions. */ - size_t const maxCompressedSizeWithBloat = ipdb_img_size(imgP) * 2; + size_t const maxCompressedSizeWithBloat = ipdb_imgSize(imgP) * 2; /* * Provide a buffer large enough for the worst case. * See note in lib/util/runlength.c . @@ -251,7 +251,7 @@ readCompressed(IMAGE * const imgP, * Read to the indicated offset. */ dataSize = end_offset - ftell(fP) + 1; - + MALLOCARRAY(buffer, dataSize); if (buffer == NULL) @@ -306,9 +306,9 @@ imageReadHeader(FILE * const fileP, pm_message(" Y_anchor: %u", imgP->y_anchor); pm_message(" Width: %u", imgP->width); pm_message(" Height: %u", imgP->height); - pm_message("Pixels per byte: %u", ipdb_img_ppb(imgP)); + pm_message("Pixels per byte: %u", ipdb_imgPpb(imgP)); pm_message("Image size: %lu bytes", - (unsigned long)ipdb_img_size(imgP)); + (unsigned long)ipdb_imgSize(imgP)); } } @@ -318,7 +318,7 @@ imageReadData(FILE * const fileP, IMAGE * const imgP, uint32_t const end_offset) { - size_t const imageSize = ipdb_img_size(imgP); + size_t const imageSize = ipdb_imgSize(imgP); int retval; size_t dataSize; @@ -390,7 +390,7 @@ textRead(TEXT * const textP, return 0; textP->r->offset = (uint32_t)ftell(fileP); - + /* * What a pain in the ass! Why the hell isn't there a length * attached to the text record? I suppose the designer wasn't @@ -453,7 +453,7 @@ pdbheadRead(PDBHEAD * const pdbHeadP, pm_readbiglongu2(fileP, &pdbHeadP->next_rec); pm_readbigshortu(fileP, &pdbHeadP->num_recs); - if (!memeq(pdbHeadP->type, IPDB_vIMG, 4) + if (!memeq(pdbHeadP->type, IPDB_vIMG, 4) || !memeq(pdbHeadP->id, IPDB_View, 4)) retval = E_NOTIMAGE; else @@ -531,62 +531,54 @@ ipdbRead(IPDB * const pdbP, int retval; - ipdb_clear(pdbP); + int status; - pdbP->p = ipdb_pdbhead_alloc(NULL); + status = pdbheadRead(pdbP->p, fileP); - if (pdbP->p == NULL) - retval = ENOMEM; + if (status != 0) + retval = status; else { - int status; - - status = pdbheadRead(pdbP->p, fileP); - - if (status != 0) - retval = status; + pdbP->i = ipdb_imageCreate(pdbP->p->name, IMG_GRAY, 0, 0); + if (pdbP->i == NULL) + retval = ENOMEM; else { - pdbP->i = ipdb_image_alloc(pdbP->p->name, IMG_GRAY, 0, 0); - if (pdbP->i == NULL) - retval = ENOMEM; + int status; + status = rechdrRead(pdbP->i->r, fileP); + if (status != 0) + retval = status; else { - int status; - status = rechdrRead(pdbP->i->r, fileP); - if (status != 0) - retval = status; - else { - if (pdbP->p->num_recs > 1) { - pdbP->t = ipdb_text_alloc(NULL); - if (pdbP->t == NULL) - retval = ENOMEM; - else { - int status; - status = rechdrRead(pdbP->t->r, fileP); - if (status != 0) - retval = status; - else - retval = 0; - } - } else - retval = 0; - - if (retval == 0) { - uint32_t const offset = - pdbP->t == NULL ? - UNKNOWN_OFFSET : pdbP->t->r->offset - 1; - + if (pdbP->p->num_recs > 1) { + pdbP->t = ipdb_textAlloc(); + if (pdbP->t == NULL) + retval = ENOMEM; + else { int status; - - status = imageRead(pdbP->i, offset, fileP, verbose); + status = rechdrRead(pdbP->t->r, fileP); if (status != 0) retval = status; - else { - if (pdbP->t != NULL) { - int status; - - status = textRead(pdbP->t, fileP); - if (status != 0) - retval = status; - } + else + retval = 0; + } + } else + retval = 0; + + if (retval == 0) { + uint32_t const offset = + pdbP->t == NULL ? + UNKNOWN_OFFSET : pdbP->t->r->offset - 1; + + int status; + + status = imageRead(pdbP->i, offset, fileP, verbose); + if (status != 0) + retval = status; + else { + if (pdbP->t != NULL) { + int status; + + status = textRead(pdbP->t, fileP); + if (status != 0) + retval = status; } } } @@ -663,8 +655,8 @@ static void g16row(IPDB * const pdbP, unsigned int const row, uint8_t * const buffer) { - - g16unpack(ipdb_img_row(pdbP->i, row), buffer, ipdb_width(pdbP)); + + g16unpack(ipdb_imgRow(pdbP->i, row), buffer, ipdb_width(pdbP)); } @@ -674,7 +666,7 @@ grow(IPDB * const pdbP, unsigned int const row, uint8_t * const buffer) { - gunpack(ipdb_img_row(pdbP->i, row), buffer, ipdb_width(pdbP)); + gunpack(ipdb_imgRow(pdbP->i, row), buffer, ipdb_width(pdbP)); } @@ -684,7 +676,7 @@ mrow(IPDB * const pdbP, unsigned int const row, uint8_t * const buffer) { - munpack(ipdb_img_row(pdbP->i, row), buffer, ipdb_width(pdbP)); + munpack(ipdb_imgRow(pdbP->i, row), buffer, ipdb_width(pdbP)); } @@ -715,7 +707,7 @@ writeImgPam(IPDB * const pdbP, PAM_PBM_TUPLETYPE : PAM_PGM_TUPLETYPE); pnm_writepaminit(&pam); - + tupleRow = pnm_allocpamrow(&pam); for (row = 0; row < pam.height; ++row) { @@ -731,7 +723,7 @@ writeImgPam(IPDB * const pdbP, for (col = 0; col < pam.width; ++col) tupleRow[col][0] = imgRow[col]; - + pnm_writepamrow(&pam, tupleRow); } pnm_freepamrow(tupleRow); @@ -754,7 +746,7 @@ writeText(IPDB * const pdbP, fP = pm_openw(name); if (fP == NULL) pm_error("Could not open note file '%s' for output", name); - + fprintf(fP, "%s\n", note); pm_close(fP); @@ -777,7 +769,7 @@ main(int argc, const char ** argv) { ifP = pm_openr(cmdline.inputFileName); - pdbP = ipdb_alloc(NULL); + pdbP = ipdb_alloc(); if (pdbP == NULL) pm_error("Could not allocate IPDB structure."); @@ -795,3 +787,6 @@ main(int argc, const char ** argv) { return EXIT_SUCCESS; } + + + diff --git a/converter/other/pnmtorle.c b/converter/other/pnmtorle.c index d61884dd..17c9cfbc 100644 --- a/converter/other/pnmtorle.c +++ b/converter/other/pnmtorle.c @@ -33,71 +33,157 @@ * 2000.04.13 adapted for Netpbm by Bryan Henderson. Quieted compiler * warnings. * + * 2022.03.06 revision by Akira F Urushibata + * use shhopt instead of scanargs + * proper handling of multiple image files with -h + * */ /*----------------------------------------------------- * System includes. */ #include <string.h> #include <stdio.h> +#include <assert.h> #include "pnm.h" #include "mallocvar.h" #include "rle.h" +#include "shhopt.h" +#include "pm_c_util.h" + + +struct CmdlineInfo { + /* All the information the user supplied in the command line, + in a form easy for the program to use. + */ + const char * inFileName; + const char * outfile; + unsigned int verbose; + unsigned int header; + unsigned int alpha; +}; + + + +static void +parseCommandLine(int argc, const char ** argv, + struct CmdlineInfo * const cmdlineP) { +/*---------------------------------------------------------------------------- + Note that the file spec array we return is stored in the storage that + was passed to us as the argv array. +-----------------------------------------------------------------------------*/ + optEntry * option_def; + /* Instructions to pm_optParseOptions3 on how to parse our options. */ + + optStruct3 opt; + + unsigned int option_def_index; + unsigned int outfileSpec; + + MALLOCARRAY_NOFAIL(option_def, 100); + + option_def_index = 0; /* incremented by OPTENTRY */ + OPTENT3(0, "alpha", OPT_FLAG, NULL, &cmdlineP->alpha, 0); + OPTENT3(0, "header", OPT_FLAG, NULL, &cmdlineP->header, 0); + OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0); + OPTENT3(0, "outfile", OPT_STRING, &cmdlineP->outfile, + &outfileSpec, 0); + + opt.opt_table = option_def; + opt.short_allowed = false; /* We have no short (old-fashioned) options */ + opt.allowNegNum = false; /* We have no parms that are negative numbers */ + + pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0); + /* Uses and sets argc, argv, and some of *cmdlineP and others. */ + + free(option_def); + + if (argc-1 == 0) + cmdlineP->inFileName = "-"; + else if (argc-1 != 1) { + pm_error("Program takes zero or one argument (filename). You " + "specified %d", argc-1); + } + else + cmdlineP->inFileName = argv[1]; + + if (!outfileSpec) + cmdlineP->outfile = "-"; +} + -#define VPRINTF if (verbose || header) fprintf -typedef unsigned char U_CHAR; -/* - * Global variables. - */ -static FILE *fp; -static rle_hdr hdr; -static int format; -static int width, height; -static int verbose = 0, header = 0, do_alpha = 0; -static gray maxval; -/*----------------------------------------------------------------------------- - * Read the pnm image file header. - */ static void -read_pnm_header(void) { +readPnmHeader(bool const verbose, + bool const wantAlpha, + FILE * const ifP, + int * const widthP, + int * const heightP, + gray * const maxvalP, + int * const formatP) { +/*----------------------------------------------------------------------------- + Read the pnm image file header. +---------------------------------------------------------------------------- */ + int width; + int height; + gray maxval; + int format; + const char * type; + + pnm_readpnminit(ifP, &width, &height, &maxval, &format); - pnm_readpnminit(fp, &width, &height, &maxval, &format); switch (format) { case PBM_FORMAT: - VPRINTF(stderr, "Image type: plain pbm format\n"); + type="plain pbm"; break; case RPBM_FORMAT: - VPRINTF(stderr, "Image type: raw pbm format\n"); + type="raw pbm"; break; case PGM_FORMAT: - VPRINTF(stderr, "Image type: plain pgm format\n"); + type="plain pgm"; break; case RPGM_FORMAT: - VPRINTF(stderr, "Image type: raw pgm format\n"); + type="raw pgm"; break; case PPM_FORMAT: - VPRINTF(stderr, "Image type: plain ppm format\n"); + type="plain ppm"; break; case RPPM_FORMAT: - VPRINTF(stderr, "Image type: raw ppm format\n"); + type="raw ppm"; break; } - VPRINTF(stderr, "Full image: %dx%d\n", width, height); - VPRINTF(stderr, "Maxval: %d\n", maxval); - if (do_alpha) - VPRINTF(stderr, "Computing alpha channel...\n"); + if (verbose) { + pm_message("Image type: %s format", type); + pm_message("Full image: %dx%d", width, height); + pm_message("Maxval: %d", maxval); + + if (wantAlpha) + pm_message("Computing alpha channel..."); + } + *widthP = width; + *heightP = height; + *maxvalP = maxval; + *formatP = format; } static void -write_rle_header(void) { +writeRleHeader(bool const wantAlpha, + int const format, + unsigned int const width, + unsigned int const height, + rle_hdr * const hdrP) { + + rle_hdr hdr; + + hdr = *hdrP; /* initial value */ hdr.xmin = 0; hdr.xmax = width-1; hdr.ymin = 0; hdr.ymax = height-1; hdr.background = 0; + switch (format) { case PBM_FORMAT: case RPBM_FORMAT: @@ -114,31 +200,42 @@ write_rle_header(void) { RLE_SET_BIT(hdr, RLE_BLUE); break; } - if (do_alpha) { + if (wantAlpha) { hdr.alpha = 1; RLE_SET_BIT(hdr, RLE_ALPHA); } rle_put_setup(&hdr); + + *hdrP = hdr; } static void -write_rle_data(void) { +writeRleData(bool const verbose, + bool const wantAlpha, + FILE * const ifP, + rle_hdr * const hdrP, + unsigned int const width, + unsigned int const height, + gray const maxval, + int const format) { unsigned int scan; xel * xelrow; rle_pixel *** scanlines; MALLOCARRAY(xelrow, width); - MALLOCARRAY(scanlines, height); + if (xelrow == NULL) + pm_error("Failed to allocate memory for row of %u pixels", width); - if (!scanlines) + MALLOCARRAY(scanlines, height); + if (scanlines == NULL) pm_error("Failed to allocate memory for %u scanline pointers", height); for (scan = 0; scan < height; ++scan) { int rc; - rc = rle_row_alloc(&hdr, &scanlines[scan]); + rc = rle_row_alloc(hdrP, &scanlines[scan]); if (rc < 0) pm_error("Failed to allocate memory for a scanline"); } @@ -151,10 +248,10 @@ write_rle_data(void) { for (scan = 0; scan < height; ++scan) { rle_pixel ** const scanline = scanlines[height - scan - 1]; unsigned int col; - pnm_readpnmrow(fp, xelrow, width, maxval, format); + pnm_readpnmrow(ifP, xelrow, width, maxval, format); for (col = 0; col < width; ++col) { scanline[RLE_RED][col] = PNM_GET1(xelrow[col]) ? 255 : 0; - if (do_alpha) + if (wantAlpha) scanline[RLE_ALPHA][col] = scanline[RLE_RED][col]; } } @@ -165,10 +262,10 @@ write_rle_data(void) { for (scan = 0; scan < height; ++scan) { rle_pixel ** const scanline = scanlines[height - scan - 1]; unsigned int col; - pnm_readpnmrow(fp, xelrow, width, maxval, format); + pnm_readpnmrow(ifP, xelrow, width, maxval, format); for (col = 0; col < width; ++col) { scanline[RLE_RED][col] = PNM_GET1(xelrow[col]); - if (do_alpha) + if (wantAlpha) scanline[RLE_ALPHA][col] = scanline[RLE_RED][col] ? 255 : 0; } @@ -179,13 +276,16 @@ write_rle_data(void) { unsigned int scan; for (scan = 0; scan < height; scan++) { rle_pixel ** const scanline = scanlines[height - scan - 1]; + unsigned int col; - pnm_readpnmrow(fp, xelrow, width, maxval, format); + + pnm_readpnmrow(ifP, xelrow, width, maxval, format); + for (col = 0; col < width; ++col) { scanline[RLE_RED][col] = PPM_GETR(xelrow[col]); scanline[RLE_GREEN][col] = PPM_GETG(xelrow[col]); scanline[RLE_BLUE][col] = PPM_GETB(xelrow[col]); - if (do_alpha) + if (wantAlpha) scanline[RLE_ALPHA][col] = (scanline[RLE_RED][col] || scanline[RLE_GREEN][col] || @@ -196,25 +296,27 @@ write_rle_data(void) { } /* Write out data in URT order (bottom to top). */ for (scan = 0; scan < height; ++scan) - rle_putrow(scanlines[scan], width, &hdr); + rle_putrow(scanlines[scan], width, hdrP); for (scan = 0; scan < height; ++scan) - rle_row_free(&hdr, scanlines[scan]); + rle_row_free(hdrP, scanlines[scan]); free(scanlines); free(xelrow); - VPRINTF(stderr, "Done -- write eof to RLE data.\n"); - rle_puteof(&hdr); + if (verbose) + pm_message("Done -- write eof to RLE data."); + + rle_puteof(hdrP); } static void -skip_data(FILE * const fp, - int const width, - int const height, - gray const maxval, - int const format) { +skipData(FILE * const ifP, + unsigned int const width, + unsigned int const height, + gray const maxval, + int const format) { xel * xelrow; unsigned int scan; @@ -223,8 +325,8 @@ skip_data(FILE * const fp, if (xelrow == NULL) pm_error("Failed to allocate memory for row of %u pixels", width); - for(scan=0; scan < height; ++scan) - pnm_readpnmrow(fp, xelrow, width, maxval, format); + for (scan=0; scan < height; ++scan) + pnm_readpnmrow(ifP, xelrow, width, maxval, format); free(xelrow); } @@ -232,57 +334,57 @@ skip_data(FILE * const fp, int -main(int argc, char ** argv) { - - const char * pnmname; - const char * outname; - int oflag; +main(int argc, char ** argv) { + + struct CmdlineInfo cmdline; + + FILE * ifP; + rle_hdr hdr; + int format; + int width, height; + gray maxval; + bool verbose; + const char ** argvWork; + unsigned int i; int eof; - pnm_init(&argc, argv); - - pnmname = NULL; /* initial value */ - outname = NULL; /* initial value */ - - /* Get those options. */ - if (!scanargs(argc,argv, - "% v%- h%- a%- o%-outfile!s pnmfile%s\n(\ -\tConvert a PNM file to URT RLE format.\n\ -\t-a\tFake an alpha channel. Alpha=0 when input=0, 255 otherwise.\n\ -\t-h\tPrint header of PNM file and exit.\n\ -\t-v\tVerbose mode.)", - &verbose, - &header, - &do_alpha, - &oflag, &outname, - &pnmname)) - exit(-1); + MALLOCARRAY_NOFAIL(argvWork, argc + 1); + + for (i = 0; i < argc; ++i) /* Make a copy of argv */ + argvWork[i] = argv[i]; + + pm_proginit(&argc, argvWork); + + parseCommandLine(argc, argvWork, &cmdline); + + verbose = cmdline.verbose || cmdline.header; hdr = *rle_hdr_init(NULL); - rle_names(&hdr, cmd_name(argv), outname, 0); + + rle_names(&hdr, "pnmtorle", cmdline.outfile, 0); /* Open the file. */ - if (pnmname == NULL) { - fp = pm_openr("-"); - } else { - fp = pm_openr(pnmname); - } + assert(cmdline.inFileName != NULL); + ifP = pm_openr(cmdline.inFileName); - hdr.rle_file = rle_open_f( hdr.cmd, outname, "wb" ); + hdr.rle_file = rle_open_f(hdr.cmd, cmdline.outfile, "wb"); for (eof = 0; !eof; ) { - read_pnm_header(); + readPnmHeader(verbose, cmdline.alpha, ifP, + &width, &height, &maxval, &format); - if (header) - skip_data(fp, width, height, maxval, format); - else { + if (cmdline.header) { + skipData(ifP, width, height, maxval, format); + } else { rle_addhist(argv, NULL, &hdr); - write_rle_header(); - write_rle_data(); + writeRleHeader(cmdline.alpha, format, width, height, &hdr); + writeRleData(verbose, cmdline.alpha, ifP, &hdr, + width, height, maxval, format); } - pnm_nextimage(fp, &eof); + pnm_nextimage(ifP, &eof); } - pm_close(fp); + + pm_close(ifP); return 0; } diff --git a/converter/other/pnmtosir.c b/converter/other/pnmtosir.c index 20bb6178..7b7650fe 100644 --- a/converter/other/pnmtosir.c +++ b/converter/other/pnmtosir.c @@ -20,16 +20,16 @@ int main(int argc, const char * argv[]) { - + FILE * ifP; xel ** xels; int rows, cols, format; unsigned int n; bool isGrayscale; xelval maxval; - unsigned short Header[16]; - unsigned short LutHeader[16]; - unsigned short Lut[2048]; + unsigned short Header[10]; + unsigned short LutHeader[5]; + unsigned short Lut[1024]; pm_proginit(&argc, argv); @@ -43,9 +43,9 @@ main(int argc, const char * argv[]) { } else { ifP = stdin; } - + xels = pnm_readpnm(ifP, &cols, &rows, &maxval, &format); - + /* Figure out the colormap. */ switch (PNM_FORMAT_TYPE(format) ) { case PPM_TYPE: @@ -93,15 +93,19 @@ main(int argc, const char * argv[]) { pm_writelittleshort(stdout,LutHeader[n]); for (n = 5; n < 256; ++n) pm_writelittleshort(stdout,0); - - for (n = 0; n < 3; ++n) { + + for (n = 0; n < 256; ++n) { unsigned int m; - for (m = 0; m < 256; ++m) - Lut[m * 4 + n] = m << 8; + for (m = 0; m < 3; ++m) + Lut[n * 4 + m] = n << 8; + + Lut[n * 4 + 3] = 0; + /* Clear to ensure repeatable output, suppress Valgrind error */ } + for (n = 0; n < 1024; ++n) pm_writelittleshort(stdout,Lut[n]); - + /* Finally, write out the data. */ switch (PNM_FORMAT_TYPE(format)) { case PPM_TYPE: { @@ -110,13 +114,13 @@ main(int argc, const char * argv[]) { unsigned int col; for (col = 0; col < cols; ++col) { unsigned char const ub = - (char) (PPM_GETR(xels[row][col]) * (255 / maxval)); + (char) (PPM_GETR(xels[row][col]) * (255 / maxval)); fputc(ub, stdout); } } for (row = 0; row < rows; ++row) { unsigned int col; - for (col = 0; col < cols; ++col) { + for (col = 0; col < cols; ++col) { unsigned const char ub = (char) (PPM_GETG(xels[row][col]) * (255 / maxval)); fputc(ub, stdout); @@ -124,7 +128,7 @@ main(int argc, const char * argv[]) { } for (row = 0; row < rows; ++row) { unsigned int col; - for (col = 0; col < cols; ++col) { + for (col = 0; col < cols; ++col) { unsigned const char ub = (char) (PPM_GETB(xels[row][col]) * (255 / maxval)); fputc(ub, stdout); @@ -144,10 +148,11 @@ main(int argc, const char * argv[]) { } } break; } - + pm_close(ifP); return 0; } + diff --git a/converter/ppm/picttoppm.c b/converter/ppm/picttoppm.c index bf60748f..ec701cfc 100644 --- a/converter/ppm/picttoppm.c +++ b/converter/ppm/picttoppm.c @@ -35,6 +35,7 @@ #include "pbmfont.h" #include "mallocvar.h" #include "nstring.h" +#include "shhopt.h" /* @@ -46,6 +47,82 @@ typedef unsigned short Word; typedef unsigned long Longword; +struct CmdlineInfo { + /* All the information the user supplied in the command line, + in a form easy for the program to use. + */ + const char * inputFileName; /* File name of input file */ + + unsigned int fullres; + unsigned int noheader; + unsigned int quickdraw; + const char * fontdir; /* Null if not specified */ + unsigned int verbose; +}; + + + +static void +parseCommandLine(int argc, + const char ** argv, + struct CmdlineInfo * const cmdlineP) { +/* -------------------------------------------------------------------------- + Parse program command line described in Unix standard form by argc + and argv. Return the information in the options as *cmdlineP. + + If command line is internally inconsistent (invalid options, etc.), + issue error message to stderr and abort program. + + Note that the strings we return are stored in the storage that + was passed to us as the argv array. We also trash *argv. +--------------------------------------------------------------------------*/ + optEntry * option_def; + optStruct3 opt; + /* Instructions to pm_optParseOptions3 on how to parse our options. */ + + unsigned int option_def_index; + + unsigned int fontdirSpec, verboseSpec; + + MALLOCARRAY_NOFAIL(option_def, 100); + + option_def_index = 0; /* incremented by OPTENT3 */ + OPTENT3(0, "fontdir", OPT_STRING, &cmdlineP->fontdir, + &fontdirSpec, 0); + OPTENT3(0, "fullres", OPT_FLAG, NULL, + &cmdlineP->fullres, 0); + OPTENT3(0, "noheader", OPT_FLAG, NULL, + &cmdlineP->noheader, 0); + OPTENT3(0, "quickdraw", OPT_FLAG, NULL, + &cmdlineP->quickdraw, 0); + OPTENT3(0, "verbose", OPT_UINT, &cmdlineP->verbose, + &verboseSpec, 0); + + opt.opt_table = option_def; + opt.short_allowed = false; /* We have no short (old-fashioned) options */ + opt.allowNegNum = false; /* We have no parms that are negative numbers */ + + pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0); + /* Uses and sets argc, argv, and some of *cmdlineP and others. */ + + if (!fontdirSpec) + cmdlineP->fontdir = NULL; + + if (!verboseSpec) + cmdlineP->verbose = 0; + + if (argc-1 < 1) + cmdlineP->inputFileName = "-"; + else { + cmdlineP->inputFileName = argv[1]; + if (argc-1 > 1) + pm_error("Too many arguments: %u. The only possible non-option " + "argument is the input file name", argc-1); + } +} + + + /* * Data structures for QuickDraw (and hence PICT) stuff. */ @@ -65,20 +142,20 @@ struct Rect { /* "End" means last plus one */ }; -struct pixMap { +struct PixMap { struct Rect Bounds; - Word version; - Word packType; - Longword packSize; - Longword hRes; - Longword vRes; - Word pixelType; - Word pixelSize; - Word cmpCount; - Word cmpSize; - Longword planeBytes; - Longword pmTable; - Longword pmReserved; + Word version; + Word packType; + Longword packSize; + Longword hRes; + Longword vRes; + Word pixelType; + Word pixelSize; + Word cmpCount; + Word cmpSize; + Longword planeBytes; + Longword pmTable; + Longword pmReserved; }; struct RGBColor { @@ -109,7 +186,7 @@ struct RgbPlanes { Word * blu; }; -struct canvas { +struct Canvas { struct RgbPlanes planes; }; @@ -120,7 +197,6 @@ static struct Rect picFrame; static Word rowlen; static Word collen; static int verbose; -static int fullres; static int recognize_comment; static struct RGBColor black = { 0, 0, 0 }; @@ -157,7 +233,7 @@ static int ps_cent_x; static int ps_cent_y; static int ps_cent_set; -struct raster { +struct Raster { /*---------------------------------------------------------------------------- An image raster. May be either truecolor or paletted. @@ -188,7 +264,7 @@ struct raster { static void -allocateRaster(struct raster * const rasterP, +allocateRaster(struct Raster * const rasterP, unsigned int const width, unsigned int const height, unsigned int const bitsPerPixel) { @@ -217,19 +293,24 @@ allocateRaster(struct raster * const rasterP, /* TODO: I'm still trying to figure out this format. My theory today: - The row data is in plane order (a row consists of red - plane, then, green, then blue, then some 4th plane). - The old hack code said 3 bytes per pixel here, and could get - away with it because it never got to decoding the 4th plane. + The row data is in plane order (a row consists of red plane, then, + green, then blue, then some 4th plane). + + If the image is compressed, each row is compressed separately, with + the planes opaque to the compressor. - But the new clean code needs to tell it like it is and allocate - 4 bytes per pixel. If we say 3 bytes per pixel here, we get an - "invalid PICT" error because the image actually contains 4 - bytes per pixel and as we decompress it, we run out of place - to put the data. + The old hack code said 3 bytes per pixel here, and could get away + with it because it never got to decoding the 4th plane. - We have yet to see if we can properly interpret the data. + But the new clean code needs to tell it like it is and allocate 4 + bytes per pixel. If we say 3 bytes per pixel here, we get an + "invalid PICT" error on one image because the image actually + contains 4 bytes per pixel and as we decompress it, we run out of + place to put the data. + + On another image we've seen, the decompressor generates 3 bytes per + pixel. */ rasterP->rowSize = allocWidth * 4; @@ -259,37 +340,37 @@ allocateRaster(struct raster * const rasterP, static void -freeRaster(struct raster const raster) { +freeRaster(struct Raster const raster) { free(raster.bytes); } -struct blit_info { - struct Rect srcRect; - struct Rect srcBounds; - struct raster srcplane; - int pixSize; - struct Rect dstRect; - struct RGBColor * colorMap; - int mode; - struct blit_info * next; +struct BlitInfo { + struct Rect srcRect; + struct Rect srcBounds; + struct Raster srcplane; + int pixSize; + struct Rect dstRect; + struct RGBColor * colorMap; + int mode; + struct BlitInfo * next; }; typedef struct { - struct blit_info * firstP; - struct blit_info ** connectorP; + struct BlitInfo * firstP; + struct BlitInfo ** connectorP; bool unblittableText; /* The image contains text opcodes, and we don't know how to put that in a blit list (I really don't even know what a blit _is_), so the image information here is incomplete. */ -} blitList; +} BlitList; -typedef void (drawFn)(struct canvas *, blitList *, int); +typedef void (drawFn)(FILE *, struct Canvas *, BlitList *, int); -struct opdef { +struct Opdef { const char* name; int len; /* If non-negative, this is the length of the argument of the @@ -322,16 +403,15 @@ struct opdef { #define RGB_LEN (6) -static FILE* ifp; static int align = 0; static Byte -readByte(void) { +readByte(FILE * const ifP) { int c; - if ((c = fgetc(ifp)) == EOF) + if ((c = fgetc(ifP)) == EOF) pm_error("EOF / read error while %s", stage); ++align; @@ -341,27 +421,29 @@ readByte(void) { static Word -readWord(void) { +readWord(FILE * const ifP) { - Byte const hi = readByte(); - Byte const lo = readByte(); + Byte const hi = readByte(ifP); + Byte const lo = readByte(ifP); return (hi << 8) | (lo << 0); } -static void readPoint(struct Point * const p) { - p->y = readWord(); - p->x = readWord(); +static void readPoint(FILE * const ifP, + struct Point * const p) { + p->y = readWord(ifP); + p->x = readWord(ifP); } static Longword -readLong(void) { - Word const hi = readWord(); - Word const lo = readWord(); +readLong(FILE * const ifP) { + + Word const hi = readWord(ifP); + Word const lo = readWord(ifP); return (hi << 16) | (lo << 0); } @@ -369,40 +451,45 @@ readLong(void) { static SignedByte -readSignedByte(void) { - return (SignedByte)readByte(); +readSignedByte(FILE * const ifP) { + return (SignedByte)readByte(ifP); } static void -readShortPoint(struct Point * const p) { - p->x = readSignedByte(); - p->y = readSignedByte(); +readShortPoint(FILE * const ifP, + struct Point * const p) { + + p->x = readSignedByte(ifP); + p->y = readSignedByte(ifP); } static void -skip(int const byteCount) { - static Byte buf[1024]; +skip(FILE * const ifP, + unsigned int const byteCount) { + + Byte buf[1024]; int n; align += byteCount; - for (n = byteCount; n > 0; n -= 1024) - if (fread(buf, n > 1024 ? 1024 : n, 1, ifp) != 1) + for (n = byteCount; n > 0; n -= 1024) { + if (fread(buf, n > 1024 ? 1024 : n, 1, ifP) != 1) pm_error("EOF / read error while %s", stage); + } } -struct const_name { +struct ConstName { int value; const char * name; }; -struct const_name const transfer_name[] = { +struct ConstName const transfer_name[] = { { 0, "srcCopy" }, { 1, "srcOr" }, { 2, "srcXor" }, @@ -422,7 +509,7 @@ struct const_name const transfer_name[] = { { -1, 0 } }; -struct const_name font_name[] = { +struct ConstName font_name[] = { { 0, "systemFont" }, { 1, "applFont" }, { 2, "newYork" }, @@ -443,7 +530,7 @@ struct const_name font_name[] = { { -1, 0 } }; -struct const_name ps_just_name[] = { +struct ConstName ps_just_name[] = { { 0, "no" }, { 1, "left" }, { 2, "center" }, @@ -452,7 +539,7 @@ struct const_name ps_just_name[] = { { -1, 0 } }; -struct const_name ps_flip_name[] = { +struct ConstName ps_flip_name[] = { { 0, "no" }, { 1, "horizontal" }, { 2, "vertical" }, @@ -462,8 +549,8 @@ struct const_name ps_flip_name[] = { static const char* -const_name(const struct const_name * const table, - unsigned int const ct) { +constName(const struct ConstName * const table, + unsigned int const ct) { static char numbuf[32]; @@ -480,8 +567,9 @@ const_name(const struct const_name * const table, static void -picComment(Word const type, - int const length) { +picComment(FILE * const ifP, + Word const type, + int const length) { unsigned int remainingLength; @@ -489,10 +577,10 @@ picComment(Word const type, case 150: if (verbose) pm_message("TextBegin"); if (length >= 6) { - ps_just = readByte(); - ps_flip = readByte(); - ps_rotation = readWord(); - ps_linespace = readByte(); + ps_just = readByte(ifP); + ps_flip = readByte(ifP); + ps_rotation = readWord(ifP); + ps_linespace = readByte(ifP); remainingLength = length - 5; if (recognize_comment) ps_text = 1; @@ -500,8 +588,8 @@ picComment(Word const type, if (verbose) { pm_message("%s justification, %s flip, %d degree rotation, " "%d/2 linespacing", - const_name(ps_just_name, ps_just), - const_name(ps_flip_name, ps_flip), + constName(ps_just_name, ps_just), + constName(ps_flip_name, ps_flip), ps_rotation, ps_linespace); } } else @@ -525,14 +613,14 @@ picComment(Word const type, if (length < 8) remainingLength = length; else { - ps_cent_y = readWord(); + ps_cent_y = readWord(ifP); if (ps_cent_y > 32767) ps_cent_y -= 65536; - skip(2); /* ignore fractional part */ - ps_cent_x = readWord(); + skip(ifP, 2); /* ignore fractional part */ + ps_cent_x = readWord(ifP); if (ps_cent_x > 32767) ps_cent_x -= 65536; - skip(2); /* ignore fractional part */ + skip(ifP, 2); /* ignore fractional part */ remainingLength = length - 8; if (verbose) pm_message("offset %d %d", ps_cent_x, ps_cent_y); @@ -628,7 +716,7 @@ picComment(Word const type, break; } if (remainingLength > 0) - skip(remainingLength); + skip(ifP, remainingLength); } @@ -636,11 +724,12 @@ picComment(Word const type, static drawFn ShortComment; static void -ShortComment(struct canvas * const canvasP, - blitList * const blitListP, +ShortComment(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - picComment(readWord(), 0); + picComment(ifP, readWord(ifP), 0); } @@ -648,27 +737,29 @@ ShortComment(struct canvas * const canvasP, static drawFn LongComment; static void -LongComment(struct canvas * const canvasP, - blitList * const blitListP, +LongComment(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { Word type; - type = readWord(); - picComment(type, readWord()); + type = readWord(ifP); + picComment(ifP, type, readWord(ifP)); } -static drawFn skip_poly_or_region; +static drawFn skipPolyOrRegion; static void -skip_poly_or_region(struct canvas * const canvasP, - blitList * const blitListP, - int const version) { +skipPolyOrRegion(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, + int const version) { stage = "skipping polygon or region"; - skip(readWord() - 2); + skip(ifP, readWord(ifP) - 2); } @@ -684,17 +775,17 @@ skip_poly_or_region(struct canvas * const canvasP, /* Some font searching routines */ -struct fontinfo { +struct FontInfo { int font; int size; int style; char* filename; struct font* loaded; - struct fontinfo* next; + struct FontInfo* next; }; -static struct fontinfo* fontlist = 0; -static struct fontinfo** fontlist_ins = &fontlist; +static struct FontInfo* fontlist = 0; +static struct FontInfo** fontlist_ins = &fontlist; @@ -729,9 +820,9 @@ tokenize(char * const s, static void parseFontLine(const char ** const token, - struct fontinfo ** const fontinfoPP) { + struct FontInfo ** const fontinfoPP) { - struct fontinfo * fontinfoP; + struct FontInfo * fontinfoP; MALLOCVAR(fontinfoP); if (fontinfoP == NULL) @@ -752,7 +843,7 @@ parseFontLine(const char ** const token, static int -load_fontdir(const char * const dirfile) { +loadFontdir(const char * const dirfile) { /*---------------------------------------------------------------------------- Load the font directory from file named 'dirfile'. Add its contents to the global list of fonts 'fontlist'. @@ -777,7 +868,7 @@ load_fontdir(const char * const dirfile) { } else if (nToken != 4) { /* Unrecognized format - ignore */ } else { - struct fontinfo * fontinfoP; + struct FontInfo * fontinfoP; parseFontLine(token, &fontinfoP); @@ -795,6 +886,23 @@ load_fontdir(const char * const dirfile) { static void +loadDefaultFontDir(void) { +/*---------------------------------------------------------------------------- + Load the fonts from the font directory file "fontdir" (in the current + directory), if it exists. +-----------------------------------------------------------------------------*/ + struct stat statbuf; + int rc; + + rc = stat("fontdir", &statbuf); + + if (rc == 0) + loadFontdir("fontdir"); +} + + + +static void dumpRect(const char * const label, struct Rect const rectangle) { @@ -807,7 +915,8 @@ dumpRect(const char * const label, static void -readRect(struct Rect * const r) { +readRect(FILE * const ifP, + struct Rect * const r) { /* We don't have a formal specification for the Pict format, but we have seen samples that have the rectangle corners either in top left, bottom @@ -819,10 +928,10 @@ readRect(struct Rect * const r) { So now we accept all 4 possibilities. */ - Word const y1 = readWord(); - Word const x1 = readWord(); - Word const y2 = readWord(); - Word const x2 = readWord(); + Word const y1 = readWord(ifP); + Word const x1 = readWord(ifP); + Word const y2 = readWord(ifP); + Word const x2 = readWord(ifP); r->top = MIN(y1, y2); r->left = MIN(x1, x2); @@ -909,7 +1018,7 @@ rectscale(struct Rect * const r, static void -initBlitList(blitList * const blitListP) { +initBlitList(BlitList * const blitListP) { blitListP->firstP = NULL; blitListP->connectorP = &blitListP->firstP; @@ -919,16 +1028,16 @@ initBlitList(blitList * const blitListP) { static void -addBlitList(blitList * const blitListP, +addBlitList(BlitList * const blitListP, struct Rect const srcRect, struct Rect const srcBounds, - struct raster const srcplane, + struct Raster const srcplane, int const pixSize, struct Rect const dstRect, struct RGBColor * const colorMap, int const mode) { - struct blit_info * biP; + struct BlitInfo * biP; MALLOCVAR(biP); if (biP == NULL) @@ -945,7 +1054,7 @@ addBlitList(blitList * const blitListP, biP->next = NULL; *blitListP->connectorP = biP; - blitListP->connectorP = &biP->next; + blitListP->connectorP = &biP->next; } } @@ -970,12 +1079,15 @@ rgbAllSame(const struct RGBColor * const colorP, } + static bool rgbIsWhite(const struct RGBColor * const colorP) { return rgbAllSame(colorP, 0xffff); } + + static bool rgbIsBlack(const struct RGBColor * const colorP) { @@ -983,122 +1095,133 @@ rgbIsBlack(const struct RGBColor * const colorP) { } + static void -srcCopy(struct RGBColor * const src, - struct RGBColor * const dst) { +srcCopy(struct RGBColor * const srcP, + struct RGBColor * const dstP) { - if (rgbIsBlack(src)) - *dst = foreground; + if (rgbIsBlack(srcP)) + *dstP = foreground; else - *dst = background; + *dstP = background; } static void -srcOr(struct RGBColor * const src, - struct RGBColor * const dst) { - if (rgbIsBlack(src)) - *dst = foreground; +srcOr(struct RGBColor * const srcP, + struct RGBColor * const dstP) { + + if (rgbIsBlack(srcP)) + *dstP = foreground; } static void -srcXor(struct RGBColor * const src, - struct RGBColor * const dst) { - dst->red ^= ~src->red; - dst->grn ^= ~src->grn; - dst->blu ^= ~src->blu; +srcXor(struct RGBColor * const srcP, + struct RGBColor * const dstP) { + + dstP->red ^= ~srcP->red; + dstP->grn ^= ~srcP->grn; + dstP->blu ^= ~srcP->blu; } static void -srcBic(struct RGBColor * const src, - struct RGBColor * const dst) { - if (rgbIsBlack(src)) - *dst = background; +srcBic(struct RGBColor * const srcP, + struct RGBColor * const dstP) { + + if (rgbIsBlack(srcP)) + *dstP = background; } static void -notSrcCopy(struct RGBColor * const src, - struct RGBColor * const dst) { - if (rgbIsWhite(src)) - *dst = foreground; - else if (rgbIsBlack(src)) - *dst = background; +notSrcCopy(struct RGBColor * const srcP, + struct RGBColor * const dstP) { + + if (rgbIsWhite(srcP)) + *dstP = foreground; + else if (rgbIsBlack(srcP)) + *dstP = background; } static void -notSrcOr(struct RGBColor * const src, - struct RGBColor * const dst) { - if (rgbIsWhite(src)) - *dst = foreground; +notSrcOr(struct RGBColor * const srcP, + struct RGBColor * const dstP) { + + if (rgbIsWhite(srcP)) + *dstP = foreground; } static void -notSrcBic(struct RGBColor * const src, - struct RGBColor * const dst) { - if (rgbIsWhite(src)) - *dst = background; +notSrcBic(struct RGBColor * const srcP, + struct RGBColor * const dstP) { + + if (rgbIsWhite(srcP)) + *dstP = background; } static void -notSrcXor(struct RGBColor * const src, - struct RGBColor * const dst) { - dst->red ^= src->red; - dst->grn ^= src->grn; - dst->blu ^= src->blu; +notSrcXor(struct RGBColor * const srcP, + struct RGBColor * const dstP) { + + dstP->red ^= srcP->red; + dstP->grn ^= srcP->grn; + dstP->blu ^= srcP->blu; } static void -addOver(struct RGBColor * const src, - struct RGBColor * const dst) { - dst->red += src->red; - dst->grn += src->grn; - dst->blu += src->blu; +addOver(struct RGBColor * const srcP, + struct RGBColor * const dstP) { + + dstP->red += srcP->red; + dstP->grn += srcP->grn; + dstP->blu += srcP->blu; } static void -addPin(struct RGBColor * const src, - struct RGBColor * const dst) { - if ((long)dst->red + (long)src->red > (long)op_color.red) - dst->red = op_color.red; +addPin(struct RGBColor * const srcP, + struct RGBColor * const dstP) { + + if ((long)dstP->red + (long)srcP->red > (long)op_color.red) + dstP->red = op_color.red; else - dst->red = dst->red + src->red; + dstP->red = dstP->red + srcP->red; - if ((long)dst->grn + (long)src->grn > (long)op_color.grn) - dst->grn = op_color.grn; + if ((long)dstP->grn + (long)srcP->grn > (long)op_color.grn) + dstP->grn = op_color.grn; else - dst->grn = dst->grn + src->grn; + dstP->grn = dstP->grn + srcP->grn; - if ((long)dst->blu + (long)src->blu > (long)op_color.blu) - dst->blu = op_color.blu; + if ((long)dstP->blu + (long)srcP->blu > (long)op_color.blu) + dstP->blu = op_color.blu; else - dst->blu = dst->blu + src->blu; + dstP->blu = dstP->blu + srcP->blu; } static void -subOver(struct RGBColor * const src, - struct RGBColor * const dst) { - dst->red -= src->red; - dst->grn -= src->grn; - dst->blu -= src->blu; +subOver(struct RGBColor * const srcP, + struct RGBColor * const dstP) { + + dstP->red -= srcP->red; + dstP->grn -= srcP->grn; + dstP->blu -= srcP->blu; } @@ -1107,74 +1230,81 @@ subOver(struct RGBColor * const src, static void -subPin(struct RGBColor * const src, - struct RGBColor * const dst) { - if ((long)dst->red - (long)src->red < (long)op_color.red) - dst->red = op_color.red; +subPin(struct RGBColor * const srcP, + struct RGBColor * const dstP) { + + if ((long)dstP->red - (long)srcP->red < (long)op_color.red) + dstP->red = op_color.red; else - dst->red = dst->red - src->red; + dstP->red = dstP->red - srcP->red; - if ((long)dst->grn - (long)src->grn < (long)op_color.grn) - dst->grn = op_color.grn; + if ((long)dstP->grn - (long)srcP->grn < (long)op_color.grn) + dstP->grn = op_color.grn; else - dst->grn = dst->grn - src->grn; + dstP->grn = dstP->grn - srcP->grn; - if ((long)dst->blu - (long)src->blu < (long)op_color.blu) - dst->blu = op_color.blu; + if ((long)dstP->blu - (long)srcP->blu < (long)op_color.blu) + dstP->blu = op_color.blu; else - dst->blu = dst->blu - src->blu; + dstP->blu = dstP->blu - srcP->blu; } static void -adMax(struct RGBColor * const src, - struct RGBColor * const dst) { - if (src->red > dst->red) dst->red = src->red; - if (src->grn > dst->grn) dst->grn = src->grn; - if (src->blu > dst->blu) dst->blu = src->blu; +adMax(struct RGBColor * const srcP, + struct RGBColor * const dstP) { + + if (srcP->red > dstP->red) dstP->red = srcP->red; + if (srcP->grn > dstP->grn) dstP->grn = srcP->grn; + if (srcP->blu > dstP->blu) dstP->blu = srcP->blu; } static void -adMin(struct RGBColor * const src, - struct RGBColor * const dst) { - if (src->red < dst->red) dst->red = src->red; - if (src->grn < dst->grn) dst->grn = src->grn; - if (src->blu < dst->blu) dst->blu = src->blu; +adMin(struct RGBColor * const srcP, + struct RGBColor * const dstP) { + + if (srcP->red < dstP->red) dstP->red = srcP->red; + if (srcP->grn < dstP->grn) dstP->grn = srcP->grn; + if (srcP->blu < dstP->blu) dstP->blu = srcP->blu; } static void -blend(struct RGBColor * const src, - struct RGBColor * const dst) { +blend(struct RGBColor * const srcP, + struct RGBColor * const dstP) { + #define blend_component(cmp) \ - ((long)src->cmp * (long)op_color.cmp) / 65536 + \ - ((long)dst->cmp * (long)(65536 - op_color.cmp) / 65536) + ((long)srcP->cmp * (long)op_color.cmp) / 65536 + \ + ((long)dstP->cmp * (long)(65536 - op_color.cmp) / 65536) - dst->red = blend_component(red); - dst->grn = blend_component(grn); - dst->blu = blend_component(blu); + dstP->red = blend_component(red); + dstP->grn = blend_component(grn); + dstP->blu = blend_component(blu); } static void -transparent(struct RGBColor * const src, - struct RGBColor * const dst) { - if (src->red != background.red || - src->grn != background.grn || - src->blu != background.blu) { - *dst = *src; +transparent(struct RGBColor * const srcP, + struct RGBColor * const dstP) { + + if (srcP->red != background.red || + srcP->grn != background.grn || + srcP->blu != background.blu) { + + *dstP = *srcP; } } static transfer_func -transfer(int const mode) { +transferFunctionForMode(unsigned int const mode) { + switch (mode) { case 0: return srcCopy; case 1: return srcOr; @@ -1193,9 +1323,8 @@ transfer(int const mode) { case 38: return subOver; case 39: return adMin; default: - if (mode != 0) - pm_message("no transfer function for code %s, using srcCopy", - const_name(transfer_name, mode)); + pm_message("no transfer function for code %s, using srcCopy", + constName(transfer_name, mode)); return srcCopy; } } @@ -1447,186 +1576,171 @@ putRgb(struct RGBColor const rgb, static void -doSameSize(transfer_func trf, - int const pixSize, - struct Rect const srcRect, - unsigned char * const src, - unsigned int const srcwid, - struct RGBColor * const color_map, - struct RgbPlanes const dst, - unsigned int const dstwid) { -/*---------------------------------------------------------------------------- - Transfer pixels from 'src' to 'dst', applying the transfer function - 'trf'. +doSameSize8bpp(transfer_func trf, + unsigned int const xsize, + unsigned int const ysize, + unsigned char * const src, + unsigned int const srcwid, + struct RGBColor * const colorMap, + struct RgbPlanes const dst, + unsigned int const dstwid) { + + unsigned int rowNumber; + + for (rowNumber = 0; rowNumber < ysize; ++rowNumber) { + unsigned char * const srcrow = &src[rowNumber * srcwid]; + unsigned int const dstRowCurs = rowNumber * dstwid; + + unsigned int colNumber; + for (colNumber = 0; colNumber < xsize; ++colNumber) { + unsigned int const dstCursor = dstRowCurs + colNumber; + unsigned int const colorIndex = srcrow[colNumber]; + + if (trf) { + struct RGBColor dstColor; - 'src' has the same format as the 'bytes' member of struct raster. - 'srcwid' is the size in bytes of each row, like raster.rowSize. + getRgb(dst, dstCursor, &dstColor); + (*trf)(&colorMap[colorIndex], &dstColor); + putRgb(dstColor, dstCursor, dst); + } else + putRgb(colorMap[colorIndex], dstCursor, dst); + } + } +} - We use only the first 'ysize' rows and only the first 'xsize' - pixels of each row. - We really should clean this up so that we can take pixels out of - the middle of a row and rows out of the middle of the raster. As - it stands, Caller achieves the same result by passing as 'src' - a pointer into the middle of a raster -- the upper left corner of - the rectangle he wants. But that is messy and nonobvious. - Each plane of 'dst' is one word per pixel and contains actual - colors, never a palette index. It is an array in row-major order - with 'dstwid' words per row. ------------------------------------------------------------------------------*/ - unsigned int const xsize = rectwidth(&srcRect); - unsigned int const ysize = rectheight(&srcRect); +static void +doSameSize16bpp(transfer_func trf, + unsigned int const xsize, + unsigned int const ysize, + unsigned char * const src, + unsigned int const srcwid, + struct RgbPlanes const dst, + unsigned int const dstwid) { - switch (pixSize) { - case 8: { - unsigned int rowNumber; + unsigned int rowNumber; - for (rowNumber = 0; rowNumber < ysize; ++rowNumber) { - unsigned char * const srcrow = &src[rowNumber * srcwid]; - unsigned int const dstRowCurs = rowNumber * dstwid; + for (rowNumber = 0; rowNumber < ysize; ++rowNumber) { + unsigned char * const row = &src[rowNumber * srcwid]; + unsigned int const dstRowCurs = rowNumber * dstwid; - unsigned int colNumber; - for (colNumber = 0; colNumber < xsize; ++colNumber) { - unsigned int const dstCursor = dstRowCurs + colNumber; - unsigned int const colorIndex = srcrow[colNumber]; - struct RGBColor dstColor; - getRgb(dst, dstCursor, &dstColor); - (*trf)(&color_map[colorIndex], &dstColor); - putRgb(dstColor, dstCursor, dst); - } - } - } break; - case 16: { - unsigned int rowNumber; + unsigned int colNumber; + for (colNumber = 0; colNumber < xsize; ++colNumber) { + unsigned int const dstCursor = dstRowCurs + colNumber; + struct RGBColor const srcColor = decode16(&row[colNumber*2]); + + struct RGBColor scaledSrcColor; - for (rowNumber = 0; rowNumber < ysize; ++rowNumber) { - unsigned char * const row = &src[rowNumber * srcwid]; - unsigned int const dstRowCurs = rowNumber * dstwid; + scaledSrcColor.red = srcColor.red << 11; + scaledSrcColor.grn = srcColor.grn << 11; + scaledSrcColor.blu = srcColor.blu << 11; - unsigned int colNumber; - for (colNumber = 0; colNumber < xsize; ++colNumber) { - unsigned int const dstCursor = dstRowCurs + colNumber; - struct RGBColor const srcColor = decode16(&row[colNumber*2]); + if (trf) { struct RGBColor dstColor; - struct RGBColor scaledSrcColor; - scaledSrcColor.red = srcColor.red << 11; - scaledSrcColor.grn = srcColor.grn << 11; - scaledSrcColor.blu = srcColor.blu << 11; + getRgb(dst, dstCursor, &dstColor); (*trf)(&scaledSrcColor, &dstColor); putRgb(dstColor, dstCursor, dst); - } + } else + putRgb(scaledSrcColor, dstCursor, dst); } - } break; - case 32: { - unsigned int const planeSize = xsize; - unsigned int rowNumber; + } +} + + + +static void +doSameSize32bpp(transfer_func trf, + unsigned int const xsize, + unsigned int const ysize, + unsigned char * const src, + unsigned int const srcwid, + struct RgbPlanes const dst, + unsigned int const dstwid) { + + unsigned int const planeSize = xsize; + + unsigned int rowNumber; + + for (rowNumber = 0; rowNumber < ysize; ++rowNumber) { + unsigned char * const row = &src[rowNumber * srcwid]; + unsigned char * const redPlane = &row[planeSize * 0]; + unsigned char * const grnPlane = &row[planeSize * 1]; + unsigned char * const bluPlane = &row[planeSize * 2]; + unsigned int const dstRowCurs = rowNumber * dstwid; - for (rowNumber = 0; rowNumber < ysize; ++rowNumber) { - unsigned char * const row = &src[rowNumber * srcwid]; - unsigned char * const redPlane = &row[planeSize * 0]; - unsigned char * const grnPlane = &row[planeSize * 1]; - unsigned char * const bluPlane = &row[planeSize * 2]; - unsigned int const dstRowCurs = rowNumber * dstwid; + unsigned int colNumber; - unsigned int colNumber; + for (colNumber = 0; colNumber < xsize; ++colNumber) { + unsigned int const dstCursor = dstRowCurs + colNumber; + + struct RGBColor srcColor; + + srcColor.red = redPlane[colNumber] << 8; + srcColor.grn = grnPlane[colNumber] << 8; + srcColor.blu = bluPlane[colNumber] << 8; + + if (trf) { + struct RGBColor dstColor; - for (colNumber = 0; colNumber < xsize; ++colNumber) { - unsigned int const dstCursor = dstRowCurs + colNumber; - struct RGBColor srcColor, dstColor; getRgb(dst, dstCursor, &dstColor); - srcColor.red = redPlane[colNumber] << 8; - srcColor.grn = grnPlane[colNumber] << 8; - srcColor.blu = bluPlane[colNumber] << 8; (*trf)(&srcColor, &dstColor); putRgb(dstColor, dstCursor, dst); - } + } else + putRgb(srcColor, dstCursor, dst); } - } break; - default: - pm_error("Impossible value of pixSize: %u", pixSize); } } static void -blitIdempotent(unsigned int const pixSize, - struct Rect const srcRect, - unsigned char * const src, - unsigned int const srcwid, - struct RGBColor * const colorMap, - struct RgbPlanes const dst, - unsigned int const dstwid) { +doSameSize(transfer_func trf, + int const pixSize, + struct Rect const srcRect, + unsigned char * const src, + unsigned int const srcwid, + struct RGBColor * const colorMap, + struct RgbPlanes const dst, + unsigned int const dstwid) { /*---------------------------------------------------------------------------- - This is the same as doSameSize(), except optimized for the case that - the transfer function is idempotent (i.e. it's just a straight copy). - The original author's comments suggest that this optimization isn't - all that important -- that he just wrote this first and instead of - expanding it to handle arbitrary transfer functions, added functions - for that. + Transfer pixels from 'src' to 'dst', applying the transfer function + 'trf'. + + 'src' has the same format as the 'bytes' member of struct Raster. + 'srcwid' is the size in bytes of each row, like raster.rowSize. + Note that there may be padding in there; there isn't necessarily + 'srcwid' bytes of information in a row. + + We use only the first 'ysize' rows and only the first 'xsize' + pixels of each row. + + We really should clean this up so that we can take pixels out of + the middle of a row and rows out of the middle of the raster. As + it stands, Caller achieves the same result by passing as 'src' + a pointer into the middle of a raster -- the upper left corner of + the rectangle he wants. But that is messy and nonobvious. + + Each plane of 'dst' is one word per pixel and contains actual + colors, never a palette index. It is an array in row-major order + with 'dstwid' words per row. -----------------------------------------------------------------------------*/ unsigned int const xsize = rectwidth(&srcRect); unsigned int const ysize = rectheight(&srcRect); switch (pixSize) { - case 8: { - unsigned int rowNumber; - - for (rowNumber = 0; rowNumber < ysize; ++rowNumber) { - unsigned char * const srcrow = &src[rowNumber * srcwid]; - unsigned int const dstRowCurs = rowNumber * dstwid; - unsigned int colNumber; - for (colNumber = 0; colNumber < xsize; ++colNumber) { - unsigned int const dstCursor = dstRowCurs + colNumber; - struct RGBColor * const ct = colorMap + srcrow[colNumber]; - dst.red[dstCursor] = ct->red; - dst.grn[dstCursor] = ct->grn; - dst.blu[dstCursor] = ct->blu; - } - } - } break; - case 16: { - unsigned int rowNumber; - - for (rowNumber = 0; rowNumber < ysize; ++rowNumber) { - unsigned char * const srcrow = &src[rowNumber * srcwid]; - unsigned int const dstRowCurs = rowNumber * dstwid; - unsigned int colNumber; - for (colNumber = 0; colNumber < xsize; ++colNumber) { - unsigned int const dstCursor = dstRowCurs + colNumber; - struct RGBColor const srcColor = - decode16(&srcrow[colNumber * 2]); - dst.red[dstCursor] = srcColor.red << 11; - dst.grn[dstCursor] = srcColor.grn << 11; - dst.blu[dstCursor] = srcColor.blu << 11; - } - } - } break; - case 32: { - unsigned int const planeSize = xsize; - unsigned int rowNumber; - - for (rowNumber = 0; rowNumber < ysize; ++rowNumber) { - unsigned char * const srcrow = &src[rowNumber * srcwid]; - unsigned char * const redPlane = &srcrow[planeSize * 0]; - unsigned char * const grnPlane = &srcrow[planeSize * 1]; - unsigned char * const bluPlane = &srcrow[planeSize * 2]; - unsigned int const dstRowCurs = rowNumber * dstwid; - - unsigned int colNumber; - for (colNumber = 0; colNumber < xsize; ++colNumber) { - unsigned int const dstCursor = dstRowCurs + colNumber; - dst.red[dstCursor] = redPlane[colNumber] << 8; - dst.grn[dstCursor] = grnPlane[colNumber] << 8; - dst.blu[dstCursor] = bluPlane[colNumber] << 8; - } - } - } break; + case 8: + doSameSize8bpp(trf, xsize, ysize, src, srcwid, colorMap, dst, dstwid); + break; + case 16: + doSameSize16bpp(trf, xsize, ysize, src, srcwid, dst, dstwid); + break; + case 32: + doSameSize32bpp(trf, xsize, ysize, src, srcwid, dst, dstwid); + break; default: - pm_error("INTERNAL ERROR: invalid bits per pixel (%u) in " - "blitIdempotent()", pixSize); + pm_error("Impossible value of pixSize: %u", pixSize); } } @@ -1636,16 +1750,15 @@ static void doBlit(struct Rect const srcRect, struct Rect const dstRect, struct Rect const srcBounds, - struct raster const srcplane, + struct Raster const srcplane, struct Rect const dstBounds, struct RgbPlanes const canvasPlanes, int const pixSize, int const dstwid, - struct RGBColor * const color_map, - int const mode) { + struct RGBColor * const colorMap, + unsigned int const mode) { /*---------------------------------------------------------------------------- - Transfer some pixels from 'srcplane' to 'canvasPlanes', applying the - transfer function 'trf'. + Transfer some pixels from 'srcplane' to 'canvasPlanes'. 'srcplane' contains the rectangle 'srcBounds' of the image. 'canvasPlanes' contains the rectangle 'dstRect' of the image. @@ -1661,6 +1774,9 @@ doBlit(struct Rect const srcRect, struct RgbPlanes dst; int dstoff; transfer_func trf; + /* A transfer function to use as we transfer the pixels. + NULL for none. + */ if (verbose) { dumpRect("copying from:", srcRect); @@ -1697,35 +1813,31 @@ doBlit(struct Rect const srcRect, if ((mode & ~64) == 0) trf = NULL; /* optimized srcCopy */ else - trf = transfer(mode & ~64); + trf = transferFunctionForMode(mode & ~64); if (!rectsamesize(srcRect, dstRect)) doDiffSize(srcRect, dstRect, pixSize, - trf, color_map, src, srcplane.rowSize, dst); + trf, colorMap, src, srcplane.rowSize, dst); else { - if (trf == NULL) - blitIdempotent(pixSize, srcRect, src, srcplane.rowSize, - color_map, dst, dstwid); - else - doSameSize(trf, pixSize, srcRect, src, srcplane.rowSize, - color_map, dst, dstwid); + doSameSize(trf, pixSize, srcRect, src, srcplane.rowSize, + colorMap, dst, dstwid); } } -static int +static void blit(struct Rect const srcRect, struct Rect const srcBounds, - struct raster const srcplane, - struct canvas * const canvasP, - blitList * const blitListP, + struct Raster const srcplane, + struct Canvas * const canvasP, + BlitList * const blitListP, int const pixSize, struct Rect const dstRect, struct Rect const dstBounds, int const dstwid, struct RGBColor * const color_map, - int const mode) { + unsigned int const mode) { /*---------------------------------------------------------------------------- 'srcplane' contains the rectangle 'srcBounds' of the image. @@ -1734,19 +1846,8 @@ blit(struct Rect const srcRect, if 'blitListP' is non-null, we don't draw anything on 'canvasP'; instead, we add to the list *blitlistP a description of what needs to be drawn. -----------------------------------------------------------------------------*/ - - /* I can't tell what the result value of this function is supposed to mean, - but I found several return statements that did not set it to anything, - and several calls that examine it. I'm guessing that "1" is the - appropriate thing to return in those cases, so I made it so. - -Bryan 00.03.02 - */ - - int retval; - - if (ps_text) - retval = 1; - else { + if (ps_text) { + } else { /* Almost got it. Clip source rect with source bounds. clip dest rect with dest bounds. */ @@ -1760,17 +1861,12 @@ blit(struct Rect const srcRect, addBlitList(blitListP, clipsrc, srcBounds, srcplane, pixSize, clipdst, color_map, mode); - - retval = 0; } else { doBlit(clipsrc, clipdst, srcBounds, srcplane, dstBounds, canvasP->planes, pixSize, dstwid, color_map, mode); - - retval = 1; } } - return retval; } @@ -1828,11 +1924,11 @@ compact(Word const input) { static void -reportBlitList(blitList * const blitListP) { +reportBlitList(BlitList * const blitListP) { if (verbose) { unsigned int count; - struct blit_info * biP; + struct BlitInfo * biP; for (count = 0, biP = blitListP->firstP; biP; biP = biP->next) ++count; @@ -1844,15 +1940,15 @@ reportBlitList(blitList * const blitListP) { static void -doBlitList(struct canvas * const canvasP, - blitList * const blitListP) { +doBlitList(struct Canvas * const canvasP, + BlitList * const blitListP) { /*---------------------------------------------------------------------------- Do the list of blits *blitListP, drawing on canvas *canvasP. We allocate new plane data structures in *canvasP. We assume it doesn't have them already. -----------------------------------------------------------------------------*/ - struct blit_info * bi; + struct BlitInfo * bi; int srcwidth, dstwidth, srcheight, dstheight; double scale, scalelow, scalehigh; double xscale = 1.0; @@ -1862,8 +1958,6 @@ doBlitList(struct canvas * const canvasP, reportBlitList(blitListP); - fullres = 0; - for (bi = blitListP->firstP; bi; bi = bi->next) { srcwidth = rectwidth(&bi->srcRect); dstwidth = rectwidth(&bi->dstRect); @@ -1940,7 +2034,7 @@ doBlitList(struct canvas * const canvasP, } if (xscale != 1.0 || yscale != 1.0) { - struct blit_info * biP; + struct BlitInfo * biP; for (biP = blitListP->firstP; biP; biP = biP->next) rectscale(&biP->dstRect, xscale, yscale); @@ -2007,19 +2101,20 @@ outputPpm(FILE * const ofP, * is padded with a null. */ static Word -nextOp(int const version) { +nextOp(FILE * const ifP, + unsigned int const version) { if ((align & 0x1) && version == 2) { stage = "aligning for opcode"; - readByte(); + readByte(ifP); } stage = "reading opcode"; if (version == 1) - return readByte(); + return readByte(ifP); else - return readWord(); + return readWord(ifP); } @@ -2027,11 +2122,12 @@ nextOp(int const version) { static drawFn ClipRgn; static void -ClipRgn(struct canvas * const canvasP, - blitList * const blitListP, +ClipRgn(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - Word const len = readWord(); + Word const len = readWord(ifP); /* Length in bytes of the parameter (including this word) */ if (len == 10) { /* null rgn */ @@ -2046,7 +2142,7 @@ ClipRgn(struct canvas * const canvasP, */ struct Rect clipRgnParm; - readRect(&clipRgnParm); + readRect(ifP, &clipRgnParm); rectintersect(&clipRgnParm, &picFrame, &clip_rect); @@ -2060,7 +2156,7 @@ ClipRgn(struct canvas * const canvasP, if (verbose) dumpRect("clipping to", clip_rect); } else - skip(len - 2); + skip(ifP, len - 2); } @@ -2068,35 +2164,37 @@ ClipRgn(struct canvas * const canvasP, static drawFn OpColor; static void -OpColor(struct canvas * const canvasP, - blitList * const blitListP, +OpColor(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - op_color.red = readWord(); - op_color.grn = readWord(); - op_color.blu = readWord(); + op_color.red = readWord(ifP); + op_color.grn = readWord(ifP); + op_color.blu = readWord(ifP); } static void -readPixmap(struct pixMap * const p) { +readPixmap(FILE * const ifP, + struct PixMap * const p) { stage = "getting pixMap header"; - readRect(&p->Bounds); - p->version = readWord(); - p->packType = readWord(); - p->packSize = readLong(); - p->hRes = readLong(); - p->vRes = readLong(); - p->pixelType = readWord(); - p->pixelSize = readWord(); - p->cmpCount = readWord(); - p->cmpSize = readWord(); - p->planeBytes = readLong(); - p->pmTable = readLong(); - p->pmReserved = readLong(); + readRect(ifP, &p->Bounds); + p->version = readWord(ifP); + p->packType = readWord(ifP); + p->packSize = readLong(ifP); + p->hRes = readLong(ifP); + p->vRes = readLong(ifP); + p->pixelType = readWord(ifP); + p->pixelSize = readWord(ifP); + p->cmpCount = readWord(ifP); + p->cmpSize = readWord(ifP); + p->planeBytes = readLong(ifP); + p->pmTable = readLong(ifP); + p->pmReserved = readLong(ifP); if (verbose) { pm_message("pixelType: %d", p->pixelType); @@ -2119,19 +2217,20 @@ readPixmap(struct pixMap * const p) { static struct RGBColor* -readColorTable(void) { +readColorTable(FILE * const ifP) { + Longword ctSeed; Word ctFlags; Word ctSize; Word val; int i; - struct RGBColor* color_table; + struct RGBColor* colorTable; stage = "getting color table info"; - ctSeed = readLong(); - ctFlags = readWord(); - ctSize = readWord(); + ctSeed = readLong(ifP); + ctFlags = readWord(ifP); + ctSize = readWord(ifP); if (verbose) { pm_message("ctSeed: %ld", ctSeed); @@ -2141,12 +2240,12 @@ readColorTable(void) { stage = "reading color table"; - MALLOCARRAY(color_table, ctSize + 1); - if (color_table == NULL) + MALLOCARRAY(colorTable, ctSize + 1); + if (!colorTable) pm_error("no memory for color table"); - for (i = 0; i <= ctSize; i++) { - val = readWord(); + for (i = 0; i <= ctSize; ++i) { + val = readWord(ifP); /* The indices in a device color table are bogus and usually == 0. * so I assume we allocate up the list of colors in order. */ @@ -2154,18 +2253,19 @@ readColorTable(void) { val = i; if (val > ctSize) pm_error("pixel value greater than color table size"); - color_table[val].red = readWord(); - color_table[val].grn = readWord(); - color_table[val].blu = readWord(); + + colorTable[val].red = readWord(ifP); + colorTable[val].grn = readWord(ifP); + colorTable[val].blu = readWord(ifP); if (verbose > 1) pm_message("Color %3u: [%u,%u,%u]", val, - color_table[val].red, - color_table[val].grn, - color_table[val].blu); + colorTable[val].red, + colorTable[val].grn, + colorTable[val].blu); } - return color_table; + return colorTable; } @@ -2261,7 +2361,7 @@ expand1Bit(unsigned char * const packed, static void unpackBuf(unsigned char * const packed, unsigned int const packedLen, - int const bitsPerPixel, + unsigned int const bitsPerPixel, unsigned char ** const expandedP, unsigned int * const expandedLenP) { /*---------------------------------------------------------------------------- @@ -2317,7 +2417,7 @@ unpackBuf(unsigned char * const packed, static void unpackUncompressedBits(FILE * const ifP, - struct raster const raster, + struct Raster const raster, unsigned int const rowBytes, unsigned int const bitsPerPixel) { /*---------------------------------------------------------------------------- @@ -2332,6 +2432,9 @@ unpackUncompressedBits(FILE * const ifP, unsigned int rowOfRect; unsigned char * linebuf; + if (verbose) + pm_message("Bits are not packed"); + MALLOCARRAY(linebuf, rowBytes + 100); if (linebuf == NULL) pm_error("can't allocate memory for line buffer"); @@ -2364,13 +2467,15 @@ reportValidateCompressedLineLen(unsigned int const row, unsigned int const linelen, unsigned int const rowSize) { /*---------------------------------------------------------------------------- - 'row' is a row number in the raster. + Report the line length and fail the program if it is obviously wrong. - 'linelen' is the number of bytes of PICT that the PICT says hold the - compressed version of that row. + 'row' is a row number in the raster. - 'rowSize' is the number of bytes we expect the uncompressed line to - be (includes pad pixels on the right). + 'linelen' is the number of bytes of PICT that the PICT says hold the + compressed version of that row. + + 'rowSize' is the number of bytes we expect the uncompressed line to + be (includes pad pixels on the right). -----------------------------------------------------------------------------*/ if (verbose > 1) pm_message("Row %u: %u-byte compressed line", row, linelen); @@ -2428,7 +2533,7 @@ expandRun(unsigned char * const block, assert(block[0] & 0x80); /* It's a run */ - if (verbose > 1) + if (verbose > 2) pm_message("Block: run of %u packed %u-byte units", runLength, pkpixsize); @@ -2507,7 +2612,7 @@ copyPixelGroup(unsigned char * const block, assert(blockLimit >= 1); /* block[0] exists */ assert((block[0] & 0x80) == 0); /* It's not a run */ - if (verbose > 1) + if (verbose > 2) pm_message("Block: %u explicit packed %u-byte units", groupLen, pkpixsize); @@ -2576,13 +2681,13 @@ interpretCompressedLine(unsigned char * const linebuf, unsigned int const rowSize, unsigned int const bitsPerPixel) { /*---------------------------------------------------------------------------- - linebuf[] contains 'linelen' bytes from the PICT image that represents - one row of the image, in compressed format. Return the - uncompressed pixels of that row as rowRaster[]. + linebuf[] contains 'linelen' bytes from the PICT image that represents one + row of the image, in compressed format. Return the uncompressed pixels of + that row as rowRaster[]. - rowRaster[] has 'rowSize' bytes of space. Caller ensures that - linebuf[] does not contain more pixels than that, unless the PICT - image from which it comes is corrupt. + rowRaster[] has 'rowSize' bytes of space. Caller ensures that linebuf[] + does not contain more pixels than that, unless the PICT image from which it + comes is corrupt. -----------------------------------------------------------------------------*/ unsigned int lineCursor; /* Cursor into linebuf[] -- the compressed data */ @@ -2609,7 +2714,8 @@ interpretCompressedLine(unsigned char * const linebuf, assert(rasterCursor <= rowSize); } if (verbose > 1) - pm_message("Got %u pixels for row", rasterCursor); + pm_message("Decompressed %u bytes into %u bytes for row", + lineCursor, rasterCursor); } @@ -2642,11 +2748,11 @@ interpretCompressedLine(unsigned char * const linebuf, static void unpackCompressedBits(FILE * const ifP, - struct raster const raster, + struct Raster const raster, unsigned int const rowBytes, unsigned int const bitsPerPixel) { /*---------------------------------------------------------------------------- - Read the raster on file *ifP and place it in 'raster'. + Set the raster bytes of 'raster' with bytes read from *ifP. The data in the file is compressed with run length encoding and possibly packed multiple pixels per byte as well. @@ -2665,6 +2771,9 @@ unpackCompressedBits(FILE * const ifP, unsigned char * linebuf; unsigned int linebufSize; + if (verbose) + pm_message("Bits are packed"); + linebufSize = rowBytes; MALLOCARRAY(linebuf, linebufSize); if (linebuf == NULL) @@ -2676,9 +2785,9 @@ unpackCompressedBits(FILE * const ifP, unsigned int linelen; if (llsize == 2) - linelen = readWord(); + linelen = readWord(ifP); else - linelen = readByte(); + linelen = readByte(ifP); reportValidateCompressedLineLen(row, linelen, raster.rowSize); @@ -2702,19 +2811,19 @@ static void unpackbits(FILE * const ifP, struct Rect * const boundsP, Word const rowBytesArg, - int const bitsPerPixel, - struct raster * const rasterP) { + unsigned int const bitsPerPixel, + struct Raster * const rasterP) { unsigned int const rectHeight = boundsP->bottom - boundsP->top; unsigned int const rectWidth = boundsP->right - boundsP->left; - struct raster raster; + struct Raster raster; unsigned int rowBytes; stage = "unpacking packbits"; if (verbose) - pm_message("rowBytes = %u, bitsPerPixel = %d", + pm_message("rowBytes = %u, bitsPerPixel = %u", rowBytesArg, bitsPerPixel); allocateRaster(&raster, rectWidth, rectHeight, bitsPerPixel); @@ -2754,33 +2863,33 @@ interpretRowBytesWord(Word const rowBytesWord, * a pattern in the fabled complete version. */ static void -readPattern(void) { +readPattern(FILE * const ifP) { Word PatType; stage = "Reading a pattern"; - PatType = readWord(); + PatType = readWord(ifP); switch (PatType) { case 2: - skip(8); /* old pattern data */ - skip(5); /* RGB for pattern */ + skip(ifP, 8); /* old pattern data */ + skip(ifP, 5); /* RGB for pattern */ break; case 1: { Word rowBytesWord; bool pixMap; unsigned int rowBytes; - struct pixMap p; - struct raster raster; + struct PixMap p; + struct Raster raster; struct RGBColor * ct; - skip(8); /* old pattern data */ - rowBytesWord = readWord(); + skip(ifP, 8); /* old pattern data */ + rowBytesWord = readWord(ifP); interpretRowBytesWord(rowBytesWord, &pixMap, &rowBytes); - readPixmap(&p); - ct = readColorTable(); - unpackbits(ifp, &p.Bounds, rowBytes, p.pixelSize, &raster); + readPixmap(ifP, &p); + ct = readColorTable(ifP); + unpackbits(ifP, &p.Bounds, rowBytes, p.pixelSize, &raster); freeRaster(raster); free(ct); } break; @@ -2791,16 +2900,17 @@ readPattern(void) { -/* these 3 do nothing but skip over their data! */ +/* These three do nothing but skip over their data! */ static drawFn BkPixPat; static void -BkPixPat(struct canvas * const canvasP, - blitList * const blitListP, +BkPixPat(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - readPattern(); + readPattern(ifP); } @@ -2808,11 +2918,12 @@ BkPixPat(struct canvas * const canvasP, static drawFn PnPixPat; static void -PnPixPat(struct canvas * const canvasP, - blitList * const blitListP, +PnPixPat(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - readPattern(); + readPattern(ifP); } @@ -2820,17 +2931,20 @@ PnPixPat(struct canvas * const canvasP, static drawFn FillPixPat; static void -FillPixPat(struct canvas * const canvasP, - blitList * const blitListP, +FillPixPat(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - readPattern(); + readPattern(ifP); } static void -read8x8Pattern(struct Pattern * const pat) { +read8x8Pattern(FILE * const ifP, + struct Pattern * const pat) { + unsigned char buf[8]; unsigned char * exp; unsigned int len; @@ -2838,7 +2952,7 @@ read8x8Pattern(struct Pattern * const pat) { unsigned int i; len = 8; /* initial value */ - readBytes(ifp, len, buf); + readBytes(ifP, len, buf); if (verbose) { pm_message("pattern: %02x%02x%02x%02x", buf[0], buf[1], buf[2], buf[3]); @@ -2855,11 +2969,12 @@ read8x8Pattern(struct Pattern * const pat) { static drawFn BkPat; static void -BkPat(struct canvas * const canvasP, - blitList * const blitListP, +BkPat(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - read8x8Pattern(&bkpat); + read8x8Pattern(ifP, &bkpat); } @@ -2867,11 +2982,12 @@ BkPat(struct canvas * const canvasP, static drawFn PnPat; static void -PnPat(struct canvas * const canvasP, - blitList * const blitListP, +PnPat(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - read8x8Pattern(&pen_pat); + read8x8Pattern(ifP, &pen_pat); } @@ -2879,11 +2995,12 @@ PnPat(struct canvas * const canvasP, static drawFn FillPat; static void -FillPat(struct canvas * const canvasP, - blitList * const blitListP, +FillPat(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - read8x8Pattern(&fillpat); + read8x8Pattern(ifP, &fillpat); } @@ -2891,12 +3008,14 @@ FillPat(struct canvas * const canvasP, static drawFn PnSize; static void -PnSize(struct canvas * const canvasP, - blitList * const blitListP, +PnSize(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - pen_height = readWord(); - pen_width = readWord(); + pen_height = readWord(ifP); + pen_width = readWord(ifP); + if (verbose) pm_message("pen size %d x %d", pen_width, pen_height); } @@ -2906,28 +3025,31 @@ PnSize(struct canvas * const canvasP, static drawFn PnSize; static void -PnMode(struct canvas * const canvasP, - blitList * const blitListP, +PnMode(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - pen_mode = readWord(); + pen_mode = readWord(ifP); if (pen_mode >= 8 && pen_mode < 15) pen_mode -= 8; if (verbose) pm_message("pen transfer mode = %s", - const_name(transfer_name, pen_mode)); + constName(transfer_name, pen_mode)); - pen_trf = transfer(pen_mode); + pen_trf = transferFunctionForMode(pen_mode); } static void -readRgb(struct RGBColor * const rgb) { - rgb->red = readWord(); - rgb->grn = readWord(); - rgb->blu = readWord(); +readRgb(FILE * const ifP, + struct RGBColor * const rgb) { + + rgb->red = readWord(ifP); + rgb->grn = readWord(ifP); + rgb->blu = readWord(ifP); } @@ -2935,11 +3057,13 @@ readRgb(struct RGBColor * const rgb) { static drawFn RGBFgCol; static void -RGBFgCol(struct canvas * const canvasP, - blitList * const blitListP, +RGBFgCol(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - readRgb(&foreground); + readRgb(ifP, &foreground); + if (verbose) pm_message("foreground now [%d,%d,%d]", foreground.red, foreground.grn, foreground.blu); @@ -2950,11 +3074,13 @@ RGBFgCol(struct canvas * const canvasP, static drawFn RGBBkCol; static void -RGBBkCol(struct canvas * const canvasP, - blitList * const blitListP, +RGBBkCol(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - readRgb(&background); + readRgb(ifP, &background); + if (verbose) pm_message("background now [%d,%d,%d]", background.red, background.grn, background.blu); @@ -2978,7 +3104,7 @@ pixelIndex(struct Rect const picFrame, static void -drawPixel(struct canvas * const canvasP, +drawPixel(struct Canvas * const canvasP, int const x, int const y, struct RGBColor * const clr, @@ -3006,7 +3132,7 @@ drawPixel(struct canvas * const canvasP, static void -drawPenRect(struct canvas * const canvasP, +drawPenRect(struct Canvas * const canvasP, struct Rect * const rP) { if (!rectisnull(rP)) { @@ -3051,7 +3177,7 @@ drawPenRect(struct canvas * const canvasP, static void -drawPen(struct canvas * const canvasP, +drawPen(struct Canvas * const canvasP, int const x, int const y) { @@ -3082,7 +3208,7 @@ drawPen(struct canvas * const canvasP, * Paul Heckbert 3 Sep 85 */ static void -scanLine(struct canvas * const canvasP, +scanLine(struct Canvas * const canvasP, short const x1, short const y1, short const x2, @@ -3133,16 +3259,19 @@ scanLine(struct canvas * const canvasP, static drawFn Line; static void -Line(struct canvas * const canvasP, - blitList * const blitListP, +Line(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { struct Point p1; - readPoint(&p1); - readPoint(¤t); + readPoint(ifP, &p1); + readPoint(ifP, ¤t); + if (verbose) pm_message("(%d,%d) to (%d, %d)", p1.x,p1.y,current.x,current.y); + scanLine(canvasP, p1.x,p1.y,current.x,current.y); } @@ -3151,12 +3280,15 @@ Line(struct canvas * const canvasP, static drawFn LineFrom; static void -LineFrom(struct canvas * const canvasP, - blitList * const blitListP, +LineFrom(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { struct Point p1; - readPoint(&p1); + + readPoint(ifP, &p1); + if (verbose) pm_message("(%d,%d) to (%d, %d)", current.x, current.y, p1.x, p1.y); @@ -3172,15 +3304,19 @@ LineFrom(struct canvas * const canvasP, static drawFn ShortLine; static void -ShortLine(struct canvas * const canvasP, - blitList * const blitListP, +ShortLine(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { struct Point p1; - readPoint(&p1); - readShortPoint(¤t); + + readPoint(ifP,&p1); + readShortPoint(ifP, ¤t); + if (verbose) pm_message("(%d,%d) delta (%d, %d)", p1.x, p1.y, current.x, current.y); + current.x += p1.x; current.y += p1.y; @@ -3193,19 +3329,25 @@ ShortLine(struct canvas * const canvasP, static drawFn ShortLineFrom; static void -ShortLineFrom(struct canvas * const canvasP, - blitList * const blitListP, +ShortLineFrom(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { struct Point p1; - readShortPoint(&p1); + + readShortPoint(ifP, &p1); + if (verbose) pm_message("(%d,%d) delta (%d, %d)", current.x,current.y,p1.x,p1.y); + p1.x += current.x; p1.y += current.y; + if (!blitListP) scanLine(canvasP, current.x, current.y, p1.x, p1.y); + current.x = p1.x; current.y = p1.y; } @@ -3213,7 +3355,7 @@ ShortLineFrom(struct canvas * const canvasP, static void -doPaintRect(struct canvas * const canvasP, +doPaintRect(struct Canvas * const canvasP, struct Rect const prect) { struct Rect rect; @@ -3231,11 +3373,13 @@ doPaintRect(struct canvas * const canvasP, static drawFn paintRect; static void -paintRect(struct canvas * const canvasP, - blitList * const blitListP, +paintRect(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - readRect(&cur_rect); + readRect(ifP, &cur_rect); + if (!blitListP) doPaintRect(canvasP, cur_rect); } @@ -3245,8 +3389,9 @@ paintRect(struct canvas * const canvasP, static drawFn paintSameRect; static void -paintSameRect(struct canvas * const canvasP, - blitList * const blitListP, +paintSameRect(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { if (!blitListP) @@ -3256,7 +3401,7 @@ paintSameRect(struct canvas * const canvasP, static void -doFrameRect(struct canvas * const canvasP, +doFrameRect(struct Canvas * const canvasP, struct Rect const rect) { if (verbose) @@ -3282,11 +3427,12 @@ doFrameRect(struct canvas * const canvasP, static drawFn frameRect; static void -frameRect(struct canvas * const canvasP, - blitList * const blitListP, +frameRect(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - readRect(&cur_rect); + readRect(ifP, &cur_rect); if (!blitListP) doFrameRect(canvasP, cur_rect); @@ -3297,8 +3443,9 @@ frameRect(struct canvas * const canvasP, static drawFn frameSameRect; static void -frameSameRect(struct canvas * const canvasP, - blitList * const blitListP, +frameSameRect(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { if (!blitListP) @@ -3343,7 +3490,7 @@ polySort(int const sort_index, struct Point points[]) { /* Watch out for the lack of error checking in the next two functions ... */ static void -scanPoly(struct canvas * const canvasP, +scanPoly(struct Canvas * const canvasP, int const np, struct Point pts[]) { @@ -3448,17 +3595,22 @@ scanPoly(struct canvas * const canvasP, static drawFn paintPoly; static void -paintPoly(struct canvas * const canvasP, - blitList * const blitListP, +paintPoly(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { struct Rect bb; struct Point pts[100]; - int i, np = (readWord() - 10) >> 2; + int i; + int np; + + np = (readWord(ifP) - 10) >> 2; - readRect(&bb); - for (i=0; i<np; ++i) - readPoint(&pts[i]); + readRect(ifP, &bb); + + for (i = 0; i < np; ++i) + readPoint(ifP, &pts[i]); /* scan convert poly ... */ if (!blitListP) @@ -3470,11 +3622,14 @@ paintPoly(struct canvas * const canvasP, static drawFn PnLocHFrac; static void -PnLocHFrac(struct canvas * const canvasP, - blitList * const blitListP, +PnLocHFrac(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - Word frac = readWord(); + Word frac; + + frac = readWord(ifP); if (verbose) pm_message("PnLocHFrac = %d", frac); @@ -3485,20 +3640,21 @@ PnLocHFrac(struct canvas * const canvasP, static drawFn TxMode; static void -TxMode(struct canvas * const canvasP, - blitList * const blitListP, +TxMode(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - text_mode = readWord(); + text_mode = readWord(ifP); if (text_mode >= 8 && text_mode < 15) text_mode -= 8; if (verbose) pm_message("text transfer mode = %s", - const_name(transfer_name, text_mode)); + constName(transfer_name, text_mode)); /* ignore the text mask bit 'cause we don't handle it yet */ - text_trf = transfer(text_mode & ~64); + text_trf = transferFunctionForMode(text_mode & ~64); } @@ -3506,13 +3662,15 @@ TxMode(struct canvas * const canvasP, static drawFn TxFont; static void -TxFont(struct canvas * const canvasP, - blitList * const blitListP, +TxFont(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - text_font = readWord(); + text_font = readWord(ifP); + if (verbose) - pm_message("text font %s", const_name(font_name, text_font)); + pm_message("text font %s", constName(font_name, text_font)); } @@ -3520,11 +3678,13 @@ TxFont(struct canvas * const canvasP, static drawFn TxFace; static void -TxFace(struct canvas * const canvasP, - blitList * const blitListP, +TxFace(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - text_face = readByte(); + text_face = readByte(ifP); + if (verbose) pm_message("text face %d", text_face); } @@ -3534,11 +3694,13 @@ TxFace(struct canvas * const canvasP, static drawFn TxSize; static void -TxSize(struct canvas * const canvasP, - blitList * const blitListP, +TxSize(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - text_size = readWord(); + text_size = readWord(ifP); + if (verbose) pm_message("text size %d", text_size); } @@ -3546,9 +3708,10 @@ TxSize(struct canvas * const canvasP, static void -skipText(blitList * const blitListP) { +skipText(FILE * const ifP, + BlitList * const blitListP) { - skip(readByte()); + skip(ifP, readByte(ifP)); blitListP->unblittableText = true; } @@ -3571,7 +3734,7 @@ getFont(int const font, int const style) { int closeness, bestcloseness; - struct fontinfo* fi, *best; + struct FontInfo* fi, *best; best = 0; for (fi = fontlist; fi; fi = fi->next) { @@ -3631,7 +3794,8 @@ rotate(int * const x, static void -doPsText(struct canvas * const canvasP, +doPsText(FILE * const ifP, + struct Canvas * const canvasP, Word const tx, Word const ty) { @@ -3648,12 +3812,12 @@ doPsText(struct canvas * const canvasP, ps_cent_set = 1; } - len = readByte(); + len = readByte(ifP); /* XXX this width calculation is not completely correct */ width = 0; for (i = 0; i < len; i++) { - ch = str[i] = readByte(); + ch = str[i] = readByte(ifP); if (tfont->glyph[ch]) width += tfont->glyph[ch]->xadd; } @@ -3700,27 +3864,28 @@ doPsText(struct canvas * const canvasP, static void -doText(struct canvas * const canvasP, - blitList * const blitListP, +doText(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, Word const startx, Word const starty) { if (blitListP) - skipText(blitListP); + skipText(ifP, blitListP); else { if (!(tfont = getFont(text_font, text_size, text_face))) tfont = pbm_defaultfont("bdf"); if (ps_text) - doPsText(canvasP, startx, starty); + doPsText(ifP, canvasP, startx, starty); else { int len; Word x, y; x = startx; y = starty; - for (len = readByte(); len > 0; --len) { - struct glyph* const glyph = tfont->glyph[readByte()]; + for (len = readByte(ifP); len > 0; --len) { + struct glyph* const glyph = tfont->glyph[readByte(ifP)]; if (glyph) { int dy; int h; @@ -3750,15 +3915,16 @@ doText(struct canvas * const canvasP, static drawFn LongText; static void -LongText(struct canvas * const canvasP, - blitList * const blitListP, +LongText(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { struct Point p; - readPoint(&p); + readPoint(ifP, &p); - doText(canvasP, blitListP, p.x, p.y); + doText(ifP, canvasP, blitListP, p.x, p.y); } @@ -3766,13 +3932,14 @@ LongText(struct canvas * const canvasP, static drawFn DHText; static void -DHText(struct canvas * const canvasP, - blitList * const blitListP, +DHText(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - current.x += readByte(); + current.x += readByte(ifP); - doText(canvasP, blitListP, current.x, current.y); + doText(ifP, canvasP, blitListP, current.x, current.y); } @@ -3780,13 +3947,14 @@ DHText(struct canvas * const canvasP, static drawFn DVText; static void -DVText(struct canvas * const canvasP, - blitList * const blitListP, +DVText(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - current.y += readByte(); + current.y += readByte(ifP); - doText(canvasP, blitListP, current.x, current.y); + doText(ifP, canvasP, blitListP, current.x, current.y); } @@ -3794,13 +3962,15 @@ DVText(struct canvas * const canvasP, static drawFn DHDVText; static void -DHDVText(struct canvas * const canvasP, - blitList * const blitListP, +DHDVText(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { + Byte dh, dv; - dh = readByte(); - dv = readByte(); + dh = readByte(ifP); + dv = readByte(ifP); if (verbose) pm_message("dh, dv = %d, %d", dh, dv); @@ -3808,7 +3978,7 @@ DHDVText(struct canvas * const canvasP, current.x += dh; current.y += dv; - doText(canvasP, blitListP, current.x, current.y); + doText(ifP, canvasP, blitListP, current.x, current.y); } @@ -3818,50 +3988,51 @@ DHDVText(struct canvas * const canvasP, */ static void -directBits(struct canvas * const canvasP, - blitList * const blitListP, +directBits(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, unsigned int const pictVersion, bool const skipRegion) { - struct pixMap p; + struct PixMap p; struct Rect srcRect; struct Rect dstRect; - struct raster raster; + struct Raster raster; Word mode; /* skip fake len, and fake EOF */ - skip(4); /* Ptr baseAddr == 0x000000ff */ - readWord(); /* version */ - readRect(&p.Bounds); - p.packType = readWord(); - p.packSize = readLong(); - p.hRes = readLong(); - p.vRes = readLong(); - p.pixelType = readWord(); - p.pixelSize = readWord(); - p.pixelSize = readWord(); /* XXX twice??? */ - p.cmpCount = readWord(); - p.cmpSize = readWord(); - p.planeBytes = readLong(); - p.pmTable = readLong(); - p.pmReserved = readLong(); - - readRect(&srcRect); + skip(ifP, 4); /* Ptr baseAddr == 0x000000ff */ + readWord(ifP); /* version */ + readRect(ifP, &p.Bounds); + p.packType = readWord(ifP); + p.packSize = readLong(ifP); + p.hRes = readLong(ifP); + p.vRes = readLong(ifP); + p.pixelType = readWord(ifP); + p.pixelSize = readWord(ifP); + p.pixelSize = readWord(ifP); /* XXX twice??? */ + p.cmpCount = readWord(ifP); + p.cmpSize = readWord(ifP); + p.planeBytes = readLong(ifP); + p.pmTable = readLong(ifP); + p.pmReserved = readLong(ifP); + + readRect(ifP, &srcRect); if (verbose) dumpRect("source rectangle:", srcRect); - readRect(&dstRect); + readRect(ifP, &dstRect); if (verbose) dumpRect("destination rectangle:", dstRect); - mode = readWord(); + mode = readWord(ifP); if (verbose) - pm_message("transfer mode = %s", const_name(transfer_name, mode)); + pm_message("transfer mode = %s", constName(transfer_name, mode)); if (skipRegion) - skip_poly_or_region(canvasP, blitListP, pictVersion); + skipPolyOrRegion(ifP, canvasP, blitListP, pictVersion); - unpackbits(ifp, &p.Bounds, 0, p.pixelSize, &raster); + unpackbits(ifP, &p.Bounds, 0, p.pixelSize, &raster); blit(srcRect, p.Bounds, raster, canvasP, blitListP, p.pixelSize, dstRect, picFrame, rowlen, NULL, mode); @@ -3877,11 +4048,12 @@ directBits(struct canvas * const canvasP, static drawFn DirectBitsRect; static void -DirectBitsRect(struct canvas * const canvasP, - blitList * const blitListP, +DirectBitsRect(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - directBits(canvasP, blitListP, version, SKIP_REGION_FALSE); + directBits(ifP, canvasP, blitListP, version, SKIP_REGION_FALSE); } @@ -3889,18 +4061,20 @@ DirectBitsRect(struct canvas * const canvasP, static drawFn DirectBitsRgn; static void -DirectBitsRgn(struct canvas * const canvasP, - blitList * const blitListP, +DirectBitsRgn(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { - directBits(canvasP, blitListP, version, SKIP_REGION_TRUE); + directBits(ifP, canvasP, blitListP, version, SKIP_REGION_TRUE); } static void -doPixmap(struct canvas * const canvasP, - blitList * const blitListP, +doPixmap(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version, Word const rowBytes, int const is_region) { @@ -3908,47 +4082,47 @@ doPixmap(struct canvas * const canvasP, Do a paletted image. -----------------------------------------------------------------------------*/ Word mode; - struct pixMap p; - struct raster raster; - struct RGBColor * color_table; + struct PixMap p; + struct Raster raster; + struct RGBColor * colorTable; struct Rect srcRect; struct Rect dstRect; - readPixmap(&p); + readPixmap(ifP, &p); if (verbose) pm_message("%u x %u paletted image", p.Bounds.right - p.Bounds.left, p.Bounds.bottom - p.Bounds.top); - color_table = readColorTable(); + colorTable = readColorTable(ifP); - readRect(&srcRect); + readRect(ifP, &srcRect); if (verbose) dumpRect("source rectangle:", srcRect); - readRect(&dstRect); + readRect(ifP, &dstRect); if (verbose) dumpRect("destination rectangle:", dstRect); - mode = readWord(); + mode = readWord(ifP); if (verbose) - pm_message("transfer mode = %s", const_name(transfer_name, mode)); + pm_message("transfer mode = %s", constName(transfer_name, mode)); if (is_region) - skip_poly_or_region(canvasP, blitListP, version); + skipPolyOrRegion(ifP, canvasP, blitListP, version); stage = "unpacking rectangle"; - unpackbits(ifp, &p.Bounds, rowBytes, p.pixelSize, &raster); + unpackbits(ifP, &p.Bounds, rowBytes, p.pixelSize, &raster); blit(srcRect, p.Bounds, raster, canvasP, blitListP, 8, - dstRect, picFrame, rowlen, color_table, mode); + dstRect, picFrame, rowlen, colorTable, mode); - free(color_table); + free(colorTable); freeRaster(raster); } @@ -3956,8 +4130,8 @@ doPixmap(struct canvas * const canvasP, static void doBitmap(FILE * const ifP, - struct canvas * const canvasP, - blitList * const blitListP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version, int const rowBytes, int const is_region) { @@ -3970,22 +4144,22 @@ doBitmap(FILE * const ifP, struct Rect srcRect; struct Rect dstRect; Word mode; - struct raster raster; + struct Raster raster; /* This raster contains padding on the right to make a multiple of 16 pixels per row. */ static struct RGBColor color_table[] = { {65535L, 65535L, 65535L}, {0, 0, 0} }; - readRect(&Bounds); - readRect(&srcRect); - readRect(&dstRect); - mode = readWord(); + readRect(ifP, &Bounds); + readRect(ifP, &srcRect); + readRect(ifP, &dstRect); + mode = readWord(ifP); if (verbose) - pm_message("transfer mode = %s", const_name(transfer_name, mode)); + pm_message("transfer mode = %s", constName(transfer_name, mode)); if (is_region) - skip_poly_or_region(canvasP, blitListP, version); + skipPolyOrRegion(ifP, canvasP, blitListP, version); stage = "unpacking rectangle"; @@ -4002,8 +4176,9 @@ doBitmap(FILE * const ifP, static drawFn BitsRect; static void -BitsRect(struct canvas * const canvasP, - blitList * const blitListP, +BitsRect(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { Word rowBytesWord; @@ -4011,14 +4186,14 @@ BitsRect(struct canvas * const canvasP, unsigned int rowBytes; stage = "Reading rowBytes word for bitsrect"; - rowBytesWord = readWord(); + rowBytesWord = readWord(ifP); interpretRowBytesWord(rowBytesWord, &pixMap, &rowBytes); if (pixMap) - doPixmap(canvasP, blitListP, version, rowBytes, 0); + doPixmap(ifP, canvasP, blitListP, version, rowBytes, 0); else - doBitmap(ifp, canvasP, blitListP, version, rowBytes, 0); + doBitmap(ifP, canvasP, blitListP, version, rowBytes, 0); } @@ -4026,8 +4201,9 @@ BitsRect(struct canvas * const canvasP, static drawFn BitsRegion; static void -BitsRegion(struct canvas * const canvasP, - blitList * const blitListP, +BitsRegion(FILE * const ifP, + struct Canvas * const canvasP, + BlitList * const blitListP, int const version) { Word rowBytesWord; @@ -4035,14 +4211,14 @@ BitsRegion(struct canvas * const canvasP, unsigned int rowBytes; stage = "Reading rowBytes for bitsregion"; - rowBytesWord = readWord(); + rowBytesWord = readWord(ifP); interpretRowBytesWord(rowBytesWord, &pixMap, &rowBytes); if (pixMap) - doPixmap(canvasP, blitListP, version, rowBytes, 1); + doPixmap(ifP, canvasP, blitListP, version, rowBytes, 1); else - doBitmap(ifp, canvasP, blitListP, version, rowBytes, 1); + doBitmap(ifP, canvasP, blitListP, version, rowBytes, 1); } @@ -4051,7 +4227,7 @@ BitsRegion(struct canvas * const canvasP, * See http://developer.apple.com/techpubs/mac/QuickDraw/QuickDraw-461.html * for opcode description */ -static struct opdef const optable[] = { +static struct Opdef const optable[] = { /* 0x00 */ { "NOP", 0, NULL, "nop" }, /* 0x01 */ { "ClipRgn", NA, ClipRgn, "clip region" }, /* 0x02 */ { "BkPat", 8, BkPat, "background pattern" }, @@ -4167,14 +4343,14 @@ static struct opdef const optable[] = { /* 0x6d */ RESERVED_OP(4), /* 0x6e */ RESERVED_OP(4), /* 0x6f */ RESERVED_OP(4), -/* 0x70 */ { "framePoly", NA, skip_poly_or_region, "poly" }, +/* 0x70 */ { "framePoly", NA, skipPolyOrRegion, "poly" }, /* 0x71 */ { "paintPoly", NA, paintPoly, "poly" }, -/* 0x72 */ { "erasePoly", NA, skip_poly_or_region, "poly" }, -/* 0x73 */ { "invertPoly", NA, skip_poly_or_region, "poly" }, -/* 0x74 */ { "fillPoly", NA, skip_poly_or_region, "poly" }, -/* 0x75 */ RESERVED_OP_F(skip_poly_or_region), -/* 0x76 */ RESERVED_OP_F(skip_poly_or_region), -/* 0x77 */ RESERVED_OP_F(skip_poly_or_region), +/* 0x72 */ { "erasePoly", NA, skipPolyOrRegion, "poly" }, +/* 0x73 */ { "invertPoly", NA, skipPolyOrRegion, "poly" }, +/* 0x74 */ { "fillPoly", NA, skipPolyOrRegion, "poly" }, +/* 0x75 */ RESERVED_OP_F(skipPolyOrRegion), +/* 0x76 */ RESERVED_OP_F(skipPolyOrRegion), +/* 0x77 */ RESERVED_OP_F(skipPolyOrRegion), /* 0x78 */ { "frameSamePoly", 0, NULL, "poly (NYI)" }, /* 0x79 */ { "paintSamePoly", 0, NULL, "poly (NYI)" }, /* 0x7a */ { "eraseSamePoly", 0, NULL, "poly (NYI)" }, @@ -4183,14 +4359,14 @@ static struct opdef const optable[] = { /* 0x7d */ RESERVED_OP(0), /* 0x7e */ RESERVED_OP(0), /* 0x7f */ RESERVED_OP(0), -/* 0x80 */ { "frameRgn", NA, skip_poly_or_region, "region" }, -/* 0x81 */ { "paintRgn", NA, skip_poly_or_region, "region" }, -/* 0x82 */ { "eraseRgn", NA, skip_poly_or_region, "region" }, -/* 0x83 */ { "invertRgn", NA, skip_poly_or_region, "region" }, -/* 0x84 */ { "fillRgn", NA, skip_poly_or_region, "region" }, -/* 0x85 */ RESERVED_OP_F(skip_poly_or_region), -/* 0x86 */ RESERVED_OP_F(skip_poly_or_region), -/* 0x87 */ RESERVED_OP_F(skip_poly_or_region), +/* 0x80 */ { "frameRgn", NA, skipPolyOrRegion, "region" }, +/* 0x81 */ { "paintRgn", NA, skipPolyOrRegion, "region" }, +/* 0x82 */ { "eraseRgn", NA, skipPolyOrRegion, "region" }, +/* 0x83 */ { "invertRgn", NA, skipPolyOrRegion, "region" }, +/* 0x84 */ { "fillRgn", NA, skipPolyOrRegion, "region" }, +/* 0x85 */ RESERVED_OP_F(skipPolyOrRegion), +/* 0x86 */ RESERVED_OP_F(skipPolyOrRegion), +/* 0x87 */ RESERVED_OP_F(skipPolyOrRegion), /* 0x88 */ { "frameSameRgn", 0, NULL, "region (NYI)" }, /* 0x89 */ { "paintSameRgn", 0, NULL, "region (NYI)" }, /* 0x8a */ { "eraseSameRgn", 0, NULL, "region (NYI)" }, @@ -4225,9 +4401,10 @@ static struct opdef const optable[] = { static void -processOpcode(Word const opcode, - struct canvas * const canvasP, - blitList * const blitListP, +processOpcode(FILE * const ifP, + Word const opcode, + struct Canvas * const canvasP, + BlitList * const blitListP, unsigned int const version) { if (opcode < 0xa2) { @@ -4240,15 +4417,15 @@ processOpcode(Word const opcode, } if (optable[opcode].impl != NULL) - (*optable[opcode].impl)(canvasP, blitListP, version); + (*optable[opcode].impl)(ifP, canvasP, blitListP, version); else if (optable[opcode].len >= 0) - skip(optable[opcode].len); + skip(ifP, optable[opcode].len); else { /* It's a special length code */ switch (optable[opcode].len) { case WORD_LEN: { - Word const len = readWord(); - skip(len); + Word const len = readWord(ifP); + skip(ifP, len); } break; default: pm_error("can't do length %d", optable[opcode].len); @@ -4258,12 +4435,12 @@ processOpcode(Word const opcode, if (verbose) pm_message("HeaderOp"); stage = "HeaderOp"; - skip(24); + skip(ifP, 24); } else if (opcode >= 0xa2 && opcode <= 0xaf) { stage = "skipping reserved"; if (verbose) pm_message("%s 0x%x", stage, opcode); - skip(readWord()); + skip(ifP, readWord(ifP)); } else if (opcode >= 0xb0 && opcode <= 0xcf) { /* just a reserved opcode, no data */ if (verbose) @@ -4272,12 +4449,12 @@ processOpcode(Word const opcode, stage = "skipping reserved"; if (verbose) pm_message("%s 0x%x", stage, opcode); - skip(readLong()); + skip(ifP, readLong(ifP)); } else if (opcode >= 0x100 && opcode <= 0x7fff) { stage = "skipping reserved"; if (verbose) pm_message("%s 0x%x", stage, opcode); - skip((opcode >> 7) & 255); + skip(ifP, (opcode >> 7) & 255); } else if (opcode >= 0x8000 && opcode <= 0x80ff) { /* just a reserved opcode, no data */ if (verbose) @@ -4286,7 +4463,7 @@ processOpcode(Word const opcode, stage = "skipping reserved"; if (verbose) pm_message("%s 0x%x", stage, opcode); - skip(readLong()); + skip(ifP, readLong(ifP)); } else pm_error("This program does not understand opcode 0x%04x", opcode); } @@ -4294,15 +4471,23 @@ processOpcode(Word const opcode, static void -interpretPict(FILE * const ofP) { +interpretPict(FILE * const ifP, + FILE * const ofP, + bool const noheader, + bool const fullres, + bool const quickdraw, + unsigned int const verboseArg) { Byte ch; Word picSize; Word opcode; unsigned int version; unsigned int i; - struct canvas canvas; - blitList blitList; + struct Canvas canvas; + BlitList blitList; + + verbose = verboseArg; + recognize_comment = !quickdraw; initBlitList(&blitList); @@ -4310,18 +4495,24 @@ interpretPict(FILE * const ofP) { pen_pat.pix[i] = bkpat.pix[i] = fillpat.pix[i] = 1; pen_width = pen_height = 1; pen_mode = 0; /* srcCopy */ - pen_trf = transfer(pen_mode); + pen_trf = transferFunctionForMode(pen_mode); text_mode = 0; /* srcCopy */ - text_trf = transfer(text_mode); + text_trf = transferFunctionForMode(text_mode); + + if (!noheader) { + stage = "Reading 512 byte header"; + /* Note that the "header" in PICT is entirely comment! */ + skip(ifP, 512); + } stage = "Reading picture size"; - picSize = readWord(); + picSize = readWord(ifP); if (verbose) pm_message("picture size = %u (0x%x)", picSize, picSize); stage = "reading picture frame"; - readRect(&picFrame); + readRect(ifP, &picFrame); if (verbose) { dumpRect("Picture frame:", picFrame); @@ -4339,18 +4530,18 @@ interpretPict(FILE * const ofP) { clip_rect = picFrame; } - while ((ch = readByte()) == 0) + while ((ch = readByte(ifP)) == 0) ; if (ch != 0x11) pm_error("No version number"); - version = readByte(); + version = readByte(ifP); switch (version) { case 1: break; case 2: { - unsigned char const subcode = readByte(); + unsigned char const subcode = readByte(ifP); if (subcode != 0xff) pm_error("The only Version 2 PICT images this program " "undertands are subcode 0xff. This image has " @@ -4363,8 +4554,9 @@ interpretPict(FILE * const ofP) { if (verbose) pm_message("PICT version %u", version); - while((opcode = nextOp(version)) != 0xff) - processOpcode(opcode, &canvas, fullres ? &blitList : NULL, version); + while((opcode = nextOp(ifP, version)) != 0xff) + processOpcode(ifP, opcode, &canvas, fullres ? &blitList : NULL, + version); if (fullres) { if (blitList.unblittableText) @@ -4379,77 +4571,25 @@ interpretPict(FILE * const ofP) { -static void -loadDefaultFontDir(void) { -/*---------------------------------------------------------------------------- - Load the fonts from the font directory file "fontdir" (in the current - directory), if it exists. ------------------------------------------------------------------------------*/ - struct stat statbuf; - int rc; - - rc = stat("fontdir", &statbuf); - - if (rc == 0) - load_fontdir("fontdir"); -} +int +main(int argc, const char *argv[]) { + struct CmdlineInfo cmdline; + FILE * ifP; + pm_proginit(&argc, argv); -int -main(int argc, char * argv[]) { - int argn; - int header; - const char* const usage = -"[-verbose] [-fullres] [-noheader] [-quickdraw] [-fontdir file] [pictfile]"; - - ppm_init( &argc, argv ); - - argn = 1; - verbose = 0; - fullres = 0; - header = 1; - recognize_comment = 1; - - while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0') { - if (pm_keymatch(argv[argn], "-verbose", 2)) - verbose++; - else if (pm_keymatch(argv[argn], "-fullres", 3)) - fullres = 1; - else if (pm_keymatch(argv[argn], "-noheader", 2)) - header = 0; - else if (pm_keymatch(argv[argn], "-quickdraw", 2)) - recognize_comment = 0; - else if (pm_keymatch(argv[argn], "-fontdir", 3)) { - argn++; - if (!argv[argn]) - pm_usage(usage); - else - load_fontdir(argv[argn]); - } - else - pm_usage(usage); - ++argn; - } + parseCommandLine(argc, argv, &cmdline); - if (argn < argc) { - ifp = pm_openr(argv[argn]); - ++argn; - } else - ifp = stdin; + ifP = pm_openr(cmdline.inputFileName); - if (argn != argc) - pm_usage(usage); + if (cmdline.fontdir) + loadFontdir(cmdline.fontdir); loadDefaultFontDir(); - if (header) { - stage = "Reading 512 byte header"; - /* Note that the "header" in PICT is entirely comment! */ - skip(512); - } - - interpretPict(stdout); + interpretPict(ifP, stdout, cmdline.noheader, + cmdline.fullres, cmdline.quickdraw, cmdline.verbose); pm_close(stdout); diff --git a/converter/ppm/ppmtoapplevol.c b/converter/ppm/ppmtoapplevol.c index eb4b6b2a..1e979de9 100644 --- a/converter/ppm/ppmtoapplevol.c +++ b/converter/ppm/ppmtoapplevol.c @@ -78,7 +78,7 @@ main (int argc, const char * argv[]) { for (row = 0; row < rows; row++) { unsigned int col; - + ppm_readppmrow(ifP, pixelrow, cols, maxval, format); for (col = 0; col < cols; ++col) { diff --git a/converter/ppm/ppmtoilbm.c b/converter/ppm/ppmtoilbm.c index c5fa4d00..ad07f9fb 100644 --- a/converter/ppm/ppmtoilbm.c +++ b/converter/ppm/ppmtoilbm.c @@ -73,7 +73,6 @@ #include "pm_c_util.h" #include "mallocvar.h" #include "ppm.h" -#include "ppmfloyd.h" #include "pbm.h" #include "runlength.h" #include "ilbm.h" @@ -118,13 +117,13 @@ static unsigned char *coded_rowbuf; /* buffer for uncompressed scanline */ static unsigned char *compr_rowbuf; /* buffer for compressed scanline */ static pixel **pixels; /* PPM image (NULL for row-by-row operation) */ -static pixel *pixrow; +static pixel *pixrow; /* current row in PPM image (pointer into pixels array, or buffer - for row-by-row operation) + for row-by-row operation) */ static long viewportmodes = 0; -static int slicesize = 1; +static int slicesize = 1; /* rows per slice for multipalette images - NOT USED */ static unsigned char compmethod = DEF_COMPRESSION; /* default compression */ @@ -155,9 +154,8 @@ static bodyblock *cur_block = &firstblock; static char *anno_chunk, *auth_chunk, *name_chunk, *text_chunk, *copyr_chunk; /* flags */ -static short compr_force = 0; +static short compr_force = 0; /* force compressed output, even if the image got larger - NOT USED */ -static short floyd = 0; /* apply floyd-steinberg error diffusion */ static short gen_camg = 0; /* write CAMG chunk */ #define WORSTCOMPR(bytes) ((bytes) + (bytes)/128 + 1) @@ -360,7 +358,7 @@ writeCmap(pixel * const colormap, pm_writebiglong(stdout, cmapsize); if( maxval != MAXCOLVAL ) { int *table; - pm_message("maxval is not %d - automatically rescaling colors", + pm_message("maxval is not %d - automatically rescaling colors", MAXCOLVAL); table = makeValTable(maxval, MAXCOLVAL); for( i = 0; i < colors; i++ ) { @@ -457,7 +455,7 @@ compressRow(unsigned int const bytes) { &compressedByteCt); break; default: - pm_error("compressRow(): unknown compression method %d", + pm_error("compressRow(): unknown compression method %d", compmethod); } storeBodyrow(compr_rowbuf, compressedByteCt); @@ -551,35 +549,35 @@ reportTooManyColors(int const ifmode, int const hamplanes, DirectColor const dcol, int const deepbits) { - + int const maxcolors = 1 << maxplanes; switch( ifmode ) { case MODE_HAM: pm_message("too many colors for %d planes - " - "proceeding to write a HAM%d file", + "proceeding to write a HAM%d file", maxplanes, hamplanes); - pm_message("if you want a non-HAM file, try doing a 'pnmquant %d'", + pm_message("if you want a non-HAM file, try doing a 'pnmquant %d'", maxcolors); break; case MODE_DCOL: pm_message("too many colors for %d planes - " - "proceeding to write a %d:%d:%d direct color ILBM", + "proceeding to write a %d:%d:%d direct color ILBM", maxplanes, dcol.r, dcol.g, dcol.b); pm_message("if you want a non-direct color file, " "try doing a 'pnmquant %d'", maxcolors); break; case MODE_DEEP: pm_message("too many colors for %d planes - " - "proceeding to write a %d-bit \'deep\' ILBM", + "proceeding to write a %d-bit \'deep\' ILBM", maxplanes, deepbits*3); pm_message("if you want a non-deep file, " - "try doing a 'pnmquant %d'", + "try doing a 'pnmquant %d'", maxcolors); break; default: pm_error("too many colors for %d planes - " - "try doing a 'pnmquant %d'", + "try doing a 'pnmquant %d'", maxplanes, maxcolors); break; } @@ -599,7 +597,7 @@ getIntVal(const char * const string, pm_error("option \"%s\" needs integer argument", option); if (val < bot || val > top) - pm_error("option \"%s\" argument value out of range (%d..%d)", + pm_error("option \"%s\" argument value out of range (%d..%d)", option, bot, top); return val; @@ -613,10 +611,10 @@ getComprMethod(const char * const string) { int retval; if( pm_keymatch(string, "none", 1) || pm_keymatch(string, "0", 1) ) retval = cmpNone; - else if( pm_keymatch(string, "byterun1", 1) || + else if( pm_keymatch(string, "byterun1", 1) || pm_keymatch(string, "1", 1) ) retval = cmpByteRun1; - else + else pm_error("unknown compression method: %s", string); return retval; } @@ -631,12 +629,12 @@ getMaskType(const char * const string) { if( pm_keymatch(string, "none", 1) || pm_keymatch(string, "0", 1) ) retval = mskNone; else - if( pm_keymatch(string, "plane", 1) || + if( pm_keymatch(string, "plane", 1) || pm_keymatch(string, "maskplane", 1) || pm_keymatch(string, "1", 1) ) retval = mskHasMask; else - if( pm_keymatch(string, "transparentcolor", 1) || + if( pm_keymatch(string, "transparentcolor", 1) || pm_keymatch(string, "2", 1) ) retval = mskHasTransparentColor; else @@ -665,7 +663,7 @@ getHammapMode(const char * const string) { else if( pm_keymatch(string, "rgb5", 4) ) retval = HAMMODE_RGB5; - else + else pm_error("unknown HAM colormap selection mode: %s", string); return retval; } @@ -726,7 +724,7 @@ hcmp(const void * const a, const hentry * const vaP = a; const hentry * const vbP = b; - return(vbP->count - vaP->count); + return(vbP->count - vaP->count); } @@ -801,17 +799,17 @@ computeHamCmap(int const cols, if( dist <= maxdist ) { unsigned int sum = hmap[i].count + hmap[col].count; - hmap[i].r = ROUNDDIV(hmap[i].r * hmap[i].count + + hmap[i].r = ROUNDDIV(hmap[i].r * hmap[i].count + r * hmap[col].count, sum); - hmap[i].g = ROUNDDIV(hmap[i].g * hmap[i].count + + hmap[i].g = ROUNDDIV(hmap[i].g * hmap[i].count + g * hmap[col].count, sum); - hmap[i].b = ROUNDDIV(hmap[i].b * hmap[i].count + + hmap[i].b = ROUNDDIV(hmap[i].b * hmap[i].count + b * hmap[col].count, sum); hmap[i].count = sum; hmap[col] = hmap[i]; /* temp store */ - for( tmp = i-1; - tmp >= 0 && hmap[tmp].count < hmap[col].count; + for( tmp = i-1; + tmp >= 0 && hmap[tmp].count < hmap[col].count; tmp-- ) hmap[tmp+1] = hmap[tmp]; hmap[tmp+1] = hmap[col]; @@ -878,7 +876,6 @@ doHamBody(FILE * const ifP, int col, row, i; rawtype *raw_rowbuf; - ppm_fs_info *fi = NULL; colorhash_table cht, cht2; long bodysize = 0; int *itoh; /* table image -> ham */ @@ -898,9 +895,6 @@ doHamBody(FILE * const ifP, itoh = makeValTable(maxval, hammaxval); - if( floyd ) - fi = ppm_fs_init(cols, maxval, 0); - for( row = 0; row < rows; row++ ) { int noprev; int spr, spg, spb; /* scaled values of previous pixel */ @@ -909,9 +903,7 @@ doHamBody(FILE * const ifP, noprev = 1; prow = nextPixrow(ifP, row); - for( col = ppm_fs_startrow(fi, prow); - col < cols; - col = ppm_fs_next(fi, col) ) { + for( col = 0; col < cols; ++col ) { pixel const p = prow[col]; @@ -930,15 +922,15 @@ doHamBody(FILE * const ifP, int ucr, ucg, ucb; /* unscaled values of colormap entry */ if( hammapmode == HAMMODE_GRAY ) { - if( maxval <= 255 ) - /* Use fast approximation to + if( maxval <= 255 ) + /* Use fast approximation to 0.299 r + 0.587 g + 0.114 b. */ i = (int)ppm_fastlumin(p); - else - /* Can't use fast approximation, - so fall back on floats. + else + /* Can't use fast approximation, + so fall back on floats. */ - i = (int)(PPM_LUMIN(p) + 0.5); + i = (int)(PPM_LUMIN(p) + 0.5); /* -IUW added '+ 0.5' */ i = itoh[i]; } @@ -956,8 +948,8 @@ doHamBody(FILE * const ifP, } } } - ucr = PPM_GETR(colormap[i]); - ucg = PPM_GETG(colormap[i]); + ucr = PPM_GETR(colormap[i]); + ucg = PPM_GETG(colormap[i]); ucb = PPM_GETB(colormap[i]); if( noprev ) { /* no previous pixel, must use colormap */ @@ -985,21 +977,21 @@ doHamBody(FILE * const ifP, (sb - scb)*(sb - scb); #endif - if( di <= dr && di <= dg && di <= db ) { + if( di <= dr && di <= dg && di <= db ) { /* prefer colormap lookup */ - raw_rowbuf[col] = i; + raw_rowbuf[col] = i; upr = ucr; upg = ucg; upb = ucb; spr = scr; spg = scg; spb = scb; } else if( db <= dr && db <= dg ) { - raw_rowbuf[col] = sb + hamcode_blue; + raw_rowbuf[col] = sb + hamcode_blue; spb = sb; upb = ub; } else if( dr <= dg ) { - raw_rowbuf[col] = sr + hamcode_red; + raw_rowbuf[col] = sr + hamcode_red; spr = sr; upr = ur; } @@ -1012,19 +1004,17 @@ doHamBody(FILE * const ifP, } else { /* prefect match in cmap */ raw_rowbuf[col] = i; /* + (HAMCODE_CMAP << colbits) */ - upr = PPM_GETR(colormap[i]); - upg = PPM_GETG(colormap[i]); + upr = PPM_GETR(colormap[i]); + upg = PPM_GETG(colormap[i]); upb = PPM_GETB(colormap[i]); - spr = itoh[upr]; - spg = itoh[upg]; + spr = itoh[upr]; + spg = itoh[upg]; spb = itoh[upb]; } - ppm_fs_update3(fi, col, upr, upg, upb); } bodysize += encodeRow(ofP, raw_rowbuf, cols, nPlanes); if( maskmethod == mskHasMask ) bodysize += encodeMaskrow(ofP, raw_rowbuf, cols); - ppm_fs_endrow(fi); } if( ofP && ODD(bodysize) ) putByte(0); @@ -1033,7 +1023,6 @@ doHamBody(FILE * const ifP, /* clean up */ free(raw_rowbuf); - ppm_fs_free(fi); return bodysize; } @@ -1113,12 +1102,12 @@ ppmToHam(FILE * const ifP, } break; case HAMMODE_RGB4: - colormap = computeHamCmap(cols, rows, maxval, hamcolors, + colormap = computeHamCmap(cols, rows, maxval, hamcolors, &colors, 4); cmapmaxval = 15; break; case HAMMODE_RGB5: - colormap = computeHamCmap(cols, rows, maxval, + colormap = computeHamCmap(cols, rows, maxval, hamcolors, &colors, 5); cmapmaxval = 31; break; @@ -1129,7 +1118,7 @@ ppmToHam(FILE * const ifP, else { hammapmode = HAMMODE_MAPFILE; if( colors > hamcolors ) { - pm_message("colormap too large - using first %d colors", + pm_message("colormap too large - using first %d colors", hamcolors); colors = hamcolors; } @@ -1142,9 +1131,9 @@ ppmToHam(FILE * const ifP, newcmap = ppm_allocrow(colors); table = makeValTable(cmapmaxval, maxval); for( i = 0; i < colors; i++ ) - PPM_ASSIGN(newcmap[i], - table[PPM_GETR(colormap[i])], - table[PPM_GETG(colormap[i])], + PPM_ASSIGN(newcmap[i], + table[PPM_GETR(colormap[i])], + table[PPM_GETG(colormap[i])], table[PPM_GETB(colormap[i])]); free(table); ppm_freerow(colormap); @@ -1158,16 +1147,16 @@ ppmToHam(FILE * const ifP, bodysize = oldsize = rows * TOTALPLANES(nPlanes) * RowBytes(cols); if( DO_COMPRESS ) { - bodysize = doHamBody(ifP, NULL, cols, rows, maxval, + bodysize = doHamBody(ifP, NULL, cols, rows, maxval, hammaxval, nPlanes, colormap, colors); - /*bodysize = doHamBody(ifP, NULL, cols, + /*bodysize = doHamBody(ifP, NULL, cols, rows, maxval, hammaxval, nPlanes, colbits, nocolor);*/ if( bodysize > oldsize ) pm_message("warning - %s compression increases BODY size " - "by %ld%%", + "by %ld%%", cmpNAME[compmethod], 100*(bodysize-oldsize)/oldsize); else - pm_message("BODY compression (%s): %ld%%", + pm_message("BODY compression (%s): %ld%%", cmpNAME[compmethod], 100*(oldsize-bodysize)/oldsize); } @@ -1195,7 +1184,7 @@ ppmToHam(FILE * const ifP, if( DO_COMPRESS ) writeBodyRows(); else - doHamBody(ifP, stdout, cols, rows, maxval, hammaxval, + doHamBody(ifP, stdout, cols, rows, maxval, hammaxval, nPlanes, colormap, colors); } @@ -1210,7 +1199,7 @@ doDeepBody(FILE * const ifP, FILE * const ofP, int const cols, int const rows, - pixval const maxval, + pixval const maxval, int const bitspercolor) { int row, col; @@ -1226,7 +1215,7 @@ doDeepBody(FILE * const ifP, newmaxval = pm_bitstomaxval(bitspercolor); if( maxval != newmaxval ) { - pm_message("maxval is not %d - automatically rescaling colors", + pm_message("maxval is not %d - automatically rescaling colors", newmaxval); table = makeValTable(maxval, newmaxval); } @@ -1294,7 +1283,7 @@ ppmToDeep(FILE * const ifP, pm_message("warning - %s compression increases BODY size by %ld%%", cmpNAME[compmethod], 100*(bodysize-oldsize)/oldsize); else - pm_message("BODY compression (%s): %ld%%", + pm_message("BODY compression (%s): %ld%%", cmpNAME[compmethod], 100*(oldsize-bodysize)/oldsize); } @@ -1335,7 +1324,7 @@ doDcolBody(FILE * const ifP, FILE * const ofP, int const cols, int const rows, - pixval const maxval, + pixval const maxval, DirectColor * const dcol) { int row, col; @@ -1405,10 +1394,10 @@ ppmToDcol(FILE * const ifP, bodysize = doDcolBody(ifP, NULL, cols, rows, maxval, dcol); if( bodysize > oldsize ) pm_message("warning - %s compression increases BODY size by %ld%%", - cmpNAME[compmethod], + cmpNAME[compmethod], 100*(bodysize-oldsize)/oldsize); else - pm_message("BODY compression (%s): %ld%%", cmpNAME[compmethod], + pm_message("BODY compression (%s): %ld%%", cmpNAME[compmethod], 100*(oldsize-bodysize)/oldsize); } @@ -1467,35 +1456,30 @@ doStdBody(FILE * const ifP, int row, col, i; pixel *pP; rawtype *raw_rowbuf; - ppm_fs_info *fi = NULL; long bodysize = 0; int usehash = 1; colorhash_table cht; MALLOCARRAY_NOFAIL(raw_rowbuf, cols); cht = ppm_colorrowtocolorhash(colormap, colors); - if( floyd ) - fi = ppm_fs_init(cols, maxval, FS_ALTERNATE); for( row = 0; row < rows; row++ ) { pixel *prow; prow = nextPixrow(ifP, row); - for( col = ppm_fs_startrow(fi, prow); - col < cols; - col = ppm_fs_next(fi, col) ) { + for( col = 0; col < cols; ++col ) { pP = &prow[col]; - if( maskmethod == mskHasTransparentColor && + if( maskmethod == mskHasTransparentColor && maskrow[col] == PBM_WHITE ) i = transpIndex; else { /* Check hash table to see if we have already matched - this color. + this color. */ i = ppm_lookupcolor(cht, pP); if( i == -1 ) { - i = ppm_findclosestcolor(colormap, colors, pP); + i = ppm_findclosestcolor(colormap, colors, pP); /* No; search colormap for closest match. */ if( usehash ) { if( ppm_addtocolorhash(cht, pP, i) < 0 ) { @@ -1507,12 +1491,10 @@ doStdBody(FILE * const ifP, } } raw_rowbuf[col] = i; - ppm_fs_update(fi, col, &colormap[i]); } bodysize += encodeRow(ofP, raw_rowbuf, cols, nPlanes); if( maskmethod == mskHasMask ) bodysize += encodeMaskrow(ofP, raw_rowbuf, cols); - ppm_fs_endrow(fi); } if( ofP && ODD(bodysize) ) putByte(0); @@ -1520,7 +1502,6 @@ doStdBody(FILE * const ifP, /* clean up */ ppm_freecolorhash(cht); free(raw_rowbuf); - ppm_fs_free(fi); return bodysize; } @@ -1534,7 +1515,7 @@ ppmToStd(FILE * const ifP, int const maxval, pixel * const colormapArg, int const colorsArg, - int const cmapmaxvalArg, + int const cmapmaxvalArg, int const maxcolors, int const nPlanes) { @@ -1550,7 +1531,7 @@ ppmToStd(FILE * const ifP, if( maskmethod == mskHasTransparentColor ) { if( transpColor ) { - transpIndex = + transpIndex = ppm_addtocolorrow(colormap, &colors, maxcolors, transpColor); } else @@ -1572,9 +1553,9 @@ ppmToStd(FILE * const ifP, newcmap = ppm_allocrow(colors); table = makeValTable(cmapmaxval, maxval); for (i = 0; i < colors; ++i) - PPM_ASSIGN(newcmap[i], - table[PPM_GETR(colormap[i])], - table[PPM_GETG(colormap[i])], + PPM_ASSIGN(newcmap[i], + table[PPM_GETR(colormap[i])], + table[PPM_GETG(colormap[i])], table[PPM_GETB(colormap[i])]); free(table); colormap = newcmap; @@ -1584,13 +1565,13 @@ ppmToStd(FILE * const ifP, bodysize = oldsize = rows * TOTALPLANES(nPlanes) * RowBytes(cols); if( DO_COMPRESS ) { - bodysize = doStdBody(ifP, NULL, cols, rows, maxval, colormap, + bodysize = doStdBody(ifP, NULL, cols, rows, maxval, colormap, colors, nPlanes); if( bodysize > oldsize ) pm_message("warning - %s compression increases BODY size by %ld%%", cmpNAME[compmethod], 100*(bodysize-oldsize)/oldsize); else - pm_message("BODY compression (%s): %ld%%", + pm_message("BODY compression (%s): %ld%%", cmpNAME[compmethod], 100*(oldsize-bodysize)/oldsize); } @@ -1659,7 +1640,7 @@ ppmToRgb8(FILE * const ifP, for( col1 = 0; col1 < cols; col1 = col2 ) { col2 = col1 + 1; if( maskrow ) { - while( col2 < cols && PPM_EQUAL(pP[col1], pP[col2]) && + while( col2 < cols && PPM_EQUAL(pP[col1], pP[col2]) && maskrow[col1] == maskrow[col2] ) col2++; } @@ -1750,7 +1731,7 @@ ppmToRgbn(FILE * const ifP, for( col1 = 0; col1 < cols; col1 = col2 ) { col2 = col1 + 1; if( maskrow ) { - while( col2 < cols && PPM_EQUAL(pP[col1], pP[col2]) && + while( col2 < cols && PPM_EQUAL(pP[col1], pP[col2]) && maskrow[col1] == maskrow[col2] ) col2++; } @@ -1782,14 +1763,14 @@ ppmToRgbn(FILE * const ifP, else { ++compr_len; /* 3 bit repeat count = 0 */ if( count <= 255 ) - compr_row[compr_len++] = (unsigned char)count; + compr_row[compr_len++] = (unsigned char)count; /* byte repeat count */ else { - compr_row[compr_len++] = (unsigned char)0; + compr_row[compr_len++] = (unsigned char)0; /* byte repeat count = 0 */ - compr_row[compr_len++] = (count >> 8) & 0xff; + compr_row[compr_len++] = (count >> 8) & 0xff; /* word repeat count MSB */ - compr_row[compr_len++] = count & 0xff; + compr_row[compr_len++] = count & 0xff; /* word repeat count LSB */ } } @@ -1878,6 +1859,7 @@ main(int argc, char ** argv) { pixval cmapmaxval; /* maxval of colors in cmap */ const char * mapfile; const char * transpname; + unsigned int defunctArgs = 0; ppm_init(&argc, argv); @@ -1902,7 +1884,7 @@ main(int argc, char ** argv) { if( pm_keymatch(argv[argn], "-rgbn", 5) ) forcemode = MODE_RGBN; else - if( pm_keymatch(argv[argn], "-maxplanes", 4) || + if( pm_keymatch(argv[argn], "-maxplanes", 4) || pm_keymatch(argv[argn], "-mp", 3) ) { if( ++argn >= argc ) pm_error("-maxplanes requires a value"); @@ -1910,7 +1892,7 @@ main(int argc, char ** argv) { fixplanes = 0; } else - if( pm_keymatch(argv[argn], "-fixplanes", 4) || + if( pm_keymatch(argv[argn], "-fixplanes", 4) || pm_keymatch(argv[argn], "-fp", 3) ) { if( ++argn >= argc ) pm_error("-fixplanes requires a value"); @@ -1935,7 +1917,7 @@ main(int argc, char ** argv) { break; default: pm_error("This program does not know how to handle " - "masking method '%s'", + "masking method '%s'", mskNAME[maskmethod]); } } @@ -2014,7 +1996,7 @@ main(int argc, char ** argv) { if( pm_keymatch(argv[argn], "-hambits", 5) ) { if( ++argn >= argc ) pm_usage("-hambits requires a value"); - hamplanes = + hamplanes = getIntVal(argv[argn], argv[argn-1], 3, HAMMAXPLANES-2) +2; } else @@ -2118,7 +2100,7 @@ main(int argc, char ** argv) { forcemode = MODE_NONE; } else - if( pm_keymatch(argv[argn], "-dcbits", 4) || + if( pm_keymatch(argv[argn], "-dcbits", 4) || pm_keymatch(argv[argn], "-dcplanes", 4) ) { if( argc - argn < 4 ) pm_error("-dcbits requires 4 arguments"); @@ -2154,13 +2136,9 @@ main(int argc, char ** argv) { compmethod = getComprMethod(argv[argn]); } else - if( pm_keymatch(argv[argn], "-floyd", 3) || - pm_keymatch(argv[argn], "-fs", 3) ) - floyd = 1; - else - if( pm_keymatch(argv[argn], "-nofloyd", 5) || + if( pm_keymatch(argv[argn], "-nofloyd", 5) || pm_keymatch(argv[argn], "-nofs", 5) ) - floyd = 0; + defunctArgs++; else if( pm_keymatch(argv[argn], "-annotation", 3) ) { if( ++argn >= argc ) @@ -2230,10 +2208,10 @@ main(int argc, char ** argv) { case MODE_CMAP: /* Figure out the colormap. */ pm_message("computing colormap..."); - colormap = ppm_mapfiletocolorrow(ifP, MAXCOLORS, &colors, + colormap = ppm_mapfiletocolorrow(ifP, MAXCOLORS, &colors, &cmapmaxval); if (colormap == NULL) - pm_error("too many colors - try doing a 'pnmquant %d'", + pm_error("too many colors - try doing a 'pnmquant %d'", MAXCOLORS); pm_message("%d colors found", colors); break; @@ -2241,11 +2219,11 @@ main(int argc, char ** argv) { if (mapfile) initRead(ifP, &cols, &rows, &maxval, &format, 0); else { - initRead(ifP, &cols, &rows, &maxval, &format, 1); + initRead(ifP, &cols, &rows, &maxval, &format, 1); /* read file into memory */ pm_message("computing colormap..."); - colormap = - ppm_computecolorrow(pixels, cols, rows, MAXCOLORS, + colormap = + ppm_computecolorrow(pixels, cols, rows, MAXCOLORS, &colors); if (colormap) { cmapmaxval = maxval; @@ -2266,7 +2244,7 @@ main(int argc, char ** argv) { pm_message("reading colormap file..."); mapfp = pm_openr(mapfile); - colormap = ppm_mapfiletocolorrow(mapfp, MAXCOLORS, &colors, + colormap = ppm_mapfiletocolorrow(mapfp, MAXCOLORS, &colors, &cmapmaxval); pm_close(mapfp); if (colormap == NULL) @@ -2302,11 +2280,11 @@ main(int argc, char ** argv) { if (DO_COMPRESS) pm_rlenc_allocoutbuf(&compr_rowbuf, RowBytes(cols), PM_RLE_PACKBITS); } - + switch (mode) { case MODE_HAM: viewportmodes |= vmHAM; - ppmToHam(ifP, cols, rows, maxval, + ppmToHam(ifP, cols, rows, maxval, colormap, colors, cmapmaxval, hamplanes); break; case MODE_DEEP: @@ -2325,9 +2303,7 @@ main(int argc, char ** argv) { ppmToCmap(colormap, colors, cmapmaxval); break; default: - if (mapfile == NULL) - floyd = 0; /* would only slow down conversion */ - ppmToStd(ifP, cols, rows, maxval, colormap, colors, + ppmToStd(ifP, cols, rows, maxval, colormap, colors, cmapmaxval, MAXCOLORS, nPlanes); break; } |