comparison gfxconv.c @ 409:b529b7e8ff83

Various improvements and cruft cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 03 Nov 2012 02:40:07 +0200
parents 59244a7ae37f
children d94f4bcb4be3
comparison
equal deleted inserted replaced
408:37e65cdcc749 409:b529b7e8ff83
349 } 349 }
350 } 350 }
351 } 351 }
352 352
353 353
354 void dmDumpCharASCII(FILE *outFile, const uint8_t *buf, int *offs, int format, BOOL multicolor) 354 void dmDumpCharASCII(FILE *outFile, const Uint8 *buf, int *offs, int format, BOOL multicolor)
355 { 355 {
356 int yc; 356 int yc;
357 357
358 for (yc = 0; yc < C64_CHR_HEIGHT; yc++) 358 for (yc = 0; yc < C64_CHR_HEIGHT; yc++)
359 { 359 {
363 (*offs)++; 363 (*offs)++;
364 } 364 }
365 } 365 }
366 366
367 367
368 void dmDumpSpriteASCII(FILE *outFile, const uint8_t *buf, int *offs, int format, BOOL multicolor) 368 void dmDumpSpriteASCII(FILE *outFile, const Uint8 *buf, int *offs, int format, BOOL multicolor)
369 { 369 {
370 int bufOffs, xc, yc; 370 int bufOffs, xc, yc;
371 371
372 for (bufOffs = yc = 0; yc < C64_SPR_HEIGHT; yc++) 372 for (bufOffs = yc = 0; yc < C64_SPR_HEIGHT; yc++)
373 { 373 {
383 } 383 }
384 (*offs)++; 384 (*offs)++;
385 } 385 }
386 386
387 387
388 int dmWriteImageData(DMImage *img, void *cbdata, BOOL (*writeRowCB)(void *, uint8_t *, size_t), int scale, int format) 388 int dmWriteImageData(DMImage *img, void *cbdata, BOOL (*writeRowCB)(void *, Uint8 *, size_t), int scale, int format)
389 { 389 {
390 int x, y, yscale, xscale, res = 0, rowSize, rowWidth; 390 int x, y, yscale, xscale, res = 0, rowSize, rowWidth;
391 uint8_t *row = NULL; 391 Uint8 *row = NULL;
392 392
393 // Allocate memory for row buffer 393 // Allocate memory for row buffer
394 rowWidth = img->width * scale; 394 rowWidth = img->width * scale;
395 rowSize = rowWidth * dmImageGetBytesPerPixel(format); 395 rowSize = rowWidth * dmImageGetBytesPerPixel(format);
396 396
397 if ((row = dmMalloc(rowSize + 16)) == NULL) 397 if ((row = dmMalloc(rowSize + 16)) == NULL)
398 { 398 {
399 res = -16; 399 res = DMERR_MALLOC;
400 goto done; 400 goto done;
401 } 401 }
402 402
403 // Generate the image 403 // Generate the image
404 for (y = 0; y < img->height; y++) 404 for (y = 0; y < img->height; y++)
405 { 405 {
406 uint8_t *ptr = row, 406 Uint8 *ptr = row,
407 *ptr1 = row, 407 *ptr1 = row,
408 *ptr2 = ptr1 + rowWidth, 408 *ptr2 = ptr1 + rowWidth,
409 *ptr3 = ptr2 + rowWidth; 409 *ptr3 = ptr2 + rowWidth;
410 410
411 for (x = 0; x < img->width; x++) 411 for (x = 0; x < img->width; x++)
412 { 412 {
413 uint8_t c = img->data[(y * img->pitch) + x], qr, qg, qb, qa; 413 Uint8 c = img->data[(y * img->pitch) + x], qr, qg, qb, qa;
414 switch (format) 414 switch (format)
415 { 415 {
416 case DM_IFMT_PALETTE: 416 case DM_IFMT_PALETTE:
417 for (xscale = 0; xscale < scale; xscale++) 417 for (xscale = 0; xscale < scale; xscale++)
418 *ptr++ = c; 418 *ptr++ = c;
463 463
464 for (yscale = 0; yscale < scale; yscale++) 464 for (yscale = 0; yscale < scale; yscale++)
465 { 465 {
466 if (!writeRowCB(cbdata, row, rowSize)) 466 if (!writeRowCB(cbdata, row, rowSize))
467 { 467 {
468 res = -32; 468 res = DMERR_FWRITE;
469 goto done; 469 goto done;
470 } 470 }
471 } 471 }
472 } 472 }
473 473
515 DMImage *img; 515 DMImage *img;
516 FILE *fp; 516 FILE *fp;
517 } DMRawData; 517 } DMRawData;
518 518
519 519
520 static BOOL dmWriteIFFMasterRAWRow(void *cbdata, uint8_t *row, size_t len) 520 static BOOL dmWriteIFFMasterRAWRow(void *cbdata, Uint8 *row, size_t len)
521 { 521 {
522 DMRawData *raw = (DMRawData *) cbdata; 522 DMRawData *raw = (DMRawData *) cbdata;
523 size_t i; 523 size_t i;
524 524
525 for (i = 0; i < len; i++) 525 for (i = 0; i < len; i++)
526 { 526 {
527 } 527 }
528 528
529 return fwrite(row, sizeof(uint8_t), len, raw->fp) == len; 529 return fwrite(row, sizeof(Uint8), len, raw->fp) == len;
530 } 530 }
531 531
532 532
533 int dmWriteIFFMasterRAWImageFILE(FILE *fp, DMImage *img, int scale, int bpp) 533 int dmWriteIFFMasterRAWImageFILE(FILE *fp, DMImage *img, int scale, int bpp)
534 { 534 {
557 fclose(fp); 557 fclose(fp);
558 return res; 558 return res;
559 } 559 }
560 560
561 561
562 static BOOL dmWritePPMRow(void *cbdata, uint8_t *row, size_t len) 562 static BOOL dmWritePPMRow(void *cbdata, Uint8 *row, size_t len)
563 { 563 {
564 return fwrite(row, sizeof(uint8_t), len, (FILE *) cbdata) == len; 564 return fwrite(row, sizeof(Uint8), len, (FILE *) cbdata) == len;
565 } 565 }
566 566
567 567
568 int dmWritePPMImageFILE(FILE *fp, DMImage *img, int scale) 568 int dmWritePPMImageFILE(FILE *fp, DMImage *img, int scale)
569 { 569 {
595 return res; 595 return res;
596 } 596 }
597 597
598 598
599 #ifdef HAVE_LIBPNG 599 #ifdef HAVE_LIBPNG
600 static BOOL dmWritePNGRow(void *cbdata, uint8_t *row, size_t len) 600 static BOOL dmWritePNGRow(void *cbdata, Uint8 *row, size_t len)
601 { 601 {
602 png_structp png_ptr = cbdata; 602 png_structp png_ptr = cbdata;
603 (void) len; 603 (void) len;
604 604
605 if (setjmp(png_jmpbuf(png_ptr))) 605 if (setjmp(png_jmpbuf(png_ptr)))
741 #endif 741 #endif
742 742
743 743
744 typedef struct 744 typedef struct
745 { 745 {
746 uint8_t r,g,b; 746 Uint8 r,g,b;
747 } DMPCXColor; 747 } DMPCXColor;
748 748
749 749
750 typedef struct 750 typedef struct
751 { 751 {
752 uint8_t manufacturer, 752 Uint8 manufacturer,
753 version, 753 version,
754 encoding, 754 encoding,
755 bpp; 755 bpp;
756 uint16_t xmin, ymin, xmax, ymax; 756 Uint16 xmin, ymin, xmax, ymax;
757 uint16_t hres, vres; 757 Uint16 hres, vres;
758 DMPCXColor colormap[16]; 758 DMPCXColor colormap[16];
759 uint8_t reserved; 759 Uint8 reserved;
760 uint8_t nplanes; 760 Uint8 nplanes;
761 uint16_t bpl; 761 Uint16 bpl;
762 uint16_t palinfo; 762 Uint16 palinfo;
763 uint8_t filler[58]; 763 Uint8 filler[58];
764 } DMPCXHeader; 764 } DMPCXHeader;
765 765
766 typedef struct 766 typedef struct
767 { 767 {
768 DMPCXHeader *header; 768 DMPCXHeader *header;
769 uint8_t *buf; 769 Uint8 *buf;
770 size_t bufLen, bufOffs; 770 size_t bufLen, bufOffs;
771 int format; 771 int format;
772 FILE *fp; 772 FILE *fp;
773 } DMPCXData; 773 } DMPCXData;
774 774
775 775
776 static inline uint8_t dmPCXGetByte(uint8_t *row, const size_t len, const size_t soffs) 776 static inline Uint8 dmPCXGetByte(Uint8 *row, const size_t len, const size_t soffs)
777 { 777 {
778 return (soffs < len) ? row[soffs] : 0; 778 return (soffs < len) ? row[soffs] : 0;
779 } 779 }
780 780
781 static BOOL dmPCXFlush(DMPCXData *pcx) 781 static BOOL dmPCXFlush(DMPCXData *pcx)
782 { 782 {
783 BOOL ret = fwrite(pcx->buf, sizeof(uint8_t), pcx->bufOffs, pcx->fp) == pcx->bufOffs; 783 BOOL ret = fwrite(pcx->buf, sizeof(Uint8), pcx->bufOffs, pcx->fp) == pcx->bufOffs;
784 pcx->bufOffs = 0; 784 pcx->bufOffs = 0;
785 return ret; 785 return ret;
786 } 786 }
787 787
788 static inline BOOL dmPCXPutByte(DMPCXData *pcx, const uint8_t val) 788 static inline BOOL dmPCXPutByte(DMPCXData *pcx, const Uint8 val)
789 { 789 {
790 if (pcx->bufOffs < pcx->bufLen) 790 if (pcx->bufOffs < pcx->bufLen)
791 { 791 {
792 pcx->buf[pcx->bufOffs++] = val; 792 pcx->buf[pcx->bufOffs++] = val;
793 return TRUE; 793 return TRUE;
794 } 794 }
795 else 795 else
796 return dmPCXFlush(pcx); 796 return dmPCXFlush(pcx);
797 } 797 }
798 798
799 BOOL dmWritePCXRow(void *cbdata, uint8_t *row, size_t len) 799 BOOL dmWritePCXRow(void *cbdata, Uint8 *row, size_t len)
800 { 800 {
801 DMPCXData *pcx = (DMPCXData *) cbdata; 801 DMPCXData *pcx = (DMPCXData *) cbdata;
802 int plane; 802 int plane;
803 size_t soffs = 0; 803 size_t soffs = 0;
804 804
805 for (plane = 0; plane < pcx->header->nplanes; plane++) 805 for (plane = 0; plane < pcx->header->nplanes; plane++)
806 { 806 {
807 uint8_t data = dmPCXGetByte(row, len, soffs++), 807 Uint8 data = dmPCXGetByte(row, len, soffs++),
808 count = 1; 808 count = 1;
809 809
810 pcx->bufOffs = 0; 810 pcx->bufOffs = 0;
811 811
812 while (soffs < pcx->header->bpl) 812 while (soffs < pcx->header->bpl)
861 pcx.format = paletted ? DM_IFMT_PALETTE : DM_IFMT_RGB_PLANE; 861 pcx.format = paletted ? DM_IFMT_PALETTE : DM_IFMT_RGB_PLANE;
862 pcx.header = &hdr; 862 pcx.header = &hdr;
863 if ((pcx.fp = fopen(filename, "wb")) == NULL) 863 if ((pcx.fp = fopen(filename, "wb")) == NULL)
864 { 864 {
865 dmError("PCX: Could not open file '%s' for writing.\n", filename); 865 dmError("PCX: Could not open file '%s' for writing.\n", filename);
866 res = -15; 866 res = DMERR_FOPEN;
867 goto error; 867 goto error;
868 } 868 }
869 869
870 // Create PCX header 870 // Create PCX header
871 memset(&hdr, 0, sizeof(hdr)); 871 memset(&hdr, 0, sizeof(hdr));
898 pcx.bufLen = hdr.bpl * 4; 898 pcx.bufLen = hdr.bpl * 4;
899 if ((pcx.buf = dmMalloc(pcx.bufLen)) == NULL) 899 if ((pcx.buf = dmMalloc(pcx.bufLen)) == NULL)
900 { 900 {
901 dmError("PCX: Could not allocate %d bytes for RLE compression buffer.\n", 901 dmError("PCX: Could not allocate %d bytes for RLE compression buffer.\n",
902 pcx.bufLen); 902 pcx.bufLen);
903 res = -11; 903 res = DMERR_MALLOC;
904 goto error; 904 goto error;
905 } 905 }
906 906
907 // Write PCX header 907 // Write PCX header
908 if (!dm_fwrite_byte(pcx.fp, hdr.manufacturer) || 908 if (!dm_fwrite_byte(pcx.fp, hdr.manufacturer) ||
909 !dm_fwrite_byte(pcx.fp, hdr.version) || 909 !dm_fwrite_byte(pcx.fp, hdr.version) ||
910 !dm_fwrite_byte(pcx.fp, hdr.encoding) || 910 !dm_fwrite_byte(pcx.fp, hdr.encoding) ||
911 !dm_fwrite_byte(pcx.fp, hdr.bpp)) 911 !dm_fwrite_byte(pcx.fp, hdr.bpp))
912 { 912 {
913 dmError("PCX: Could not write basic header data.\n"); 913 dmError("PCX: Could not write basic header data.\n");
914 res = -10; 914 res = DMERR_FWRITE;
915 goto error; 915 goto error;
916 } 916 }
917 917
918 if (!dm_fwrite_le16(pcx.fp, hdr.xmin) || 918 if (!dm_fwrite_le16(pcx.fp, hdr.xmin) ||
919 !dm_fwrite_le16(pcx.fp, hdr.ymin) || 919 !dm_fwrite_le16(pcx.fp, hdr.ymin) ||
921 !dm_fwrite_le16(pcx.fp, hdr.ymax) || 921 !dm_fwrite_le16(pcx.fp, hdr.ymax) ||
922 !dm_fwrite_le16(pcx.fp, hdr.hres) || 922 !dm_fwrite_le16(pcx.fp, hdr.hres) ||
923 !dm_fwrite_le16(pcx.fp, hdr.vres)) 923 !dm_fwrite_le16(pcx.fp, hdr.vres))
924 { 924 {
925 dmError("PCX: Could not write image dimensions.\n"); 925 dmError("PCX: Could not write image dimensions.\n");
926 res = -9; 926 res = DMERR_FWRITE;
927 goto error; 927 goto error;
928 } 928 }
929 929
930 if (!dm_fwrite_str(pcx.fp, (uint8_t *) &hdr.colormap, sizeof(hdr.colormap))) 930 if (!dm_fwrite_str(pcx.fp, (Uint8 *) &hdr.colormap, sizeof(hdr.colormap)))
931 { 931 {
932 dmError("PCX: Could not write colormap.\n"); 932 dmError("PCX: Could not write colormap.\n");
933 res = -8; 933 res = DMERR_FWRITE;
934 goto error; 934 goto error;
935 } 935 }
936 936
937 if (!dm_fwrite_byte(pcx.fp, hdr.reserved) || 937 if (!dm_fwrite_byte(pcx.fp, hdr.reserved) ||
938 !dm_fwrite_byte(pcx.fp, hdr.nplanes) || 938 !dm_fwrite_byte(pcx.fp, hdr.nplanes) ||
939 !dm_fwrite_le16(pcx.fp, hdr.bpl) || 939 !dm_fwrite_le16(pcx.fp, hdr.bpl) ||
940 !dm_fwrite_le16(pcx.fp, hdr.palinfo) || 940 !dm_fwrite_le16(pcx.fp, hdr.palinfo) ||
941 !dm_fwrite_str(pcx.fp, (uint8_t *) &hdr.filler, sizeof(hdr.filler))) 941 !dm_fwrite_str(pcx.fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler)))
942 { 942 {
943 dmError("PCX: Could not write header remainder.\n"); 943 dmError("PCX: Could not write header remainder.\n");
944 res = -7; 944 res = DMERR_FWRITE;
945 goto error; 945 goto error;
946 } 946 }
947 947
948 // Write image data 948 // Write image data
949 res = dmWriteImageData(img, (void *) &pcx, dmWritePCXRow, scale, pcx.format); 949 res = dmWriteImageData(img, (void *) &pcx, dmWritePCXRow, scale, pcx.format);
978 978
979 return res; 979 return res;
980 } 980 }
981 981
982 982
983 static BOOL dmPCXDecodeRLERow(FILE *fp, uint8_t *buf, const size_t bufLen) 983 static BOOL dmPCXDecodeRLERow(FILE *fp, Uint8 *buf, const size_t bufLen)
984 { 984 {
985 size_t offs = 0; 985 size_t offs = 0;
986 do 986 do
987 { 987 {
988 int count; 988 int count;
989 uint8_t data; 989 Uint8 data;
990 990
991 if (!dm_fread_byte(fp, &data)) 991 if (!dm_fread_byte(fp, &data))
992 return FALSE; 992 return FALSE;
993 993
994 if ((data & 0xC0) == 0xC0) 994 if ((data & 0xC0) == 0xC0)
1014 DMImage *img; 1014 DMImage *img;
1015 DMPCXData pcx; 1015 DMPCXData pcx;
1016 DMPCXHeader hdr; 1016 DMPCXHeader hdr;
1017 BOOL paletted; 1017 BOOL paletted;
1018 int res = 0, yc, xc; 1018 int res = 0, yc, xc;
1019 uint8_t *dp; 1019 Uint8 *dp;
1020 1020
1021 pcx.buf = NULL; 1021 pcx.buf = NULL;
1022 1022
1023 // Read PCX header 1023 // Read PCX header
1024 if (!dm_fread_byte(fp, &hdr.manufacturer) || 1024 if (!dm_fread_byte(fp, &hdr.manufacturer) ||
1025 !dm_fread_byte(fp, &hdr.version) || 1025 !dm_fread_byte(fp, &hdr.version) ||
1026 !dm_fread_byte(fp, &hdr.encoding) || 1026 !dm_fread_byte(fp, &hdr.encoding) ||
1027 !dm_fread_byte(fp, &hdr.bpp)) 1027 !dm_fread_byte(fp, &hdr.bpp))
1028 { 1028 {
1029 dmError("PCX: Could not read basic header data.\n"); 1029 dmError("PCX: Could not read basic header data.\n");
1030 res = -9; 1030 res = DMERR_FREAD;
1031 goto error;
1031 } 1032 }
1032 1033
1033 if (hdr.manufacturer != 10 || 1034 if (hdr.manufacturer != 10 ||
1034 hdr.version != 5 || 1035 hdr.version != 5 ||
1035 hdr.encoding != 1 || 1036 hdr.encoding != 1 ||
1036 hdr.bpp != 8) 1037 hdr.bpp != 8)
1037 { 1038 {
1038 dmError("PCX: Not a PCX file, or unsupported variant.\n"); 1039 dmError("PCX: Not a PCX file, or unsupported variant.\n");
1039 res = -11; 1040 res = DMERR_FREAD;
1040 goto error; 1041 goto error;
1041 } 1042 }
1042 1043
1043 if (!dm_fread_le16(fp, &hdr.xmin) || 1044 if (!dm_fread_le16(fp, &hdr.xmin) ||
1044 !dm_fread_le16(fp, &hdr.ymin) || 1045 !dm_fread_le16(fp, &hdr.ymin) ||
1046 !dm_fread_le16(fp, &hdr.ymax) || 1047 !dm_fread_le16(fp, &hdr.ymax) ||
1047 !dm_fread_le16(fp, &hdr.hres) || 1048 !dm_fread_le16(fp, &hdr.hres) ||
1048 !dm_fread_le16(fp, &hdr.vres)) 1049 !dm_fread_le16(fp, &hdr.vres))
1049 { 1050 {
1050 dmError("PCX: Could not read image dimensions.\n"); 1051 dmError("PCX: Could not read image dimensions.\n");
1051 res = -8; 1052 res = DMERR_FREAD;
1052 goto error; 1053 goto error;
1053 } 1054 }
1054 1055
1055 if (!dm_fread_str(fp, (uint8_t *) &hdr.colormap, sizeof(hdr.colormap))) 1056 if (!dm_fread_str(fp, (Uint8 *) &hdr.colormap, sizeof(hdr.colormap)))
1056 { 1057 {
1057 dmError("PCX: Could not read colormap.\n"); 1058 dmError("PCX: Could not read colormap.\n");
1058 res = -7; 1059 res = DMERR_FREAD;
1059 goto error; 1060 goto error;
1060 } 1061 }
1061 1062
1062 if (!dm_fread_byte(fp, &hdr.reserved) || 1063 if (!dm_fread_byte(fp, &hdr.reserved) ||
1063 !dm_fread_byte(fp, &hdr.nplanes) || 1064 !dm_fread_byte(fp, &hdr.nplanes) ||
1064 !dm_fread_le16(fp, &hdr.bpl) || 1065 !dm_fread_le16(fp, &hdr.bpl) ||
1065 !dm_fread_le16(fp, &hdr.palinfo) || 1066 !dm_fread_le16(fp, &hdr.palinfo) ||
1066 !dm_fread_str(fp, (uint8_t *) &hdr.filler, sizeof(hdr.filler))) 1067 !dm_fread_str(fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler)))
1067 { 1068 {
1068 dmError("PCX: Could not read header remainder.\n"); 1069 dmError("PCX: Could not read header remainder.\n");
1069 res = -6; 1070 res = DMERR_FREAD;
1070 goto error; 1071 goto error;
1071 } 1072 }
1072 1073
1073 if (hdr.nplanes != 3 && hdr.nplanes != 1) 1074 if (hdr.nplanes != 3 && hdr.nplanes != 1)
1074 { 1075 {
1075 dmError("PCX: Unsupported number of bitplanes %d.\n", hdr.nplanes); 1076 dmError("PCX: Unsupported number of bitplanes %d.\n", hdr.nplanes);
1076 res = -4; 1077 res = DMERR_FREAD;
1077 goto error; 1078 goto error;
1078 } 1079 }
1079 1080
1080 // Allocate image 1081 // Allocate image
1081 if ((*pimg = img = dmImageAlloc(hdr.xmax - hdr.xmin + 1, hdr.ymax - hdr.ymin + 1)) == NULL) 1082 if ((*pimg = img = dmImageAlloc(hdr.xmax - hdr.xmin + 1, hdr.ymax - hdr.ymin + 1)) == NULL)
1082 { 1083 {
1083 dmError("PCX: Could not allocate image structure.\n"); 1084 dmError("PCX: Could not allocate image structure.\n");
1084 res = -5; 1085 res = DMERR_MALLOC;
1085 goto error; 1086 goto error;
1086 } 1087 }
1087 1088
1088 paletted = hdr.nplanes == 1; 1089 paletted = hdr.nplanes == 1;
1089 pcx.bufLen = hdr.nplanes * hdr.bpl; 1090 pcx.bufLen = hdr.nplanes * hdr.bpl;
1090 if ((pcx.buf = dmMalloc(pcx.bufLen)) == NULL) 1091 if ((pcx.buf = dmMalloc(pcx.bufLen)) == NULL)
1091 { 1092 {
1092 dmError("PCX: Could not allocate RLE buffer.\n"); 1093 dmError("PCX: Could not allocate RLE buffer.\n");
1093 res = -3; 1094 res = DMERR_MALLOC;
1094 goto error; 1095 goto error;
1095 } 1096 }
1096 1097
1097 // Read image data 1098 // Read image data
1098 dp = img->data; 1099 dp = img->data;
1100 { 1101 {
1101 // Decode row of RLE'd data 1102 // Decode row of RLE'd data
1102 if (!dmPCXDecodeRLERow(fp, pcx.buf, pcx.bufLen)) 1103 if (!dmPCXDecodeRLERow(fp, pcx.buf, pcx.bufLen))
1103 { 1104 {
1104 dmError("PCX: Error decoding RLE data.\n"); 1105 dmError("PCX: Error decoding RLE data.\n");
1105 res = -100; 1106 res = DMERR_INVALID_DATA;
1106 goto error; 1107 goto error;
1107 } 1108 }
1108 1109
1109 // Decode bitplanes 1110 // Decode bitplanes
1110 switch (hdr.nplanes) 1111 switch (hdr.nplanes)
1113 memcpy(dp, pcx.buf, img->width); 1114 memcpy(dp, pcx.buf, img->width);
1114 break; 1115 break;
1115 1116
1116 case 3: 1117 case 3:
1117 { 1118 {
1118 uint8_t *dptr = dp, 1119 Uint8 *dptr = dp,
1119 *sptr1 = pcx.buf, 1120 *sptr1 = pcx.buf,
1120 *sptr2 = sptr1 + hdr.bpl, 1121 *sptr2 = sptr1 + hdr.bpl,
1121 *sptr3 = sptr2 + hdr.bpl; 1122 *sptr3 = sptr2 + hdr.bpl;
1122 1123
1123 for (xc = 0; xc < img->width; xc++) 1124 for (xc = 0; xc < img->width; xc++)
1135 1136
1136 // Read VGA palette 1137 // Read VGA palette
1137 if (paletted) 1138 if (paletted)
1138 { 1139 {
1139 int i; 1140 int i;
1140 uint8_t tmpb; 1141 Uint8 tmpb;
1142 BOOL read;
1141 1143
1142 if (!dm_fread_byte(fp, &tmpb) || tmpb != 0x0C) 1144 if (!dm_fread_byte(fp, &tmpb) || tmpb != 0x0C)
1145 {
1146 read = FALSE;
1147 img->ncolors = 16;
1148 }
1149 else
1150 {
1151 read = TRUE;
1152 img->ncolors = 256;
1153 }
1154
1155 if ((img->pal = dmCalloc(img->ncolors, sizeof(DMColor))) == NULL)
1156 {
1157 dmError("PCX: Could not allocate palette data!\n");
1158 res = DMERR_MALLOC;
1143 goto error; 1159 goto error;
1144 1160 }
1145 for (i = 0; i < img->ncolors; i++) 1161
1146 { 1162 if (read)
1147 if (!dm_fread_byte(fp, &tmpb)) 1163 {
1148 goto error; 1164 for (i = 0; i < img->ncolors; i++)
1149 img->pal[i].r = tmpb; 1165 {
1150 1166 Uint8 tmpR, tmpG, tmpB;
1151 if (!dm_fread_byte(fp, &tmpb)) 1167 if (!dm_fread_byte(fp, &tmpR) ||
1152 goto error; 1168 !dm_fread_byte(fp, &tmpG) ||
1153 img->pal[i].g = tmpb; 1169 !dm_fread_byte(fp, &tmpB))
1154 1170 goto error;
1155 if (!dm_fread_byte(fp, &tmpb)) 1171
1156 goto error; 1172 img->pal[i].r = tmpR;
1157 img->pal[i].b = tmpb; 1173 img->pal[i].g = tmpG;
1158 } 1174 img->pal[i].b = tmpB;
1175 }
1176 }
1177 else
1178 {
1179 for (i = 0; i < img->ncolors; i++)
1180 {
1181 if (i < 16)
1182 {
1183 img->pal[i].r = hdr.colormap[i].r;
1184 img->pal[i].g = hdr.colormap[i].g;
1185 img->pal[i].b = hdr.colormap[i].b;
1186 }
1187 }
1188 }
1189
1190
1159 } 1191 }
1160 1192
1161 error: 1193 error:
1162 dmFree(pcx.buf); 1194 dmFree(pcx.buf);
1163 return res; 1195 return res;
1182 } 1214 }
1183 1215
1184 1216
1185 int fmtProbePNGImageFILE(FILE *fp) 1217 int fmtProbePNGImageFILE(FILE *fp)
1186 { 1218 {
1187 uint8_t buf[6]; 1219 Uint8 buf[6];
1188 // if (!dm_fread_str(fp, 1220 // if (!dm_fread_str(fp,
1189 return DM_PROBE_SCORE_FALSE; 1221 return DM_PROBE_SCORE_FALSE;
1190 } 1222 }
1191 1223
1192 1224
1212 1244
1213 #ifdef UNFINISHED 1245 #ifdef UNFINISHED
1214 int dmConvertBMP2(DMImage *screen, const DM64Image *img) 1246 int dmConvertBMP2(DMImage *screen, const DM64Image *img)
1215 { 1247 {
1216 int yc; 1248 int yc;
1217 uint8_t *dp = screen->data; 1249 Uint8 *dp = screen->data;
1218 1250
1219 for (yc = 0; yc < screen->height; yc++) 1251 for (yc = 0; yc < screen->height; yc++)
1220 { 1252 {
1221 uint8_t *d = dp; 1253 Uint8 *d = dp;
1222 const int y = yc / 8, yb = yc & 7; 1254 const int y = yc / 8, yb = yc & 7;
1223 const int scroffsy = y * C64_SCR_CH_WIDTH; 1255 const int scroffsy = y * C64_SCR_CH_WIDTH;
1224 const int bmoffsy = y * C64_SCR_WIDTH; 1256 const int bmoffsy = y * C64_SCR_WIDTH;
1225 int xc; 1257 int xc;
1226 1258
1228 { 1260 {
1229 const int x = xc / 4; 1261 const int x = xc / 4;
1230 const int scroffs = scroffsy + x; 1262 const int scroffs = scroffsy + x;
1231 const int b = img->bitmap[0][bmoffsy + (x * 8) + yb]; 1263 const int b = img->bitmap[0][bmoffsy + (x * 8) + yb];
1232 const int v = 6 - ((xc * 2) & 6); 1264 const int v = 6 - ((xc * 2) & 6);
1233 uint8_t c; 1265 Uint8 c;
1234 1266
1235 switch ((b >> v) & 3) 1267 switch ((b >> v) & 3)
1236 { 1268 {
1237 case 0: c = img->bgcolor; break; 1269 case 0: c = img->bgcolor; break;
1238 case 1: c = img->screen[0][scroffs] >> 4; break; 1270 case 1: c = img->screen[0][scroffs] >> 4; break;
1271 { 1303 {
1272 int res; 1304 int res;
1273 char *palFilename = dm_strdup_printf("%s.pal", filename); 1305 char *palFilename = dm_strdup_printf("%s.pal", filename);
1274 res = dmWriteIFFMasterRAWPalette(palFilename, image, 1 << bpp); 1306 res = dmWriteIFFMasterRAWPalette(palFilename, image, 1 << bpp);
1275 dmFree(palFilename); 1307 dmFree(palFilename);
1276 if (res != 0) 1308 if (res != DMERR_OK)
1277 return res; 1309 return res;
1278 1310
1279 return dmWriteIFFMasterRAWImage(filename, image, scale, bpp); 1311 return dmWriteIFFMasterRAWImage(filename, image, scale, bpp);
1280 } 1312 }
1281 1313
1287 1319
1288 int dmDumpSpritesAndChars(FILE *inFile) 1320 int dmDumpSpritesAndChars(FILE *inFile)
1289 { 1321 {
1290 int dataOffs, itemCount, outWidth, outWidthPX, outHeight; 1322 int dataOffs, itemCount, outWidth, outWidthPX, outHeight;
1291 size_t bufSize; 1323 size_t bufSize;
1292 uint8_t *bufData; 1324 Uint8 *bufData;
1293 1325
1294 switch (optInFormat) 1326 switch (optInFormat)
1295 { 1327 {
1296 case INFMT_CHAR: 1328 case INFMT_CHAR:
1297 bufSize = C64_CHR_SIZE; 1329 bufSize = C64_CHR_SIZE;
1368 { 1400 {
1369 DMImage *outImage = NULL; 1401 DMImage *outImage = NULL;
1370 char *outFilename = NULL; 1402 char *outFilename = NULL;
1371 int outX = 0, outY = 0, err; 1403 int outX = 0, outY = 0, err;
1372 1404
1373 #ifndef HAVE_LIBPNG
1374 if (optOutFormat == OUTFMT_PNG)
1375 {
1376 dmError("PNG output format support not compiled in, sorry.\n");
1377 goto error;
1378 }
1379 #endif
1380
1381 if (optSequential) 1405 if (optSequential)
1382 { 1406 {
1383 if (optOutFilename == NULL) 1407 if (optOutFilename == NULL)
1384 { 1408 {
1385 dmError("Sequential image output requires filename template.\n"); 1409 dmError("Sequential image output requires filename template.\n");
1427 bufSize, dataOffs); 1451 bufSize, dataOffs);
1428 break; 1452 break;
1429 } 1453 }
1430 1454
1431 if ((err = dmC64ConvertCSData(outImage, outX * outWidthPX, outY * outHeight, 1455 if ((err = dmC64ConvertCSData(outImage, outX * outWidthPX, outY * outHeight,
1432 bufData, outWidth, outHeight, optInMulticolor, optColors)) != 0) 1456 bufData, outWidth, outHeight, optInMulticolor, optColors)) != DMERR_OK)
1433 { 1457 {
1434 dmError("Internal error in conversion of raw data to bitmap: %d.\n", err); 1458 dmError("Internal error in conversion of raw data to bitmap: %d.\n", err);
1435 break; 1459 break;
1436 } 1460 }
1437 1461
1489 dmInitProg("gfxconv", "Simple c64 graphics converter", "0.4", NULL, NULL); 1513 dmInitProg("gfxconv", "Simple c64 graphics converter", "0.4", NULL, NULL);
1490 1514
1491 if (!dmArgsProcess(argc, argv, optList, optListN, 1515 if (!dmArgsProcess(argc, argv, optList, optListN,
1492 argHandleOpt, argHandleFile, TRUE)) 1516 argHandleOpt, argHandleFile, TRUE))
1493 exit(1); 1517 exit(1);
1518
1519 #ifndef HAVE_LIBPNG
1520 if (optOutFormat == OUTFMT_PNG)
1521 {
1522 dmError("PNG output format support not compiled in, sorry.\n");
1523 exit(3);
1524 }
1525 #endif
1494 1526
1495 // Determine input format, if not specified' 1527 // Determine input format, if not specified'
1496 if (optInFormat == INFMT_AUTO && optInFilename != NULL) 1528 if (optInFormat == INFMT_AUTO && optInFilename != NULL)
1497 { 1529 {
1498 char *dext = strrchr(optInFilename, '.'); 1530 char *dext = strrchr(optInFilename, '.');
1607 break; 1639 break;
1608 case OUTFMT_PNG: 1640 case OUTFMT_PNG:
1609 // res = dmReadPNGImageFILE(inFile, &img); 1641 // res = dmReadPNGImageFILE(inFile, &img);
1610 break; 1642 break;
1611 } 1643 }
1644
1645 if (res != DMERR_OK)
1646 break;
1612 1647
1613 switch (optOutFormat) 1648 switch (optOutFormat)
1614 { 1649 {
1615 case OUTFMT_PCX: 1650 case OUTFMT_PCX:
1616 case OUTFMT_PPM: 1651 case OUTFMT_PPM: