comparison tools/gfxconv.c @ 1481:e967e8e3b8c3

Sanitize dmWriteBitmap().
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 11 May 2018 02:49:28 +0300
parents c3eba254bcc2
children 665a0b917d22
comparison
equal deleted inserted replaced
1480:c3eba254bcc2 1481:e967e8e3b8c3
1024 dmFree(used); 1024 dmFree(used);
1025 return DMERR_OK; 1025 return DMERR_OK;
1026 } 1026 }
1027 1027
1028 1028
1029 int dmWriteBitmap(const char *filename, const DMC64Image *image, const DMC64ImageFormat *fmt, const BOOL enableFixUps) 1029 int dmWriteBitmap(const char *filename, const DMC64Image *image)
1030 { 1030 {
1031 FILE *outFile = NULL; 1031 int res = DMERR_OK;
1032 const DMC64ImageFormat *fmt = &dmC64ImageFormats[image->type];
1032 DMGrowBuf buf; 1033 DMGrowBuf buf;
1033 int res = DMERR_OK; 1034
1034 1035 // Encode to target format
1035 dmMsg(1, "Converting to %s format bitmap.\n", fmt->name); 1036 dmMsg(1, "Encoding C64 bitmap data to format '%s'\n", fmt->name);
1036 if (image->type != fmt->type && enableFixUps)
1037 {
1038 // Try to do some simple fixups
1039 if ((fmt->type & D64_FMT_FLI) && (image->type & D64_FMT_FLI) == 0)
1040 {
1041 dmMsg(1, "Upconverting multicolor to FLI.\n");
1042 int i;
1043 for (i = 1; i < image->nbanks; i++)
1044 {
1045 memcpy(image->color[i], image->color[0], C64_SCR_COLOR_SIZE);
1046 memcpy(image->screen[i], image->screen[0], C64_SCR_SCREEN_SIZE);
1047 }
1048 }
1049 }
1050
1051 // Encode it!
1052 if ((res = dmC64EncodeBMP(&buf, image, fmt)) != DMERR_OK) 1037 if ((res = dmC64EncodeBMP(&buf, image, fmt)) != DMERR_OK)
1053 {
1054 dmError(res, "Error converting bitmap: %s\n", dmErrorStr(res));
1055 goto error; 1038 goto error;
1056 } 1039
1057 1040 // And output the file
1058 dmMsg(2, "Result: %d bytes\n", buf.len); 1041 dmMsg(1, "Writing output file '%s'\n", filename);
1059 1042 if ((res = dmWriteDataFile(NULL, filename, buf.data, buf.len)) != DMERR_OK)
1060 if ((outFile = fopen(filename, "wb")) == NULL)
1061 {
1062 res = dmGetErrno();
1063 dmError(res, "Error opening output file '%s', %d: %s\n",
1064 filename, res, dmErrorStr(res));
1065 goto error; 1043 goto error;
1066 }
1067
1068 if (!dm_fwrite_str(outFile, buf.data, buf.len))
1069 {
1070 res = dmGetErrno();
1071 dmError(res, "Error writing image data to '%s', %d: %s\n",
1072 filename, res, dmErrorStr(res));
1073 }
1074 1044
1075 error: 1045 error:
1076 if (outFile != NULL)
1077 fclose(outFile);
1078
1079 dmGrowBufFree(&buf); 1046 dmGrowBufFree(&buf);
1080 return res; 1047 return res;
1081 } 1048 }
1082 1049
1083 1050
1758 dmSetDefaultC64Palette(outImage); 1725 dmSetDefaultC64Palette(outImage);
1759 res = dmWriteImage(optOutFilename, outImage, &optSpec, optOutSubFormat, TRUE); 1726 res = dmWriteImage(optOutFilename, outImage, &optSpec, optOutSubFormat, TRUE);
1760 break; 1727 break;
1761 1728
1762 case FFMT_BITMAP: 1729 case FFMT_BITMAP:
1763 res = dmWriteBitmap(optOutFilename, inC64Image, &dmC64ImageFormats[optOutSubFormat], TRUE); 1730 res = dmWriteBitmap(optOutFilename, inC64Image);
1764 break; 1731 break;
1765 1732
1766 case FFMT_CHAR: 1733 case FFMT_CHAR:
1767 case FFMT_SPRITE: 1734 case FFMT_SPRITE:
1768 res = dmC64ConvertBMP2Image(&outImage, inC64Image, inC64Fmt); 1735 res = dmC64ConvertBMP2Image(&outImage, inC64Image, inC64Fmt);