comparison libinterp/corefcn/__tiff__.cc @ 31165:48d46f7a640b

Tiff setTag: handled scalar tags with the correct data type.
author magedrifaat <magedrifaat@gmail.com>
date Fri, 12 Aug 2022 00:04:37 +0200
parents 3155aa74c62e
children b8b6cc05c8ea
comparison
equal deleted inserted replaced
31164:3155aa74c62e 31165:48d46f7a640b
852 retval = octave_value (arr); 852 retval = octave_value (arr);
853 break; 853 break;
854 } 854 }
855 case TIFF_RATIONAL: 855 case TIFF_RATIONAL:
856 { 856 {
857 // TODO(maged): This is incorrect, LibTIFF already handles
858 // rationals and converts them to floats
857 NDArray arr (arr_dims); 859 NDArray arr (arr_dims);
858 for (uint32_t i = 0; i < count; i+=2) 860 for (uint32_t i = 0; i < count; i+=2)
859 { 861 {
860 arr(i / 2) = static_cast<float> ((reinterpret_cast<uint32_t *> (data))[i]) 862 arr(i / 2) = static_cast<float> ((reinterpret_cast<uint32_t *> (data))[i])
861 / static_cast<float> ((reinterpret_cast<uint32_t *> (data))[i+1]); 863 / static_cast<float> ((reinterpret_cast<uint32_t *> (data))[i+1]);
1176 1178
1177 return tag_data_ov; 1179 return tag_data_ov;
1178 } 1180 }
1179 1181
1180 void 1182 void
1183 set_scalar_field_data (TIFF *tif, const TIFFField *fip, octave_value tag_ov)
1184 {
1185 uint32_t tag_id = TIFFFieldTag (fip);
1186
1187 // Since scalar tags are the last to be handled, any tag that
1188 // require a count to be passed is an unsupported tag.
1189 if (TIFFFieldPassCount (fip))
1190 error ("Unsupported tag");
1191
1192 TIFFDataType tag_datatype = TIFFFieldDataType (fip);
1193 switch (tag_datatype)
1194 {
1195 case TIFF_BYTE:
1196 case TIFF_UNDEFINED:
1197 // TODO(maged): check if matlab errors for long data type/range
1198 TIFFSetField (tif, tag_id, tag_ov.uint8_scalar_value ());
1199 break;
1200 case TIFF_SHORT:
1201 TIFFSetField (tif, tag_id, tag_ov.uint16_scalar_value ());
1202 break;
1203 case TIFF_LONG:
1204 TIFFSetField (tif, tag_id, tag_ov.uint32_scalar_value ());
1205 break;
1206 case TIFF_LONG8:
1207 TIFFSetField (tif, tag_id, tag_ov.uint64_scalar_value ());
1208 break;
1209 case TIFF_RATIONAL:
1210 TIFFSetField (tif, tag_id, tag_ov.float_scalar_value ());
1211 break;
1212 case TIFF_SBYTE:
1213 TIFFSetField (tif, tag_id, tag_ov.int8_scalar_value ());
1214 break;
1215 case TIFF_SSHORT:
1216 TIFFSetField (tif, tag_id, tag_ov.int16_scalar_value ());
1217 break;
1218 case TIFF_SLONG:
1219 TIFFSetField (tif, tag_id, tag_ov.int32_scalar_value ());
1220 break;
1221 case TIFF_SLONG8:
1222 TIFFSetField (tif, tag_id, tag_ov.int64_scalar_value ());
1223 break;
1224 case TIFF_FLOAT:
1225 TIFFSetField (tif, tag_id, tag_ov.float_scalar_value ());
1226 break;
1227 case TIFF_DOUBLE:
1228 TIFFSetField (tif, tag_id, tag_ov.double_value ());
1229 break;
1230 case TIFF_SRATIONAL:
1231 TIFFSetField (tif, tag_id, tag_ov.float_scalar_value ());
1232 break;
1233 case TIFF_IFD:
1234 case TIFF_IFD8:
1235 error ("Unimplemented IFFD data type");
1236 break;
1237 default:
1238 error ("Unsupported tag data type");
1239 }
1240 }
1241
1242 void
1243 set_array_field_data (TIFF *tif, const TIFFField *fip,
1244 octave_value tag_ov, uint32_t count)
1245 {
1246 }
1247
1248 void
1181 set_field_data (TIFF *tif, const TIFFField *fip, octave_value tag_ov) 1249 set_field_data (TIFF *tif, const TIFFField *fip, octave_value tag_ov)
1182 { 1250 {
1183 // TODO(maged): complete the implementation of this function 1251 // TODO(maged): complete the implementation of this function
1184 uint32_t tag_id = TIFFFieldTag (fip); 1252 uint32_t tag_id = TIFFFieldTag (fip);
1253
1254 // TODO(maged): find/create images to test the special tags
1255 switch (tag_id)
1256 {
1257 case TIFFTAG_STRIPBYTECOUNTS:
1258 case TIFFTAG_STRIPOFFSETS:
1259 set_array_field_data (tif, fip, tag_ov, TIFFNumberOfStrips (tif));
1260 break;
1261 case TIFFTAG_TILEBYTECOUNTS:
1262 case TIFFTAG_TILEOFFSETS:
1263 set_array_field_data (tif, fip, tag_ov, TIFFNumberOfTiles (tif));
1264 break;
1265 case TIFFTAG_YCBCRCOEFFICIENTS:
1266 set_array_field_data (tif, fip, tag_ov, 3);
1267 break;
1268 case TIFFTAG_REFERENCEBLACKWHITE:
1269 set_array_field_data (tif, fip, tag_ov, 6);
1270 break;
1271 case TIFFTAG_GRAYRESPONSECURVE:
1272 {
1273 uint16_t bits_per_sample;
1274 if (! TIFFGetFieldDefaulted (tif, TIFFTAG_BITSPERSAMPLE,
1275 &bits_per_sample))
1276 error ("Failed to obtain the bit depth");
1277
1278 set_array_field_data (tif, fip, tag_ov, 1<<bits_per_sample);
1279 break;
1280 }
1281 case TIFFTAG_COLORMAP:
1282 {
1283 uint16_t bits_per_sample;
1284 if (! TIFFGetFieldDefaulted (tif, TIFFTAG_BITSPERSAMPLE,
1285 &bits_per_sample))
1286 error ("Failed to obtain the bit depth");
1287
1288 // According to the format specification, this field should
1289 // be 8 or 16 only.
1290 if (bits_per_sample > 16)
1291 error ("Too high bit depth for a palette image");
1292
1293 uint32_t count = 1 << bits_per_sample;
1294 // uint16_t *red, *green, *blue;
1295 // validate_tiff_get_field (TIFFGetField (tif, TIFFTAG_COLORMAP,
1296 // &red, &green, &blue));
1297 break;
1298 }
1299 case TIFFTAG_TRANSFERFUNCTION:
1300 {
1301 uint16_t samples_per_pixel;
1302 if (! TIFFGetFieldDefaulted (tif, TIFFTAG_SAMPLESPERPIXEL,
1303 &samples_per_pixel))
1304 error ("Failed to obtain the number of samples per pixel");
1305
1306 uint16_t bits_per_sample;
1307 if (! TIFFGetFieldDefaulted (tif, TIFFTAG_BITSPERSAMPLE,
1308 &bits_per_sample))
1309 error ("Failed to obtain the number of samples per pixel");
1310
1311 uint32_t count = 1 << bits_per_sample;
1312 uint16_t *ch1, *ch2, *ch3;
1313 if (samples_per_pixel == 1)
1314 {
1315 // validate_tiff_get_field (TIFFGetField (tif, TIFFTAG_COLORMAP, &ch1));
1316 }
1317 else
1318 {
1319 // validate_tiff_get_field (TIFFGetField (tif, TIFFTAG_COLORMAP,
1320 // &ch1, &ch2, &ch3));
1321 }
1322 break;
1323 }
1324 case TIFFTAG_PAGENUMBER:
1325 case TIFFTAG_HALFTONEHINTS:
1326 case TIFFTAG_DOTRANGE:
1327 case TIFFTAG_YCBCRSUBSAMPLING:
1328 {
1329 uint16_t tag_part1, tag_part2;
1330 // validate_tiff_get_field (TIFFGetField (tif, tag_id,
1331 // &tag_part1, &tag_part2));
1332 break;
1333 }
1334 case TIFFTAG_SUBIFD:
1335 {
1336 uint16_t count;
1337 uint64_t *offsets;
1338 // validate_tiff_get_field (TIFFGetField (tif, tag_id, &count, &offsets));
1339 break;
1340 }
1341 case TIFFTAG_EXTRASAMPLES:
1342 {
1343 uint16_t count;
1344 uint16_t *types;
1345 // validate_tiff_get_field (TIFFGetField (tif, tag_id, &count, &types));
1346 break;
1347 }
1348 // TODO(maged): These tags are more complex to implement
1349 // will be implemented and tested later.
1350 case TIFFTAG_XMLPACKET:
1351 case TIFFTAG_RICHTIFFIPTC:
1352 case TIFFTAG_PHOTOSHOP:
1353 case TIFFTAG_ICCPROFILE:
1354 {
1355 error ("Complex Tags not implemented");
1356 break;
1357 }
1358 // These tags are not mentioned in the LibTIFF documentation
1359 // but are handled correctly by the library
1360 case TIFFTAG_ZIPQUALITY:
1361 case TIFFTAG_SGILOGDATAFMT:
1362 case TIFFTAG_GRAYRESPONSEUNIT:
1363 {
1364 set_scalar_field_data (tif, fip, tag_ov);
1365 break;
1366 }
1367 default:
1368 set_scalar_field_data (tif, fip, tag_ov);
1369 }
1185 uint32_t tag_data = tag_ov.double_value (); 1370 uint32_t tag_data = tag_ov.double_value ();
1186 1371
1187 if (! TIFFSetField(tif, tag_id, tag_data)) 1372 if (! TIFFSetField(tif, tag_id, tag_data))
1188 error ("Failed to set tag value"); 1373 error ("Failed to set tag value");
1189 } 1374 }