comparison tools/libgfx.c @ 2049:a945a5f2fd70

Improve the cdump output format support of gfxconv. Also add some error handling.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 03 Dec 2018 18:58:45 +0200
parents 3829c292df02
children 416af5a842ec
comparison
equal deleted inserted replaced
2048:0a1fe72be4a9 2049:a945a5f2fd70
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, const int fmtid)
307 { 307 {
308 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); 314 prefix, spec->nplanes) < 0)
315 return dmferror(fp);
315 316
316 if (fmtid == DM_IMGFMT_ARAW) 317 if (fmtid == DM_IMGFMT_ARAW)
317 { 318 {
318 dmfprintf(fp, 319 if (dmfprintf(fp,
319 "%s_ncolors: dw.w %d\n" 320 "%s_ncolors: dw.w %d\n"
320 "%s_palette:\n", 321 "%s_palette:\n",
321 prefix, img->ncolors, 322 prefix, img->ncolors,
322 prefix); 323 prefix) < 0)
324 return dmferror(fp);
323 325
324 for (int i = 0; i < (1 << spec->nplanes); i++) 326 for (int i = 0; i < (1 << spec->nplanes); i++)
325 { 327 {
326 Uint32 color; 328 Uint32 color;
327 if (i < img->ncolors) 329 if (i < img->ncolors)
331 (DMCOL(img->pal[i].b)); 333 (DMCOL(img->pal[i].b));
332 } 334 }
333 else 335 else
334 color = 0; 336 color = 0;
335 337
336 dmfprintf(fp, 338 if (dmfprintf(fp,
337 "\tdc.w $%04X\n", 339 "\tdc.w $%04X\n",
338 color); 340 color) < 0)
339 } 341 return dmferror(fp);
340 342 }
341 dmfprintf(fp, 343
344 if (dmfprintf(fp,
342 "%s: incbin \"%s\"\n", 345 "%s: incbin \"%s\"\n",
343 prefix, filename); 346 prefix, filename) < 0)
347 return dmferror(fp);
344 } 348 }
345 349
346 return DMERR_OK; 350 return DMERR_OK;
347 } 351 }
348 352
394 398
395 return dmFlushBitStream(&bs); 399 return dmFlushBitStream(&bs);
396 } 400 }
397 401
398 402
399 static int dmWriteCDumpRow(void *cbdata, const Uint8 *row, const size_t len) 403 static BOOL dmPutByteCDump(DMBitStreamContext *ctx, const Uint8 val)
400 { 404 {
401 DMResource *fp = (DMResource *) cbdata; 405 return dmfprintf((DMResource *) ctx->handle, "0x%02x, ", val) > 0;
402 for (size_t n = 0; n < len; n++)
403 {
404 dmfprintf(fp, "0x%02x, ", row[n]);
405 }
406
407 dmfprintf(fp, "\n");
408
409 return DMERR_OK;
410 } 406 }
411 407
412 408
413 int dmWriteCDumpImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec) 409 int dmWriteCDumpImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
414 { 410 {
415 int res; 411 int res;
416 412 DMBitStreamContext bs;
417 dmfprintf(fp, 413
414 if (dmfprintf(fp,
418 "#define SET_WIDTH %d\n" 415 "#define SET_WIDTH %d\n"
419 "#define SET_HEIGHT %d\n" 416 "#define SET_HEIGHT %d\n"
420 "\n" 417 "\n"
421 "Uint8 img_data[] = {\n", 418 "Uint8 img_data[] = {\n",
422
423 img->width * spec->scaleX, 419 img->width * spec->scaleX,
424 img->height * spec->scaleY); 420 img->height * spec->scaleY) < 0)
425 421 return dmferror(fp);
426 res = dmWriteImageData(img, (void *) fp, dmWriteCDumpRow, spec); 422
427 423 dmInitBitStreamContext(&bs);
428 dmfprintf(fp, 424 bs.putByte = dmPutByteCDump;
429 "};\n"); 425 bs.handle = (void *) fp;
426
427 if (spec->planar)
428 {
429 // Output bitplanes in planar format
430 // (each plane of line sequentially)
431 for (int yc = 0; yc < img->height; yc++)
432 for (int yscale = 0; yscale < spec->scaleY; yscale++)
433 for (int plane = 0; plane < spec->nplanes; plane++)
434 {
435 if (!dmWriteRAWRow(&bs, img, spec, yc, plane))
436 return DMERR_FWRITE;
437
438 if (dmfprintf(fp, "\n") < 0)
439 return dmferror(fp);
440 }
441 }
442 else
443 {
444 // Output each bitplane in sequence
445 for (int plane = 0; plane < spec->nplanes; plane++)
446 for (int yc = 0; yc < img->height; yc++)
447 for (int yscale = 0; yscale < spec->scaleY; yscale++)
448 {
449 if (!dmWriteRAWRow(&bs, img, spec, yc, plane))
450 return DMERR_FWRITE;
451
452 if (dmfprintf(fp, "\n") < 0)
453 return dmferror(fp);
454 }
455 }
456
457 res = dmFlushBitStream(&bs);
458
459 if (res == DMERR_OK &&
460 dmfprintf(fp, "};\n") < 0)
461 res = dmferror(fp);
430 462
431 return res; 463 return res;
432 } 464 }
433 465
434 466