Mercurial > hg > dmlib
diff src/libgfx.c @ 958:985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 27 Feb 2015 03:58:25 +0200 |
parents | efbad8817b79 |
children | e06abfde6c39 |
line wrap: on
line diff
--- a/src/libgfx.c Fri Feb 27 02:21:57 2015 +0200 +++ b/src/libgfx.c Fri Feb 27 03:58:25 2015 +0200 @@ -299,8 +299,9 @@ if ((fp = fopen(filename, "wb")) == NULL) { - dmError("RAW: Could not open file '%s' for writing.\n", filename); - return DMERR_FOPEN; + return dmError(DMERR_FOPEN, + "RAW: Could not open file '%s' for writing.\n", + filename); } res = dmWriteRAWImageFILE(fp, img, spec); @@ -341,8 +342,9 @@ // Create output file if ((fp = fopen(filename, "wb")) == NULL) { - dmError("PPM: could not open file '%s' for writing.\n", filename); - return DMERR_FOPEN; + return dmError(DMERR_FOPEN, + "PPM: could not open file '%s' for writing.\n", + filename); } res = dmWritePPMImageFILE(fp, img, spec); @@ -380,23 +382,24 @@ if (png_ptr == NULL) { - dmError("PNG: png_create_write_struct() failed.\n"); - res = DMERR_MALLOC; + res = dmError(DMERR_MALLOC, + "PNG: png_create_write_struct() failed.\n"); goto error; } info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL) { - dmError("PNG: png_create_info_struct(%p) failed.\n", png_ptr); - res = DMERR_INIT_FAIL; + res = dmError(DMERR_INIT_FAIL, + "PNG: png_create_info_struct(%p) failed.\n", + png_ptr); goto error; } if (setjmp(png_jmpbuf(png_ptr))) { - dmError("PNG: Error during image writing..\n"); - res = DMERR_INIT_FAIL; + res = dmError(DMERR_INIT_FAIL, + "PNG: Error during image writing..\n"); goto error; } @@ -409,8 +412,9 @@ case DM_IFMT_RGB : fmt = PNG_COLOR_TYPE_RGB; break; case DM_IFMT_RGBA : fmt = PNG_COLOR_TYPE_RGB_ALPHA; break; default: - dmError("PNG: Unsupported image format %d.\n", spec->format); - res = DMERR_NOT_SUPPORTED; + res = dmError(DMERR_NOT_SUPPORTED, + "PNG: Unsupported image format %d.\n", + spec->format); goto error; } @@ -436,8 +440,8 @@ if (palette == NULL) { - dmError("PNG: Could not allocate palette structure."); - res = DMERR_MALLOC; + res = dmError(DMERR_MALLOC, + "PNG: Could not allocate palette structure."); goto error; } @@ -482,8 +486,9 @@ if ((fp = fopen(filename, "wb")) == NULL) { - dmError("PNG: could not open file '%s' for writing.\n", filename); - return DMERR_FOPEN; + return dmError(DMERR_FOPEN, + "PNG: could not open file '%s' for writing.\n", + filename); } res = dmWritePNGImageFILE(fp, img, spec); @@ -512,23 +517,24 @@ if (png_ptr == NULL) { - dmError("PNG: png_create_write_struct() failed.\n"); - res = DMERR_MALLOC; + res = dmError(DMERR_MALLOC, + "PNG: png_create_write_struct() failed.\n"); goto error; } info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL) { - dmError("PNG: png_create_info_struct(%p) failed.\n", png_ptr); - res = DMERR_INIT_FAIL; + res = dmError(DMERR_INIT_FAIL, + "PNG: png_create_info_struct(%p) failed.\n", + png_ptr); goto error; } if (setjmp(png_jmpbuf(png_ptr))) { - dmError("PNG: Error during image reading..\n"); - res = DMERR_INIT_FAIL; + res = dmError(DMERR_INIT_FAIL, + "PNG: Error during image reading..\n"); goto error; } @@ -542,9 +548,9 @@ if (width < 1 || height < 1) { - dmError("PNG: Invalid width or height (%d x %d)\n", + res = dmError(DMERR_INVALID_DATA, + "PNG: Invalid width or height (%d x %d)\n", width, height); - res = DMERR_INVALID_DATA; goto error; } @@ -556,9 +562,9 @@ if (bit_depth > 8) { - dmError("PNG: Unsupported bit depth for grayscale image: %d\n", + res = dmError(DMERR_NOT_SUPPORTED, + "PNG: Unsupported bit depth for grayscale image: %d\n", bit_depth); - res = DMERR_NOT_SUPPORTED; goto error; } break; @@ -568,8 +574,8 @@ break; default: - dmError("PNG: RGB/RGBA images not supported for loading.\n"); - res = DMERR_NOT_SUPPORTED; + res = dmError(DMERR_NOT_SUPPORTED, + "PNG: RGB/RGBA images not supported for loading.\n"); goto error; } @@ -579,8 +585,8 @@ if ((*pimg = img = dmImageAlloc(width, height)) == NULL) { - dmError("PNG: Could not allocate image data.\n"); - res = DMERR_MALLOC; + res = dmError(DMERR_MALLOC, + "PNG: Could not allocate image data.\n"); goto error; } @@ -665,8 +671,9 @@ if ((fp = fopen(filename, "rb")) == NULL) { - dmError("PNG: Could not open file '%s' for reading.\n", filename); - return DMERR_FOPEN; + return dmError(DMERR_FOPEN, + "PNG: Could not open file '%s' for reading.\n", + filename); } res = dmReadPNGImageFILE(fp, img); @@ -841,9 +848,9 @@ pcx.bufLen = hdr.bpl * 4; if ((pcx.buf = dmMalloc(pcx.bufLen)) == NULL) { - dmError("PCX: Could not allocate %d bytes for RLE compression buffer.\n", + res = dmError(DMERR_MALLOC, + "PCX: Could not allocate %d bytes for RLE compression buffer.\n", pcx.bufLen); - res = DMERR_MALLOC; goto error; } @@ -853,8 +860,8 @@ !dm_fwrite_byte(pcx.fp, hdr.encoding) || !dm_fwrite_byte(pcx.fp, hdr.bpp)) { - dmError("PCX: Could not write basic header data.\n"); - res = DMERR_FWRITE; + res = dmError(DMERR_FWRITE, + "PCX: Could not write basic header data.\n"); goto error; } @@ -865,15 +872,15 @@ !dm_fwrite_le16(pcx.fp, hdr.hres) || !dm_fwrite_le16(pcx.fp, hdr.vres)) { - dmError("PCX: Could not write image dimensions.\n"); - res = DMERR_FWRITE; + res = dmError(DMERR_FWRITE, + "PCX: Could not write image dimensions.\n"); goto error; } if (!dm_fwrite_str(pcx.fp, (Uint8 *) &hdr.colormap, sizeof(hdr.colormap))) { - dmError("PCX: Could not write colormap.\n"); - res = DMERR_FWRITE; + res = dmError(DMERR_FWRITE, + "PCX: Could not write colormap.\n"); goto error; } @@ -883,8 +890,8 @@ !dm_fwrite_le16(pcx.fp, hdr.palinfo) || !dm_fwrite_str(pcx.fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler))) { - dmError("PCX: Could not write header remainder.\n"); - res = DMERR_FWRITE; + res = dmError(DMERR_FWRITE, + "PCX: Could not write header remainder.\n"); goto error; } @@ -927,8 +934,9 @@ if ((fp = fopen(filename, "wb")) == NULL) { - dmError("PCX: Could not open file '%s' for writing.\n", filename); - return DMERR_FOPEN; + return dmError(DMERR_FOPEN, + "PCX: Could not open file '%s' for writing.\n", + filename); } res = dmWritePCXImageFILE(fp, img, spec); @@ -984,8 +992,8 @@ !dm_fread_byte(fp, &hdr.encoding) || !dm_fread_byte(fp, &hdr.bpp)) { - dmError("PCX: Could not read basic header data.\n"); - res = DMERR_FREAD; + res = dmError(DMERR_FREAD, + "PCX: Could not read basic header data.\n"); goto error; } @@ -994,8 +1002,8 @@ hdr.encoding != 1 || hdr.bpp != 8) { - dmError("PCX: Not a PCX file, or unsupported variant.\n"); - res = DMERR_FREAD; + res = dmError(DMERR_NOT_SUPPORTED, + "PCX: Not a PCX file, or unsupported variant.\n"); goto error; } @@ -1006,15 +1014,15 @@ !dm_fread_le16(fp, &hdr.hres) || !dm_fread_le16(fp, &hdr.vres)) { - dmError("PCX: Could not read image dimensions.\n"); - res = DMERR_FREAD; + res = dmError(DMERR_FREAD, + "PCX: Could not read image dimensions.\n"); goto error; } if (!dm_fread_str(fp, (Uint8 *) &hdr.colormap, sizeof(hdr.colormap))) { - dmError("PCX: Could not read colormap.\n"); - res = DMERR_FREAD; + res = dmError(DMERR_FREAD, + "PCX: Could not read colormap.\n"); goto error; } @@ -1024,23 +1032,24 @@ !dm_fread_le16(fp, &hdr.palinfo) || !dm_fread_str(fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler))) { - dmError("PCX: Could not read header remainder.\n"); - res = DMERR_FREAD; + res = dmError(DMERR_FREAD, + "PCX: Could not read header remainder.\n"); goto error; } if (hdr.nplanes != 3 && hdr.nplanes != 1) { - dmError("PCX: Unsupported number of bitplanes %d.\n", hdr.nplanes); - res = DMERR_FREAD; + res = dmError(DMERR_FREAD, + "PCX: Unsupported number of bitplanes %d.\n", + hdr.nplanes); goto error; } // Allocate image if ((*pimg = img = dmImageAlloc(hdr.xmax - hdr.xmin + 1, hdr.ymax - hdr.ymin + 1)) == NULL) { - dmError("PCX: Could not allocate image structure.\n"); - res = DMERR_MALLOC; + res = dmError(DMERR_MALLOC, + "PCX: Could not allocate image structure.\n"); goto error; } @@ -1048,8 +1057,8 @@ pcx.bufLen = hdr.nplanes * hdr.bpl; if ((pcx.buf = dmMalloc(pcx.bufLen)) == NULL) { - dmError("PCX: Could not allocate RLE buffer.\n"); - res = DMERR_MALLOC; + res = dmError(DMERR_MALLOC, + "PCX: Could not allocate RLE buffer.\n"); goto error; } @@ -1060,8 +1069,8 @@ // Decode row of RLE'd data if (!dmPCXDecodeRLERow(fp, pcx.buf, pcx.bufLen)) { - dmError("PCX: Error decoding RLE data.\n"); - res = DMERR_INVALID_DATA; + res = dmError(DMERR_INVALID_DATA, + "PCX: Error decoding RLE data.\n"); goto error; } @@ -1112,8 +1121,8 @@ if (!dmImageAllocPalette(img, ncolors, -1)) { - dmError("PCX: Could not allocate palette data!\n"); - res = DMERR_MALLOC; + res = dmError(DMERR_MALLOC, + "PCX: Could not allocate palette data!\n"); goto error; } @@ -1121,8 +1130,9 @@ { if (!dmReadPaletteData(fp, img->pal, ncolors)) { - dmError("PCX: Error reading palette.\n"); - return DMERR_FREAD; + res = dmError(DMERR_FREAD, + "PCX: Error reading palette.\n"); + goto error; } } else @@ -1152,8 +1162,9 @@ if ((fp = fopen(filename, "rb")) == NULL) { - dmError("PCX: Could not open file '%s' for reading.\n", filename); - return -15; + return dmError(DMERR_FOPEN, + "PCX: Could not open file '%s' for reading.\n", + filename); } res = dmReadPCXImageFILE(fp, pimg); @@ -1222,11 +1233,12 @@ if (!dm_fread_be32(fp, &chunk->id) || !dm_fread_be32(fp, &chunk->size)) { - dmError("ILBM: Could not read IFF chunk header.\n"); - return FALSE; + dmError(DMERR_FREAD, + "ILBM: Could not read IFF chunk header.\n"); + return TRUE; } else - return TRUE; + return FALSE; } static char * dmGetIFFChunkID(DMIFFChunk *chunk) @@ -1239,34 +1251,44 @@ return chunk->str; } -static BOOL dmSkipIFFChunkRest(FILE *fp, const DMIFFChunk *chunk, const Uint32 used) +static int dmSkipIFFChunkRest(FILE *fp, const DMIFFChunk *chunk, const Uint32 used) { if (chunk->size > used) { dmMsg(4, "ILBM: Skipping %d bytes (%d of %d consumed)\n", chunk->size - used, used, chunk->size); - return fseeko(fp, chunk->size - used, SEEK_CUR) == 0; + + if (fseeko(fp, chunk->size - used, SEEK_CUR) != 0) + { + return dmError(DMERR_FSEEK, + "ILBM: Failed to skip chunk end.\n"); + } + else + return DMERR_OK; } else - return TRUE; + return DMERR_OK; } -static BOOL dmCheckIFFChunk(DMIFFChunk *dest, DMIFFChunk *chunk, +static int dmCheckIFFChunk(DMIFFChunk *dest, DMIFFChunk *chunk, const BOOL multi, const Uint32 minSize) { if (dest->count > 0 && !multi) { - dmError("ILBM: Multiple instances of chunk %s found.\n", + return dmError(DMERR_INVALID_DATA, + "ILBM: Multiple instances of chunk %s found.\n", dmGetIFFChunkID(chunk)); - return FALSE; } - + dest->count++; if (chunk->size < minSize) - return FALSE; - - return TRUE; + { + return dmError(DMERR_OUT_OF_DATA, + "ILBM: Chunk is too small.\n"); + } + + return DMERR_OK; } @@ -1367,8 +1389,9 @@ // Decompress or read data if (!dmIFFReadOneRow(fp, iff, buf, bufLen)) { - dmError("ILBM: Error in reading image plane #%d @ %d.\n", plane, yc); - res = DMERR_FREAD; + res = dmError(DMERR_FREAD, + "ILBM: Error in reading image plane #%d @ %d.\n", + plane, yc); goto error; } @@ -1386,8 +1409,8 @@ // Decompress or read data if (!dmIFFReadOneRow(fp, iff, buf, bufLen)) { - dmError("ILBM: Error in reading mask plane.\n"); - res = DMERR_FREAD; + res = dmError(DMERR_FREAD, + "ILBM: Error in reading mask plane.\n"); goto error; } @@ -1429,8 +1452,8 @@ if (!dmIFFReadOneRow(fp, iff, dp, img->width)) { - dmError("ILBM: Error in reading image row #%d.\n", yc); - res = DMERR_FREAD; + res = dmError(DMERR_FREAD, + "ILBM: Error in reading image row #%d.\n", yc); goto error; } @@ -1458,16 +1481,16 @@ chunk.id != IFF_ID_FORM || chunk.size < 32) { - dmError("ILBM: Not a IFF file.\n"); - return DMERR_FREAD; + return dmError(DMERR_NOT_SUPPORTED, + "ILBM: Not a IFF file.\n"); } // Check IFF ILBM signature if (!dm_fread_be32(fp, &idILBM) || (idILBM != IFF_ID_ILBM && idILBM != IFF_ID_PBM)) { - dmError("ILBM: Not a ILBM file.\n"); - return DMERR_INVALID_DATA; + return dmError(DMERR_INVALID_DATA, + "ILBM: Not a ILBM file.\n"); } iff.planar = (idILBM == IFF_ID_ILBM); @@ -1476,16 +1499,16 @@ { if (!dmReadIFFChunk(fp, &chunk)) { - dmError("ILBM: Error reading IFF ILBM data.\n"); - return DMERR_FREAD; + return dmError(DMERR_FREAD, + "ILBM: Error reading IFF ILBM data.\n"); } switch (chunk.id) { case IFF_ID_BMHD: // Check for multiple occurences of BMHD - if (!dmCheckIFFChunk(&iff.chBMHD, &chunk, FALSE, sizeof(iff.bmhd))) - return DMERR_FREAD; + if ((res = dmCheckIFFChunk(&iff.chBMHD, &chunk, FALSE, sizeof(iff.bmhd))) != DMERR_OK) + return res; // Read BMHD data if (!dm_fread_be16(fp, &iff.bmhd.w) || @@ -1502,9 +1525,10 @@ !dm_fread_be16(fp, (Uint16 *) &iff.bmhd.pagew) || !dm_fread_be16(fp, (Uint16 *) &iff.bmhd.pageh)) { - dmError("ILBM: Error reading BMHD chunk.\n"); - return DMERR_FREAD; + return dmError(DMERR_FREAD, + "ILBM: Error reading BMHD chunk.\n"); } + dmMsg(3, "ILBM: BMHD %d x %d @ %d, %d : nplanes=%d, comp=%d, mask=%d\n", iff.bmhd.w, iff.bmhd.h, iff.bmhd.x, iff.bmhd.y, iff.bmhd.nplanes, iff.bmhd.compression, iff.bmhd.masking); @@ -1517,23 +1541,27 @@ iff.bmhd.masking != IFF_MASK_HAS_MASK && iff.bmhd.masking != IFF_MASK_TRANSP)) { - dmError("ILBM: Unsupported features, refusing to load.\n"); - return DMERR_NOT_SUPPORTED; + return dmError(DMERR_NOT_SUPPORTED, + "ILBM: Unsupported features, refusing to load.\n"); } - if (!dmSkipIFFChunkRest(fp, &chunk, sizeof(iff.bmhd))) - return DMERR_FREAD; + if ((res = dmSkipIFFChunkRest(fp, &chunk, sizeof(iff.bmhd))) != DMERR_OK) + return res; break; case IFF_ID_CMAP: // Check for multiple occurences of CMAP - if (!dmCheckIFFChunk(&iff.chCMAP, &chunk, FALSE, 3)) - return DMERR_FREAD; + if ((res = dmCheckIFFChunk(&iff.chCMAP, &chunk, FALSE, 3)) != DMERR_OK) + return res; // Check for sanity if (chunk.size % 3 != 0) - dmError("ILBM: CMAP chunk size not divisible by 3, possibly broken file.\n"); + { + // Non-fatal + dmError(DMERR_INVALID_DATA, + "ILBM: CMAP chunk size not divisible by 3, possibly broken file.\n"); + } iff.ncolors = chunk.size / 3; dmMsg(3, "ILBM: CMAP %d entries (%d bytes)\n", @@ -1548,13 +1576,13 @@ if (!dmPaletteAlloc(&iff.pal, iff.ncolors, (iff.bmhd.masking == IFF_MASK_TRANSP) ? iff.bmhd.transp : -1)) { - dmError("ILBM: Could not allocate palette data.\n"); - return DMERR_MALLOC; + return dmError(DMERR_MALLOC, + "ILBM: Could not allocate palette data.\n"); } if (!dmReadPaletteData(fp, iff.pal, iff.ncolors)) { - dmError("ILBM: Error reading CMAP.\n"); - return DMERR_FREAD; + return dmError(DMERR_FREAD, + "ILBM: Error reading CMAP.\n"); } } @@ -1564,14 +1592,14 @@ case IFF_ID_BODY: // Check for multiple occurences of CMAP - if (!dmCheckIFFChunk(&iff.chBODY, &chunk, FALSE, 1)) - return DMERR_FREAD; + if ((res = dmCheckIFFChunk(&iff.chBODY, &chunk, FALSE, 1)) != DMERR_OK) + return res; // Check for sanity if (!iff.chBMHD.count) { - dmError("ILBM: BODY chunk before BMHD?\n"); - return DMERR_INVALID_DATA; + return dmError(DMERR_INVALID_DATA, + "ILBM: BODY chunk before BMHD?\n"); } dmMsg(3, "ILBM: BODY chunk size %d bytes\n", chunk.size); @@ -1588,8 +1616,8 @@ return res; } - if (!dmSkipIFFChunkRest(fp, &chunk, read)) - return DMERR_FREAD; + if ((res = dmSkipIFFChunkRest(fp, &chunk, read)) != DMERR_OK) + return res; if (iff.chCMAP.count) parsed = TRUE; @@ -1599,23 +1627,23 @@ case IFF_ID_CAMG: if (!dm_fread_be32(fp, &iff.camg)) { - dmError("ILBM: Error reading CAMG chunk.\n"); - return DMERR_FREAD; + return dmError(DMERR_FREAD, + "ILBM: Error reading CAMG chunk.\n"); } dmMsg(3, "ILBM: CAMG value 0x%08x\n", iff.camg); if ((iff.camg & IFF_CAMG_HAM)) { - dmError("ILBM: HAM files are not supported.\n"); - return DMERR_NOT_SUPPORTED; + return dmError(DMERR_NOT_SUPPORTED, + "ILBM: HAM files are not supported.\n"); } - if (!dmSkipIFFChunkRest(fp, &chunk, 4)) - return DMERR_FREAD; + if ((res = dmSkipIFFChunkRest(fp, &chunk, sizeof(Uint32))) != DMERR_OK) + return res; break; - + default: { dmMsg(4, "Unknown chunk ID '%s', size %d\n", @@ -1623,8 +1651,8 @@ if (fseeko(fp, chunk.size, SEEK_CUR) != 0) { - dmError("ILBM: Error skipping in file."); - return DMERR_FREAD; + return dmError(DMERR_FSEEK, + "ILBM: Error skipping in file."); } } break; @@ -1644,8 +1672,8 @@ if (iff.ncolors > 128) { - dmError("ILBM: Halfbrite enabled, but ncolors > 128.\n"); - return DMERR_NOT_SUPPORTED; + return dmError(DMERR_NOT_SUPPORTED, + "ILBM: Halfbrite enabled, but ncolors > 128.\n"); } if ((ptmp = dmRealloc(iff.pal, sizeof(DMColor) * iff.ncolors * 2)) == NULL) @@ -1681,8 +1709,9 @@ if ((fp = fopen(filename, "rb")) == NULL) { - dmError("ILBM: Could not open file '%s' for reading.\n", filename); - return DMERR_FOPEN; + return dmError(DMERR_FOPEN, + "ILBM: Could not open file '%s' for reading.\n", + filename); } res = dmReadILBMImageFILE(fp, pimg);