diff options
Diffstat (limited to 'converter/other')
-rw-r--r-- | converter/other/ipdb.c | 205 | ||||
-rw-r--r-- | converter/other/ipdb.h | 20 | ||||
-rw-r--r-- | converter/other/pamtopdbimg.c | 59 | ||||
-rw-r--r-- | converter/other/pdbimgtopam.c | 104 |
4 files changed, 193 insertions, 195 deletions
diff --git a/converter/other/ipdb.c b/converter/other/ipdb.c index 797c7d47..477a9264 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,114 +68,108 @@ 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) { + return &imgP->data[(row) * imgP->width / imgPpb(imgP)]; +} - #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.", +static const char * const errorDesc[] = { + /* E_BADCOLORS */ + "Invalid palette, only {0x00, 0x55, 0xAA, 0xFF} allowed.", - /* E_NOTIMAGE */ - "Not an image file.", + /* E_NOTIMAGE */ + "Not an image file.", - /* E_IMAGETHERE */ - "Image record already present, logic error.", + /* E_IMAGETHERE */ + "Image record already present, logic error.", - /* E_IMAGENOTTHERE */ - "Image record required before text record, logic error.", + /* E_IMAGENOTTHERE */ + "Image record required before text record, logic error.", - /* E_TEXTTHERE */ - "Text record already present, logic error.", + /* E_TEXTTHERE */ + "Text record already present, logic error.", - /* E_NOTRECHDR */ - "Invalid record header encountered.", + /* E_NOTRECHDR */ + "Invalid record header encountered.", - /* E_UNKNOWNRECHDR */ - "Unknown record header.", + /* E_UNKNOWNRECHDR */ + "Unknown record header.", - /* E_TOOBIGG */ - "Image too big, maximum size approx. 640*400 gray pixels.", + /* E_TOOBIGG */ + "Image too big, maximum size approx. 640*400 gray pixels.", - /* E_TOOBIGM */ - "Image too big, maximum size approx. 640*800 monochrome pixels.", - }; + /* E_TOOBIGM */ + "Image too big, maximum size approx. 640*800 monochrome pixels.", +}; - const char * - ipdb_err(int const e) { +const char * +ipdb_err(int const e) { - if (e < 0) - return e >= E_LAST ? errorDesc[-e - 1] : "unknown error"; - else - return strerror(e); - } + if (e < 0) + return e >= E_LAST ? errorDesc[-e - 1] : "unknown error"; + else + return strerror(e); +} - static void - rechdr_free(RECHDR * const recP) { +static void +rechdr_free(RECHDR * const recP) { - if (recP) { - free(recP->extra); - free(recP); - } - } + if (recP) { + free(recP->extra); + free(recP); + } +} - void - ipdb_image_free(IMAGE * const imgP) { +void +ipdb_imageFree(IMAGE * const imgP) { - if (imgP) { - rechdr_free(imgP->r); - free(imgP->data); - free(imgP); - } - } + if (imgP) { + rechdr_free(imgP->r); + free(imgP->data); + free(imgP); + } +} - void - ipdb_text_free(TEXT * const textP) { +void +ipdb_textFree(TEXT * const textP) { - if (textP) { - rechdr_free(textP->r); - free(textP->data); - free(textP); - } - } + if (textP) { + rechdr_free(textP->r); + if (textP->data) + free(textP->data); + free(textP); + } +} - void - ipdb_pdbhead_free(PDBHEAD * const headP) { +void +ipdb_pdbheadFree(PDBHEAD * const headP) { - free(headP); - } + free(headP); +} - void - ipdb_clear(IPDB * const pdbP) { +void +ipdb_clear(IPDB * const pdbP) { - if (pdbP) { - ipdb_image_free(pdbP->i); - ipdb_text_free(pdbP->t); - ipdb_pdbhead_free(pdbP->p); + if (pdbP) { + ipdb_imageFree(pdbP->i); + ipdb_textFree(pdbP->t); + ipdb_pdbheadFree(pdbP->p); } } @@ -194,7 +185,7 @@ ipdb_free(IPDB * const pdbP) { PDBHEAD * -ipdb_pdbhead_alloc(const char * const name) { +ipdb_pdbheadAlloc() { PDBHEAD * pdbHeadP; @@ -202,20 +193,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 +200,7 @@ ipdb_pdbhead_alloc(const char * const name) { static RECHDR * -rechdr_alloc(int const type, +rechdrCreate(int const type, uint32_t const offset) { /* @@ -255,10 +232,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 +254,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) { @@ -294,7 +271,7 @@ ipdb_image_alloc(const char * const name, failed = true; if (failed) - ipdb_image_free(imgP); + ipdb_imageFree(imgP); } else failed = true; @@ -304,7 +281,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 +297,13 @@ ipdb_text_alloc(const char * const content) { if (textP) { MEMSZERO(textP); - textP->r = rechdr_alloc(TEXT_REC, 0); - - if (textP->r) { - if (content) { - textP->data = strdup(content); + textP->r = rechdrCreate(TEXT_REC, 0); - 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 +313,7 @@ ipdb_text_alloc(const char * const content) { IPDB * -ipdb_alloc(const char * const name) { +ipdb_alloc(void) { IPDB * pdbP; bool failed; @@ -357,12 +325,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 diff --git a/converter/other/ipdb.h b/converter/other/ipdb.h index 6af5fc44..013fa441 100644 --- a/converter/other/ipdb.h +++ b/converter/other/ipdb.h @@ -201,43 +201,43 @@ 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 *); +ipdb_alloc(void); void ipdb_clear(IPDB * const pdbP); 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..5ead08c2 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" @@ -144,6 +145,34 @@ 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) { + + 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. + */ + pdbHeadP->ctime = + pdbHeadP->mtime = (uint32_t)time(NULL) + unixepoch; + + MEMSCPY(&pdbHeadP->type, IPDB_vIMG); + MEMSCPY(&pdbHeadP->id, IPDB_View); +} + + + static int pdbheadWrite(PDBHEAD * const pdbheadP, FILE * const fileP) { @@ -249,9 +278,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 +504,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 +611,7 @@ insertMimage(IPDB * const pdbP, static int insertText(IPDB * const pdbP, - const char * const s) { + const char * const content) { int retval; @@ -591,17 +620,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; @@ -709,11 +744,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); + readimg(pdbP, ifP, cmdline.depth4); if (cmdline.notefile) diff --git a/converter/other/pdbimgtopam.c b/converter/other/pdbimgtopam.c index 85765ab3..5c0f1094 100644 --- a/converter/other/pdbimgtopam.c +++ b/converter/other/pdbimgtopam.c @@ -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 . @@ -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; @@ -531,62 +531,56 @@ ipdbRead(IPDB * const pdbP, int retval; + int status; + ipdb_clear(pdbP); - 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; } } } @@ -664,7 +658,7 @@ 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 +668,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 +678,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)); } @@ -777,7 +771,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."); |