Mercurial > hg > dmlib
comparison tools/gfxconv.c @ 1811:4f141426eb31
Clean up the image format output stuff in gfxconv.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 21 Jun 2018 13:01:57 +0300 |
parents | d53bdee5ffa5 |
children | 02f604264bc4 |
comparison
equal
deleted
inserted
replaced
1810:c9197a038e8e | 1811:4f141426eb31 |
---|---|
1064 dmGrowBufFree(&buf); | 1064 dmGrowBufFree(&buf); |
1065 return res; | 1065 return res; |
1066 } | 1066 } |
1067 | 1067 |
1068 | 1068 |
1069 void dmOutputImageBitFormat(const int format, const BOOL info) | 1069 #define DMCOL(x) (((x) >> 4) & 0xf) |
1070 { | 1070 |
1071 int dmWriteIFFMasterRAW(const char *filename, const char *prefix, const DMImage *img, const DMImageConvSpec *spec, const int fmtid) | |
1072 { | |
1073 // Open data file for writing | |
1074 FILE *fp = NULL; | |
1075 int res = DMERR_OK; | |
1076 | |
1077 if ((fp = fopen(filename, "w")) == NULL) | |
1078 { | |
1079 res = dmError(DMERR_FOPEN, | |
1080 "Could not create file '%s'.\n", filename); | |
1081 goto err; | |
1082 } | |
1083 | |
1084 | |
1085 fprintf(fp, | |
1086 "%s_width: dw.w %d\n" | |
1087 "%s_height: dw.w %d\n" | |
1088 "%s_nplanes: dw.w %d\n", | |
1089 prefix, img->width, | |
1090 prefix, img->height, | |
1091 prefix, spec->nplanes); | |
1092 | |
1093 if (fmtid == DM_IMGFMT_ARAW) | |
1094 { | |
1095 fprintf(fp, | |
1096 "%s_ncolors: dw.w %d\n" | |
1097 "%s_palette:\n", | |
1098 prefix, img->ncolors, | |
1099 prefix); | |
1100 | |
1101 for (int i = 0; i < (1 << spec->nplanes); i++) | |
1102 { | |
1103 Uint32 color; | |
1104 if (i < img->ncolors) | |
1105 { | |
1106 color = (DMCOL(img->pal[i].r) << 8) | | |
1107 (DMCOL(img->pal[i].g) << 4) | | |
1108 (DMCOL(img->pal[i].b)); | |
1109 } | |
1110 else | |
1111 color = 0; | |
1112 | |
1113 fprintf(fp, | |
1114 "\tdc.w $%04X\n", | |
1115 color); | |
1116 } | |
1117 | |
1118 fprintf(fp, | |
1119 "%s: incbin \"%s\"\n", | |
1120 prefix, filename); | |
1121 } | |
1122 | |
1123 err: | |
1124 if (fp != NULL) | |
1125 fclose(fp); | |
1126 | |
1127 return res; | |
1128 } | |
1129 | |
1130 | |
1131 int dmWriteImage(const char *filename, DMImage *pimage, DMImageConvSpec *spec, const DMImageFormat *fmt, BOOL info) | |
1132 { | |
1133 int res = DMERR_OK; | |
1134 | |
1135 // Check if writing is even supported | |
1136 if (fmt->write == NULL || (fmt->flags & DM_FMT_WR) == 0) | |
1137 { | |
1138 return dmError(DMERR_NOT_SUPPORTED, | |
1139 "Writing of '%s' format is not supported.\n", | |
1140 fmt->name); | |
1141 } | |
1142 | |
1071 if (info) | 1143 if (info) |
1072 { | 1144 { |
1073 char *str; | 1145 dmMsg(1, "Outputting '%s' image %d x %d -> %d x %d [%d x %d]\n", |
1074 switch (format) | 1146 fmt->fext, |
1075 { | |
1076 case DM_COLFMT_PALETTE : str = "Indexed 8bpp"; break; | |
1077 case DM_COLFMT_RGB : str = "24bit RGB"; break; | |
1078 case DM_COLFMT_RGBA : str = "32bit RGBA"; break; | |
1079 default : str = "???"; break; | |
1080 } | |
1081 dmMsg(2, "%s output.\n", str); | |
1082 } | |
1083 } | |
1084 | |
1085 | |
1086 #define DMCOL(x) (((x) >> 4) & 0xf) | |
1087 | |
1088 void dmWriteIFFMasterRAWPalette(FILE *fp, | |
1089 const DMImage *img, int ncolors, | |
1090 const char *indent, const char *type) | |
1091 { | |
1092 for (int i = 0; i < ncolors; i++) | |
1093 { | |
1094 int color; | |
1095 if (i < img->ncolors) | |
1096 { | |
1097 color = (DMCOL(img->pal[i].r) << 8) | | |
1098 (DMCOL(img->pal[i].g) << 4) | | |
1099 (DMCOL(img->pal[i].b)); | |
1100 } | |
1101 else | |
1102 color = 0; | |
1103 | |
1104 fprintf(fp, "%s%s $%04X\n", | |
1105 indent != NULL ? indent : "\t", | |
1106 type != NULL ? type : "dc.w", | |
1107 color); | |
1108 } | |
1109 } | |
1110 | |
1111 | |
1112 int dmWriteImage(const char *filename, DMImage *pimage, DMImageConvSpec *spec, int iformat, BOOL info) | |
1113 { | |
1114 int res = DMERR_OK; | |
1115 | |
1116 if (info) | |
1117 { | |
1118 dmMsg(1, "Outputting %s image %d x %d -> %d x %d [%d x %d]\n", | |
1119 dmImageFormatList[iformat].fext, | |
1120 pimage->width, pimage->height, | 1147 pimage->width, pimage->height, |
1121 pimage->width * spec->scaleX, pimage->height * spec->scaleY, | 1148 pimage->width * spec->scaleX, pimage->height * spec->scaleY, |
1122 spec->scaleX, spec->scaleY); | 1149 spec->scaleX, spec->scaleY); |
1123 } | 1150 } |
1124 | 1151 |
1125 // Perform color remapping | 1152 // Perform color remapping |
1126 DMImage *image = pimage; | 1153 DMImage *image = pimage; |
1127 BOOL allocated = FALSE; | 1154 BOOL allocated = FALSE; |
1128 if (optRemapColors) | 1155 if (optRemapColors) |
1129 { | 1156 { |
1130 int res; | |
1131 if ((res = dmRemapImageColors(&image, pimage)) != DMERR_OK) | 1157 if ((res = dmRemapImageColors(&image, pimage)) != DMERR_OK) |
1132 return res; | 1158 return res; |
1133 | 1159 |
1134 allocated = TRUE; | 1160 allocated = TRUE; |
1135 } | 1161 } |
1136 | 1162 |
1137 switch (iformat) | 1163 // Do some format-specific adjustments and other things |
1138 { | 1164 switch (fmt->fmtid) |
1139 #ifdef DM_USE_LIBPNG | 1165 { |
1140 case DM_IMGFMT_PNG: | 1166 case DM_IMGFMT_PNG: |
1141 spec->format = spec->paletted ? DM_COLFMT_PALETTE : DM_COLFMT_RGBA; | 1167 spec->format = spec->paletted ? DM_COLFMT_PALETTE : DM_COLFMT_RGBA; |
1142 dmOutputImageBitFormat(spec->format, info); | 1168 break; |
1143 res = dmWritePNGImage(filename, image, spec); | |
1144 break; | |
1145 #endif | |
1146 | 1169 |
1147 case DM_IMGFMT_PPM: | 1170 case DM_IMGFMT_PPM: |
1148 spec->format = DM_COLFMT_RGB; | 1171 spec->format = DM_COLFMT_RGB; |
1149 dmOutputImageBitFormat(spec->format, info); | |
1150 res = dmWritePPMImage(filename, image, spec); | |
1151 break; | |
1152 | |
1153 case DM_IMGFMT_PCX: | |
1154 spec->format = spec->paletted ? DM_COLFMT_PALETTE : DM_COLFMT_RGB; | |
1155 dmOutputImageBitFormat(spec->format, info); | |
1156 res = dmWritePCXImage(filename, image, spec); | |
1157 break; | 1172 break; |
1158 | 1173 |
1159 case DM_IMGFMT_RAW: | 1174 case DM_IMGFMT_RAW: |
1160 case DM_IMGFMT_ARAW: | 1175 case DM_IMGFMT_ARAW: |
1161 { | 1176 { |
1162 // Open data file for writing | 1177 char *prefix = NULL, *dataFilename = NULL; |
1163 FILE *fp; | 1178 if ((dataFilename = dm_strdup_fext(filename, "%s.inc")) == NULL || |
1164 char * dataFilename = dm_strdup_fext(filename, "%s.inc"); | 1179 (prefix = dm_strdup_fext(filename, "img_%s")) == NULL) |
1165 if ((fp = fopen(dataFilename, "w")) == NULL) | 1180 { |
1166 { | 1181 res = dmError(DMERR_MALLOC, |
1167 res = dmError(DMERR_FOPEN, | 1182 "Could not allocate memory for filename strings? :O\n"); |
1168 "Could not create '%s'.\n", dataFilename); | |
1169 goto err; | 1183 goto err; |
1170 } | 1184 } |
1171 | 1185 |
1172 dmFree(dataFilename); | 1186 // Replace any non-alphanumerics in palette ID |
1173 | 1187 for (int i = 0; prefix[i]; i++) |
1174 if (fp != NULL) | 1188 prefix[i] = isalnum(prefix[i]) ? tolower(prefix[i]) : '_'; |
1175 { | |
1176 // Strip extension | |
1177 char *palID = dm_strdup_fext(filename, "img_%s"); | |
1178 | |
1179 // Replace any non-alphanumerics | |
1180 for (int i = 0; palID[i]; i++) | |
1181 { | |
1182 if (isalnum(palID[i])) | |
1183 palID[i] = tolower(palID[i]); | |
1184 else | |
1185 palID[i] = '_'; | |
1186 } | |
1187 | |
1188 if (iformat == DM_IMGFMT_ARAW) | |
1189 { | |
1190 fprintf(fp, | |
1191 "%s_width: dw.w %d\n" | |
1192 "%s_height: dw.w %d\n" | |
1193 "%s_nplanes: dw.w %d\n" | |
1194 "%s_ncolors: dw.w %d\n" | |
1195 "%s_palette:\n", | |
1196 palID, image->width, | |
1197 palID, image->height, | |
1198 palID, spec->nplanes, | |
1199 palID, image->ncolors, | |
1200 palID); | |
1201 | |
1202 dmWriteIFFMasterRAWPalette(fp, image, 1 << optSpec.nplanes, NULL, NULL); | |
1203 | |
1204 fprintf(fp, | |
1205 "%s: incbin \"%s\"\n", | |
1206 palID, filename); | |
1207 } | |
1208 else | |
1209 { | |
1210 fprintf(fp, | |
1211 "%s_width: dw.w %d\n" | |
1212 "%s_height: dw.w %d\n" | |
1213 "%s_nplanes: dw.w %d\n", | |
1214 palID, image->width, | |
1215 palID, image->height, | |
1216 palID, spec->nplanes); | |
1217 } | |
1218 | |
1219 fclose(fp); | |
1220 dmFree(palID); | |
1221 } | |
1222 | 1189 |
1223 if (info) | 1190 if (info) |
1224 { | 1191 { |
1225 dmMsg(2, "%d bitplanes, %s planes.\n", | 1192 dmMsg(2, "%d bitplanes, %s planes output.\n", |
1226 spec->nplanes, | 1193 spec->nplanes, |
1227 spec->planar ? "planar/interleaved" : "non-interleaved"); | 1194 spec->planar ? "planar/interleaved" : "non-interleaved"); |
1228 } | 1195 dmMsg(2, "%s datafile '%s', ID prefix '%s'.\n", |
1229 res = dmWriteRAWImage(filename, image, spec); | 1196 fmt->fmtid == DM_IMGFMT_ARAW ? "ARAW" : "RAW", |
1197 dataFilename, prefix); | |
1198 } | |
1199 res = dmWriteIFFMasterRAW(dataFilename, prefix, image, spec, fmt->fmtid); | |
1200 | |
1201 dmFree(prefix); | |
1202 dmFree(dataFilename); | |
1230 } | 1203 } |
1231 break; | 1204 break; |
1232 | 1205 |
1233 default: | 1206 default: |
1234 res = DMERR_INVALID_DATA; | 1207 spec->format = spec->paletted ? DM_COLFMT_PALETTE : DM_COLFMT_RGB; |
1208 } | |
1209 | |
1210 // If no error has occured thus far, write the image | |
1211 if (res == DMERR_OK) | |
1212 { | |
1213 if (info) | |
1214 { | |
1215 char *str; | |
1216 switch (spec->format) | |
1217 { | |
1218 case DM_COLFMT_PALETTE : str = "indexed/paletted"; break; | |
1219 case DM_COLFMT_RGB : str = "24bit RGB"; break; | |
1220 case DM_COLFMT_RGBA : str = "32bit RGBA"; break; | |
1221 default : str = "???"; break; | |
1222 } | |
1223 dmMsg(2, "Using %s output.\n", str); | |
1224 } | |
1225 | |
1226 res = fmt->write(filename, image, spec); | |
1235 } | 1227 } |
1236 | 1228 |
1237 err: | 1229 err: |
1238 if (allocated) | 1230 if (allocated) |
1239 dmImageFree(image); | 1231 dmImageFree(image); |
1539 { | 1531 { |
1540 dmErrorMsg("Could not allocate memory for filename template?\n"); | 1532 dmErrorMsg("Could not allocate memory for filename template?\n"); |
1541 goto error; | 1533 goto error; |
1542 } | 1534 } |
1543 | 1535 |
1544 ret = dmWriteImage(outFilename, outImage, &optSpec, optOutSubFormat, TRUE); | 1536 ret = dmWriteImage(outFilename, outImage, &optSpec, |
1537 &dmImageFormatList[optOutSubFormat], TRUE); | |
1545 if (ret != DMERR_OK) | 1538 if (ret != DMERR_OK) |
1546 { | 1539 { |
1547 dmErrorMsg("Error writing output image '%s': %s.\n", | 1540 dmErrorMsg("Error writing output image '%s': %s.\n", |
1548 outFilename, dmErrorStr(ret)); | 1541 outFilename, dmErrorStr(ret)); |
1549 } | 1542 } |
1563 itemCount++; | 1556 itemCount++; |
1564 } | 1557 } |
1565 | 1558 |
1566 if (!optSequential) | 1559 if (!optSequential) |
1567 { | 1560 { |
1568 ret = dmWriteImage(optOutFilename, outImage, &optSpec, optOutSubFormat, TRUE); | 1561 ret = dmWriteImage(optOutFilename, outImage, &optSpec, |
1562 &dmImageFormatList[optOutSubFormat], TRUE); | |
1569 if (ret != DMERR_OK) | 1563 if (ret != DMERR_OK) |
1570 { | 1564 { |
1571 dmError(ret, "Error writing output image '%s': %s.\n", | 1565 dmError(ret, "Error writing output image '%s': %s.\n", |
1572 optOutFilename, dmErrorStr(ret)); | 1566 optOutFilename, dmErrorStr(ret)); |
1573 } | 1567 } |
1779 { | 1773 { |
1780 dmErrorMsg("Error in bitmap to image conversion.\n"); | 1774 dmErrorMsg("Error in bitmap to image conversion.\n"); |
1781 goto error; | 1775 goto error; |
1782 } | 1776 } |
1783 | 1777 |
1784 res = dmWriteImage(optOutFilename, outImage, &optSpec, optOutSubFormat, TRUE); | 1778 res = dmWriteImage(optOutFilename, outImage, &optSpec, |
1779 &dmImageFormatList[optOutSubFormat], TRUE); | |
1785 break; | 1780 break; |
1786 | 1781 |
1787 case FFMT_BITMAP: | 1782 case FFMT_BITMAP: |
1788 if ((res = dmConvertC64Bitmap(&outC64Image, inC64Image, &dmC64ImageFormats[optOutSubFormat], inC64Fmt)) != DMERR_OK) | 1783 if ((res = dmConvertC64Bitmap(&outC64Image, inC64Image, |
1784 &dmC64ImageFormats[optOutSubFormat], inC64Fmt)) != DMERR_OK) | |
1789 { | 1785 { |
1790 dmErrorMsg("Error in bitmap format conversion.\n"); | 1786 dmErrorMsg("Error in bitmap format conversion.\n"); |
1791 goto error; | 1787 goto error; |
1792 } | 1788 } |
1793 res = dmWriteBitmap(optOutFilename, outC64Image, &dmC64ImageFormats[optOutSubFormat]); | 1789 res = dmWriteBitmap(optOutFilename, outC64Image, &dmC64ImageFormats[optOutSubFormat]); |
1846 break; | 1842 break; |
1847 | 1843 |
1848 switch (optOutFormat) | 1844 switch (optOutFormat) |
1849 { | 1845 { |
1850 case FFMT_IMAGE: | 1846 case FFMT_IMAGE: |
1851 res = dmWriteImage(optOutFilename, inImage, &optSpec, optOutSubFormat, TRUE); | 1847 res = dmWriteImage(optOutFilename, inImage, &optSpec, |
1848 &dmImageFormatList[optOutSubFormat], TRUE); | |
1852 break; | 1849 break; |
1853 | 1850 |
1854 case FFMT_CHAR: | 1851 case FFMT_CHAR: |
1855 case FFMT_SPRITE: | 1852 case FFMT_SPRITE: |
1856 res = dmWriteSpritesAndChars(optOutFilename, inImage, optOutFormat, optInMulticolor); | 1853 res = dmWriteSpritesAndChars(optOutFilename, inImage, optOutFormat, optInMulticolor); |