comparison tools/libgfx.c @ 2065:451980580189

Refactor how paletted/indexed formats are handled in libgfx.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 05 Dec 2018 13:33:03 +0200
parents 3617ef01c1de
children 430c010d97c1
comparison
equal deleted inserted replaced
2064:3617ef01c1de 2065:451980580189
301 301
302 #define DMCOL(x) (((x) >> 4) & 0xf) 302 #define DMCOL(x) (((x) >> 4) & 0xf)
303 303
304 int dmWriteIFFMasterRAWHeader( 304 int dmWriteIFFMasterRAWHeader(
305 DMResource *fp, const char *filename, const char *prefix, 305 DMResource *fp, const char *filename, const char *prefix,
306 const DMImage *img, const DMImageConvSpec *spec, const int fmtid) 306 const DMImage *img, const DMImageConvSpec *spec)
307 { 307 {
308 if (dmfprintf(fp, 308 if (dmfprintf(fp,
309 "%s_width: dw.w %d\n" 309 "%s_width: dw.w %d\n"
310 "%s_height: dw.w %d\n" 310 "%s_height: dw.w %d\n"
311 "%s_nplanes: dw.w %d\n", 311 "%s_nplanes: dw.w %d\n",
312 prefix, img->width * spec->scaleX, 312 prefix, img->width * spec->scaleX,
313 prefix, img->height * spec->scaleY, 313 prefix, img->height * spec->scaleY,
314 prefix, spec->nplanes) < 0) 314 prefix, spec->nplanes) < 0)
315 return dmferror(fp); 315 return dmferror(fp);
316 316
317 if (fmtid == DM_IMGFMT_ARAW) 317 if (spec->fmtid == DM_IMGFMT_ARAW)
318 { 318 {
319 if (dmfprintf(fp, 319 if (dmfprintf(fp,
320 "%s_ncolors: dw.w %d\n" 320 "%s_ncolors: dw.w %d\n"
321 "%s_palette:\n", 321 "%s_palette:\n",
322 prefix, img->ncolors, 322 prefix, img->ncolors,
997 // Always force planar for PCX 997 // Always force planar for PCX
998 memcpy(&spec, pspec, sizeof(DMImageConvSpec)); 998 memcpy(&spec, pspec, sizeof(DMImageConvSpec));
999 spec.planar = TRUE; 999 spec.planar = TRUE;
1000 1000
1001 // XXX: 24bit PCX does not work yet .. 1001 // XXX: 24bit PCX does not work yet ..
1002 if (!spec.paletted) 1002 if (spec.format != DM_COLFMT_PALETTE)
1003 { 1003 {
1004 return dmError(DMERR_NOT_SUPPORTED, 1004 return dmError(DMERR_NOT_SUPPORTED,
1005 "24bit PCX not supported yet.\n"); 1005 "24bit PCX not supported yet.\n");
1006 } 1006 }
1007 1007
1008 if (spec.paletted && img->pal == NULL) 1008 if (spec.format == DM_COLFMT_PALETTE && img->pal == NULL)
1009 { 1009 {
1010 return dmError(DMERR_NULLPTR, 1010 return dmError(DMERR_NULLPTR,
1011 "Image spec says paletted/indexed image, but palette pointer is NULL.\n"); 1011 "Image spec says paletted/indexed image, but palette pointer is NULL.\n");
1012 } 1012 }
1013 1013
1016 pcx.header = &hdr; 1016 pcx.header = &hdr;
1017 pcx.fp = fp; 1017 pcx.fp = fp;
1018 1018
1019 // Create PCX header 1019 // Create PCX header
1020 dmMemset(&hdr, 0, sizeof(hdr)); 1020 dmMemset(&hdr, 0, sizeof(hdr));
1021 if (spec.paletted) 1021 if (spec.format == DM_COLFMT_PALETTE)
1022 { 1022 {
1023 const int ncolors = img->ncolors > DMPCX_PAL_COLORS ? DMPCX_PAL_COLORS : img->ncolors; 1023 const int ncolors = img->ncolors > DMPCX_PAL_COLORS ? DMPCX_PAL_COLORS : img->ncolors;
1024 for (int i = 0; i < ncolors; i++) 1024 for (int i = 0; i < ncolors; i++)
1025 { 1025 {
1026 hdr.colorMap[i].r = img->pal[i].r; 1026 hdr.colorMap[i].r = img->pal[i].r;
1053 "PCX: xmin=%d, ymin=%d, xmax=%d, ymax=%d, res=%dx%d, scr=%dx%d\n", 1053 "PCX: xmin=%d, ymin=%d, xmax=%d, ymax=%d, res=%dx%d, scr=%dx%d\n",
1054 hdr.xmin, hdr.ymin, hdr.xmax, hdr.ymax, 1054 hdr.xmin, hdr.ymin, hdr.xmax, hdr.ymax,
1055 hdr.hres, hdr.vres, 1055 hdr.hres, hdr.vres,
1056 hdr.hScreenSize, hdr.vScreenSize); 1056 hdr.hScreenSize, hdr.vScreenSize);
1057 1057
1058 dmMsg(2, "PCX: nplanes=%d, bpp=%d, bpl=%d, isPaletted=%s, planar=%s\n", 1058 dmMsg(2, "PCX: nplanes=%d, bpp=%d, bpl=%d, colfmt=%d, planar=%s\n",
1059 hdr.nplanes, hdr.bitsPerPlane, hdr.bpl, 1059 hdr.nplanes, hdr.bitsPerPlane, hdr.bpl,
1060 spec.paletted ? "yes" : "no", 1060 spec.format,
1061 spec.planar ? "yes" : "no" 1061 spec.planar ? "yes" : "no"
1062 ); 1062 );
1063 1063
1064 // TODO XXX this is also bogus 1064 // TODO XXX this is also bogus
1065 pcx.bufLen = hdr.bpl * 4; 1065 pcx.bufLen = hdr.bpl * 4;
1116 1116
1117 // Write image data 1117 // Write image data
1118 res = dmWriteImageData(img, (void *) &pcx, dmWritePCXRow, &spec); 1118 res = dmWriteImageData(img, (void *) &pcx, dmWritePCXRow, &spec);
1119 1119
1120 // Write VGA palette 1120 // Write VGA palette
1121 if (spec.paletted) 1121 if (spec.format == DM_COLFMT_PALETTE)
1122 { 1122 {
1123 int i; 1123 int i;
1124 dmMsg(2, "PCX: Writing palette of %d active entries.\n", img->ncolors); 1124 dmMsg(2, "PCX: Writing palette of %d active entries.\n", img->ncolors);
1125 1125
1126 dmf_write_byte(pcx.fp, 0x0C); 1126 dmf_write_byte(pcx.fp, 0x0C);
2197 { 2197 {
2198 DMIFF iff; 2198 DMIFF iff;
2199 Uint8 *buf = NULL; 2199 Uint8 *buf = NULL;
2200 size_t bufLen; 2200 size_t bufLen;
2201 int res = DMERR_OK; 2201 int res = DMERR_OK;
2202 //DMImageConvSpec pspec, *spec = &pspec;
2203
2204 //memcpy(&pspec, cpspec, sizeof(DMImageConvSpec));
2205 2202
2206 // XXX: Non-paletted IFF not supported! 2203 // XXX: Non-paletted IFF not supported!
2207 if (!spec->paletted) 2204 if (spec->format != DM_COLFMT_PALETTE)
2208 { 2205 {
2209 return dmError(DMERR_NOT_SUPPORTED, 2206 return dmError(DMERR_NOT_SUPPORTED,
2210 "Non-paletted IFF is not supported.\n"); 2207 "Non-paletted IFF is not supported.\n");
2211 } 2208 }
2212 2209
2213 switch (spec->format) 2210 switch (spec->fmtid)
2214 { 2211 {
2215 case DM_IMGFMT_IFF_ILBM: iff.idsig = IFF_ID_ILBM; iff.idstr = "ILBM"; break; 2212 case DM_IMGFMT_IFF_ILBM: iff.idsig = IFF_ID_ILBM; iff.idstr = "ILBM"; break;
2216 case DM_IMGFMT_IFF_PBM : iff.idsig = IFF_ID_PBM; iff.idstr = "PBM"; break; 2213 case DM_IMGFMT_IFF_PBM : iff.idsig = IFF_ID_PBM; iff.idstr = "PBM"; break;
2217 case DM_IMGFMT_IFF_ACBM: iff.idsig = IFF_ID_ACBM; iff.idstr = "ACBM"; break; 2214 case DM_IMGFMT_IFF_ACBM: iff.idsig = IFF_ID_ACBM; iff.idstr = "ACBM"; break;
2215 default:
2216 return dmError(DMERR_NOT_SUPPORTED,
2217 "Invalid IFF format.\n");
2218 } 2218 }
2219 2219
2220 // Setup headers 2220 // Setup headers
2221 iff.bmhd.x = 0; 2221 iff.bmhd.x = 0;
2222 iff.bmhd.y = 0; 2222 iff.bmhd.y = 0;
2275 goto out; 2275 goto out;
2276 2276
2277 // 2277 //
2278 // CMAP 2278 // CMAP
2279 // 2279 //
2280 if (img->ncolors > 0 && spec->paletted) 2280 if (img->ncolors > 0 && spec->format == DM_COLFMT_PALETTE)
2281 { 2281 {
2282 if ((res = dmWriteIFFChunkHdr(fp, &iff.chCMAP, IFF_ID_CMAP)) != DMERR_OK) 2282 if ((res = dmWriteIFFChunkHdr(fp, &iff.chCMAP, IFF_ID_CMAP)) != DMERR_OK)
2283 goto out; 2283 goto out;
2284 2284
2285 for (int i = 0; i < img->ncolors; i++) 2285 for (int i = 0; i < img->ncolors; i++)