comparison src/libgfx.c @ 1298:f0d6aac3adc4

Some cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 19 Aug 2017 03:20:02 +0300
parents 5bd64397453b
children b0c0be4c76f9
comparison
equal deleted inserted replaced
1297:5bd64397453b 1298:f0d6aac3adc4
742 int format; 742 int format;
743 FILE *fp; 743 FILE *fp;
744 } DMPCXData; 744 } DMPCXData;
745 745
746 746
747 // Returns one byte from row buffer (of length len) at offset soffs,
748 // OR zero if the offset is outside buffer.
747 static inline Uint8 dmPCXGetByte(Uint8 *row, const size_t len, const size_t soffs) 749 static inline Uint8 dmPCXGetByte(Uint8 *row, const size_t len, const size_t soffs)
748 { 750 {
749 return (soffs < len) ? row[soffs] : 0; 751 return (soffs < len) ? row[soffs] : 0;
750 } 752 }
751 753
1060 DMImage *img; 1062 DMImage *img;
1061 DMPCXData pcx; 1063 DMPCXData pcx;
1062 DMPCXHeader hdr; 1064 DMPCXHeader hdr;
1063 int res = 0; 1065 int res = 0;
1064 BOOL isPaletted; 1066 BOOL isPaletted;
1065
1066 pcx.buf = NULL; 1067 pcx.buf = NULL;
1067 1068
1068 // Read PCX header 1069 // Read PCX header
1069 if (!dm_fread_byte(fp, &hdr.manufacturer) || 1070 if (!dm_fread_byte(fp, &hdr.manufacturer) ||
1070 !dm_fread_byte(fp, &hdr.version) || 1071 !dm_fread_byte(fp, &hdr.version) ||
1071 !dm_fread_byte(fp, &hdr.encoding) || 1072 !dm_fread_byte(fp, &hdr.encoding) ||
1072 !dm_fread_byte(fp, &hdr.bitsPerPlane)) 1073 !dm_fread_byte(fp, &hdr.bitsPerPlane) ||
1073 { 1074 !dm_fread_le16(fp, &hdr.xmin) ||
1074 res = dmError(DMERR_FREAD,
1075 "PCX: Could not read basic header data.\n");
1076 goto error;
1077 }
1078
1079 if (hdr.manufacturer != 10 ||
1080 hdr.version != 5 ||
1081 hdr.encoding != 1)
1082 {
1083 res = dmError(DMERR_NOT_SUPPORTED,
1084 "PCX: Not a PCX file, or unsupported variant.\n");
1085 goto error;
1086 }
1087
1088 if (!dm_fread_le16(fp, &hdr.xmin) ||
1089 !dm_fread_le16(fp, &hdr.ymin) || 1075 !dm_fread_le16(fp, &hdr.ymin) ||
1090 !dm_fread_le16(fp, &hdr.xmax) || 1076 !dm_fread_le16(fp, &hdr.xmax) ||
1091 !dm_fread_le16(fp, &hdr.ymax) || 1077 !dm_fread_le16(fp, &hdr.ymax) ||
1092 !dm_fread_le16(fp, &hdr.hres) || 1078 !dm_fread_le16(fp, &hdr.hres) ||
1093 !dm_fread_le16(fp, &hdr.vres)) 1079 !dm_fread_le16(fp, &hdr.vres) ||
1094 { 1080 !dm_fread_str(fp, (Uint8 *) &hdr.colorMap, sizeof(hdr.colorMap)) ||
1095 res = dmError(DMERR_FREAD, 1081 !dm_fread_byte(fp, &hdr.reserved) ||
1096 "PCX: Could not read image dimensions.\n");
1097 goto error;
1098 }
1099
1100 if (!dm_fread_str(fp, (Uint8 *) &hdr.colorMap, sizeof(hdr.colorMap)))
1101 {
1102 res = dmError(DMERR_FREAD,
1103 "PCX: Could not read colormap.\n");
1104 goto error;
1105 }
1106
1107 if (!dm_fread_byte(fp, &hdr.reserved) ||
1108 !dm_fread_byte(fp, &hdr.nplanes) || 1082 !dm_fread_byte(fp, &hdr.nplanes) ||
1109 !dm_fread_le16(fp, &hdr.bpl) || 1083 !dm_fread_le16(fp, &hdr.bpl) ||
1110 !dm_fread_le16(fp, &hdr.palInfo) || 1084 !dm_fread_le16(fp, &hdr.palInfo) ||
1111 !dm_fread_le16(fp, &hdr.hScreenSize) || 1085 !dm_fread_le16(fp, &hdr.hScreenSize) ||
1112 !dm_fread_le16(fp, &hdr.vScreenSize) || 1086 !dm_fread_le16(fp, &hdr.vScreenSize) ||
1113 !dm_fread_str(fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler))) 1087 !dm_fread_str(fp, (Uint8 *) &hdr.filler, sizeof(hdr.filler)))
1114 { 1088 {
1115 res = dmError(DMERR_FREAD, 1089 res = dmError(DMERR_FREAD,
1116 "PCX: Could not read header remainder.\n"); 1090 "PCX: Could not read image header data.\n");
1091 goto error;
1092 }
1093
1094 if (hdr.manufacturer != 10 ||
1095 hdr.version > 5 ||
1096 hdr.encoding != 1)
1097 {
1098 res = dmError(DMERR_NOT_SUPPORTED,
1099 "PCX: Not a PCX file, or unsupported variant.\n");
1117 goto error; 1100 goto error;
1118 } 1101 }
1119 1102
1120 isPaletted = (hdr.bitsPerPlane * hdr.nplanes) <= 8; 1103 isPaletted = (hdr.bitsPerPlane * hdr.nplanes) <= 8;
1121 1104
1152 res = dmError(DMERR_MALLOC, 1135 res = dmError(DMERR_MALLOC,
1153 "PCX: Could not allocate image structure.\n"); 1136 "PCX: Could not allocate image structure.\n");
1154 goto error; 1137 goto error;
1155 } 1138 }
1156 1139
1140 // Sanity check bytes per line value
1157 if (hdr.bpl < (img->width * hdr.bitsPerPlane) / 8) 1141 if (hdr.bpl < (img->width * hdr.bitsPerPlane) / 8)
1158 { 1142 {
1159 res = dmError(DMERR_MALLOC, 1143 res = dmError(DMERR_MALLOC,
1160 "PCX: The bytes per plane line value %d is smaller than width*bpp/8 = %d!\n", 1144 "PCX: The bytes per plane line value %d is smaller than width*bpp/8 = %d!\n",
1161 hdr.bpl, (img->width * hdr.bitsPerPlane) / 8); 1145 hdr.bpl, (img->width * hdr.bitsPerPlane) / 8);