comparison tools/libgfx.c @ 1886:1af79412f249

Remove the stdio FILE support from libgfx API, now only DMResource is supported.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 25 Jun 2018 13:52:20 +0300
parents 73545a442ffe
children 297aa8f0ca7f
comparison
equal deleted inserted replaced
1885:e69de47d2419 1886:1af79412f249
297 dmFree(row); 297 dmFree(row);
298 return res; 298 return res;
299 } 299 }
300 300
301 301
302 #define DMCOL(x) (((x) >> 4) & 0xf)
303
304 int dmWriteIFFMasterRAWHeader(
305 DMResource *fp, const char *filename, const char *prefix,
306 const DMImage *img, const DMImageConvSpec *spec, const int fmtid)
307 {
308 dmfprintf(fp,
309 "%s_width: dw.w %d\n"
310 "%s_height: dw.w %d\n"
311 "%s_nplanes: dw.w %d\n",
312 prefix, img->width * spec->scaleX,
313 prefix, img->height * spec->scaleY,
314 prefix, spec->nplanes);
315
316 if (fmtid == DM_IMGFMT_ARAW)
317 {
318 dmfprintf(fp,
319 "%s_ncolors: dw.w %d\n"
320 "%s_palette:\n",
321 prefix, img->ncolors,
322 prefix);
323
324 for (int i = 0; i < (1 << spec->nplanes); i++)
325 {
326 Uint32 color;
327 if (i < img->ncolors)
328 {
329 color = (DMCOL(img->pal[i].r) << 8) |
330 (DMCOL(img->pal[i].g) << 4) |
331 (DMCOL(img->pal[i].b));
332 }
333 else
334 color = 0;
335
336 dmfprintf(fp,
337 "\tdc.w $%04X\n",
338 color);
339 }
340
341 dmfprintf(fp,
342 "%s: incbin \"%s\"\n",
343 prefix, filename);
344 }
345
346 return dmferror(fp);
347 }
348
349
302 static BOOL dmWriteRAWRow(DMBitStreamContext *bs, const DMImage *img, const DMImageConvSpec *spec, const int yc, const int plane) 350 static BOOL dmWriteRAWRow(DMBitStreamContext *bs, const DMImage *img, const DMImageConvSpec *spec, const int yc, const int plane)
303 { 351 {
304 const Uint8 *sp = img->data + (yc * img->pitch); 352 const Uint8 *sp = img->data + (yc * img->pitch);
305 for (int xc = 0; xc < img->width; xc++) 353 for (int xc = 0; xc < img->width; xc++)
306 { 354 {
310 } 358 }
311 return TRUE; 359 return TRUE;
312 } 360 }
313 361
314 362
315 int dmWriteRAWImageFILE(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec) 363 int dmWriteRAWImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
316 { 364 {
317 int res; 365 int res;
318 DMBitStreamContext bs; 366 DMBitStreamContext bs;
319 367
320 if ((res = dmInitBitStreamFILE(&bs, fp)) != DMERR_OK) 368 if ((res = dmInitBitStreamFILE(&bs, fp)) != DMERR_OK)
346 394
347 return dmFlushBitStream(&bs); 395 return dmFlushBitStream(&bs);
348 } 396 }
349 397
350 398
351 int dmWriteRAWImage(const char *filename, const DMImage *img, const DMImageConvSpec *spec)
352 {
353 DMResource *fp;
354 int res;
355
356 if ((res = dmf_open_stdio(filename, "wb", &fp)) != DMERR_OK)
357 {
358 return dmError(res,
359 "RAW: Could not open file '%s' for writing.\n",
360 filename);
361 }
362
363 res = dmWriteRAWImageFILE(fp, img, spec);
364
365 dmf_close(fp);
366 return res;
367 }
368
369
370 static int dmWritePPMRow(void *cbdata, const Uint8 *row, const size_t len) 399 static int dmWritePPMRow(void *cbdata, const Uint8 *row, const size_t len)
371 { 400 {
372 if (dmf_write_str((DMResource *) cbdata, row, len)) 401 if (dmf_write_str((DMResource *) cbdata, row, len))
373 return DMERR_OK; 402 return DMERR_OK;
374 else 403 else
375 return DMERR_FWRITE; 404 return DMERR_FWRITE;
376 } 405 }
377 406
378 407
379 int dmWritePPMImageFILE(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec) 408 int dmWritePPMImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
380 { 409 {
381 DMImageConvSpec tmpSpec; 410 DMImageConvSpec tmpSpec;
382 411
383 // Write PPM header 412 // Write PPM header
384 char *tmp = dm_strdup_printf( 413 char *tmp = dm_strdup_printf(
397 tmpSpec.format = DM_COLFMT_RGB; 426 tmpSpec.format = DM_COLFMT_RGB;
398 return dmWriteImageData(img, (void *) fp, dmWritePPMRow, &tmpSpec); 427 return dmWriteImageData(img, (void *) fp, dmWritePPMRow, &tmpSpec);
399 } 428 }
400 429
401 430
402 int dmWritePPMImage(const char *filename, const DMImage *img, const DMImageConvSpec *spec)
403 {
404 DMResource *fp;
405 int res;
406
407 // Create output file
408 if ((res = dmf_open_stdio(filename, "wb", &fp)) != DMERR_OK)
409 {
410 return dmError(res,
411 "PPM: could not open file '%s' for writing.\n",
412 filename);
413 }
414
415 res = dmWritePPMImageFILE(fp, img, spec);
416
417 dmf_close(fp);
418 return res;
419 }
420
421
422 #ifdef DM_USE_LIBPNG 431 #ifdef DM_USE_LIBPNG
423 static int fmtProbePNG(const Uint8 *buf, const size_t len) 432 static int fmtProbePNG(const Uint8 *buf, const size_t len)
424 { 433 {
425 if (len > 64 && buf[0] == 0x89 && 434 if (len > 64 && buf[0] == 0x89 &&
426 buf[1] == 'P' && buf[2] == 'N' && buf[3] == 'G' && 435 buf[1] == 'P' && buf[2] == 'N' && buf[3] == 'G' &&
464 { 473 {
465 (void) png_ptr; 474 (void) png_ptr;
466 } 475 }
467 476
468 477
469 int dmWritePNGImageFILE(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec) 478 int dmWritePNGImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
470 { 479 {
471 png_structp png_ptr = NULL; 480 png_structp png_ptr = NULL;
472 png_infop info_ptr = NULL; 481 png_infop info_ptr = NULL;
473 int fmt, res; 482 int fmt, res;
474 483
572 581
573 return res; 582 return res;
574 } 583 }
575 584
576 585
577 int dmWritePNGImage(const char *filename, const DMImage *img, const DMImageConvSpec *spec)
578 {
579 int res;
580 DMResource *fp;
581
582 if ((res = dmf_open_stdio(filename, "wb", &fp)) != DMERR_OK)
583 {
584 return dmError(res,
585 "PNG: could not open file '%s' for writing.\n",
586 filename);
587 }
588
589 res = dmWritePNGImageFILE(fp, img, spec);
590
591 dmf_close(fp);
592 return res;
593 }
594
595
596 void dmPNGReadData(png_structp png_ptr, png_bytep data, png_size_t length) 586 void dmPNGReadData(png_structp png_ptr, png_bytep data, png_size_t length)
597 { 587 {
598 DMResource *res = (DMResource *) png_get_io_ptr(png_ptr); 588 DMResource *res = (DMResource *) png_get_io_ptr(png_ptr);
599 589
600 // XXX TODO: How the fuck does one do error handling here? 590 // XXX TODO: How the fuck does one do error handling here?
601 dmf_read_str(res, data, length); 591 dmf_read_str(res, data, length);
602 } 592 }
603 593
604 int dmReadPNGImageFILE(DMResource *fp, DMImage **pimg) 594
595 int dmReadPNGImage(DMResource *fp, DMImage **pimg)
605 { 596 {
606 png_structp png_ptr = NULL; 597 png_structp png_ptr = NULL;
607 png_infop info_ptr = NULL; 598 png_infop info_ptr = NULL;
608 png_colorp palette = NULL; 599 png_colorp palette = NULL;
609 png_bytep *row_pointers = NULL; 600 png_bytep *row_pointers = NULL;
771 // png_free(png_ptr, palette); 762 // png_free(png_ptr, palette);
772 763
773 if (png_ptr && info_ptr) 764 if (png_ptr && info_ptr)
774 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); 765 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
775 766
776 return res;
777 }
778
779
780 int dmReadPNGImage(const char *filename, DMImage **img)
781 {
782 int res;
783 DMResource *fp;
784
785 if ((res = dmf_open_stdio(filename, "rb", &fp)) != DMERR_OK)
786 {
787 return dmError(res,
788 "PNG: Could not open file '%s' for reading.\n",
789 filename);
790 }
791
792 res = dmReadPNGImageFILE(fp, img);
793
794 dmf_close(fp);
795 return res; 767 return res;
796 } 768 }
797 #endif 769 #endif
798 770
799 771
942 914
943 return DMERR_OK; 915 return DMERR_OK;
944 } 916 }
945 917
946 918
947 int dmWritePCXImageFILE(DMResource *fp, const DMImage *img, const DMImageConvSpec *pspec) 919 int dmWritePCXImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *pspec)
948 { 920 {
949 DMPCXData pcx; 921 DMPCXData pcx;
950 DMPCXHeader hdr; 922 DMPCXHeader hdr;
951 DMImageConvSpec spec; 923 DMImageConvSpec spec;
952 int res; 924 int res;
1111 dmFree(pcx.buf); 1083 dmFree(pcx.buf);
1112 return res; 1084 return res;
1113 } 1085 }
1114 1086
1115 1087
1116 int dmWritePCXImage(const char *filename, const DMImage *img, const DMImageConvSpec *spec)
1117 {
1118 DMResource *fp;
1119 int res;
1120
1121 if ((res = dmf_open_stdio(filename, "wb", &fp)) != DMERR_OK)
1122 {
1123 return dmError(res,
1124 "PCX: Could not open file '%s' for writing.\n",
1125 filename);
1126 }
1127
1128 res = dmWritePCXImageFILE(fp, img, spec);
1129
1130 dmf_close(fp);
1131 return res;
1132 }
1133
1134
1135 static BOOL dmPCXDecodeRLERow(DMResource *fp, Uint8 *buf, const size_t bufLen) 1088 static BOOL dmPCXDecodeRLERow(DMResource *fp, Uint8 *buf, const size_t bufLen)
1136 { 1089 {
1137 size_t offs = 0; 1090 size_t offs = 0;
1138 do 1091 do
1139 { 1092 {
1186 1139
1187 return TRUE; 1140 return TRUE;
1188 } 1141 }
1189 1142
1190 1143
1191 int dmReadPCXImageFILE(DMResource *fp, DMImage **pimg) 1144 int dmReadPCXImage(DMResource *fp, DMImage **pimg)
1192 { 1145 {
1193 DMImage *img; 1146 DMImage *img;
1194 DMPCXData pcx; 1147 DMPCXData pcx;
1195 DMPCXHeader hdr; 1148 DMPCXHeader hdr;
1196 int res = 0; 1149 int res = 0;
1417 dmFree(pcx.buf); 1370 dmFree(pcx.buf);
1418 return res; 1371 return res;
1419 } 1372 }
1420 1373
1421 1374
1422 int dmReadPCXImage(const char *filename, DMImage **pimg)
1423 {
1424 DMResource *fp;
1425 int res;
1426
1427 if ((res = dmf_open_stdio(filename, "rb", &fp)) != DMERR_OK)
1428 {
1429 return dmError(res,
1430 "PCX: Could not open file '%s' for reading.\n",
1431 filename);
1432 }
1433
1434 res = dmReadPCXImageFILE(fp, pimg);
1435
1436 dmf_close(fp);
1437 return res;
1438 }
1439
1440
1441 // 1375 //
1442 // IFF ILBM / PBM format 1376 // IFF ILBM / PBM format
1443 // 1377 //
1444 #define IFF_ID_FORM 0x464F524D // "FORM" 1378 #define IFF_ID_FORM 0x464F524D // "FORM"
1445 #define IFF_ID_ILBM 0x494C424D // "ILBM" 1379 #define IFF_ID_ILBM 0x494C424D // "ILBM"
1742 error: 1676 error:
1743 return res; 1677 return res;
1744 } 1678 }
1745 1679
1746 1680
1747 int dmReadILBMImageFILE(DMResource *fp, DMImage **pimg) 1681 int dmReadILBMImage(DMResource *fp, DMImage **pimg)
1748 { 1682 {
1749 DMIFFChunk chunk; 1683 DMIFFChunk chunk;
1750 DMIFF iff; 1684 DMIFF iff;
1751 Uint32 read, idsig; 1685 Uint32 read, idsig;
1752 BOOL parsed = FALSE, planar; 1686 BOOL parsed = FALSE, planar;
1995 1929
1996 return res; 1930 return res;
1997 } 1931 }
1998 1932
1999 1933
2000 int dmReadILBMImage(const char *filename, DMImage **pimg)
2001 {
2002 DMResource *fp;
2003 int res;
2004
2005 if ((res = dmf_open_stdio(filename, "rb", &fp)) != DMERR_OK)
2006 {
2007 return dmError(res,
2008 "ILBM: Could not open file '%s' for reading.\n",
2009 filename);
2010 }
2011
2012 res = dmReadILBMImageFILE(fp, pimg);
2013
2014 dmf_close(fp);
2015 return res;
2016 }
2017
2018
2019 // 1934 //
2020 // List of formats 1935 // List of formats
2021 // 1936 //
2022 const DMImageFormat dmImageFormatList[] = 1937 const DMImageFormat dmImageFormatList[] =
2023 { 1938 {
2024 #ifdef DM_USE_LIBPNG 1939 #ifdef DM_USE_LIBPNG
2025 { 1940 {
2026 "png", "Portable Network Graphics", 1941 "png", "Portable Network Graphics",
2027 DM_IMGFMT_PNG, DM_FMT_RDWR, 1942 DM_IMGFMT_PNG, DM_FMT_RDWR,
2028 fmtProbePNG, 1943 fmtProbePNG, dmReadPNGImage, dmWritePNGImage,
2029 dmReadPNGImage, dmReadPNGImageFILE,
2030 dmWritePNGImage, dmWritePNGImageFILE,
2031 }, 1944 },
2032 #endif 1945 #endif
2033 { 1946 {
2034 "ppm", "Portable PixMap", 1947 "ppm", "Portable PixMap",
2035 DM_IMGFMT_PPM, DM_FMT_WR, 1948 DM_IMGFMT_PPM, DM_FMT_WR,
2036 NULL, 1949 NULL, NULL, dmWritePPMImage,
2037 NULL, NULL,
2038 dmWritePPMImage, dmWritePPMImageFILE,
2039 }, 1950 },
2040 { 1951 {
2041 "pcx", "Z-Soft Paintbrush", 1952 "pcx", "Z-Soft Paintbrush",
2042 DM_IMGFMT_PCX, DM_FMT_RDWR, 1953 DM_IMGFMT_PCX, DM_FMT_RDWR,
2043 fmtProbePCX, 1954 fmtProbePCX, dmReadPCXImage, dmWritePCXImage,
2044 dmReadPCXImage, dmReadPCXImageFILE,
2045 dmWritePCXImage, dmWritePCXImageFILE,
2046 }, 1955 },
2047 { 1956 {
2048 "ilbm", "IFF ILBM / PBM", 1957 "ilbm", "IFF ILBM / PBM",
2049 DM_IMGFMT_ILBM, DM_FMT_RD, 1958 DM_IMGFMT_ILBM, DM_FMT_RDWR,
2050 fmtProbeILBM, 1959 fmtProbeILBM, dmReadILBMImage,
2051 dmReadILBMImage, dmReadILBMImageFILE, 1960 NULL,
2052 NULL, NULL,
2053 }, 1961 },
2054 { 1962 {
2055 "raw", "Plain bitplaned (planar or non-planar) RAW", 1963 "raw", "Plain bitplaned (planar or non-planar) RAW",
2056 DM_IMGFMT_RAW, DM_FMT_WR, 1964 DM_IMGFMT_RAW, DM_FMT_WR,
2057 NULL, 1965 NULL, NULL, dmWriteRAWImage,
2058 NULL, NULL,
2059 dmWriteRAWImage, dmWriteRAWImageFILE,
2060 }, 1966 },
2061 { 1967 {
2062 "araw", "IFFMaster Amiga RAW", 1968 "araw", "IFFMaster Amiga RAW",
2063 DM_IMGFMT_ARAW, DM_FMT_WR, 1969 DM_IMGFMT_ARAW, DM_FMT_WR,
2064 NULL, 1970 NULL, NULL, dmWriteRAWImage,
2065 NULL, NULL,
2066 dmWriteRAWImage, dmWriteRAWImageFILE,
2067 } 1971 }
2068 }; 1972 };
2069 1973
2070 const int ndmImageFormatList = sizeof(dmImageFormatList) / sizeof(dmImageFormatList[0]); 1974 const int ndmImageFormatList = sizeof(dmImageFormatList) / sizeof(dmImageFormatList[0]);
2071 1975