comparison libinterp/dldfcn/__tiff__.cc @ 31154:828b7cc9aa36

Tiff write: added support for BiLevel stripped images and added unit tests * __tiff__.cc(write_stripped_image): implemented support for writing logical images. * Tiff.m: added tests for writing stripped images using the write method.
author magedrifaat <magedrifaat@gmail.com>
date Thu, 04 Aug 2022 00:02:27 +0200
parents c66d6c7f025e
children a30b144bc10b
comparison
equal deleted inserted replaced
31153:c66d6c7f025e 31154:828b7cc9aa36
1266 uint32_t rows_in_strip; 1266 uint32_t rows_in_strip;
1267 for (uint32_t strip = 0; strip < strip_count; strip++) 1267 for (uint32_t strip = 0; strip < strip_count; strip++)
1268 { 1268 {
1269 rows_in_strip = get_rows_in_strip (strip, strip_count, 1269 rows_in_strip = get_rows_in_strip (strip, strip_count,
1270 row_per_strip, image_data); 1270 row_per_strip, image_data);
1271 strip_size = rows_in_strip * image_data->width * sizeof (P); 1271 if (image_data->bits_per_sample == 8
1272 if (image_data->planar_configuration == PLANARCONFIG_CONTIG) 1272 || image_data->bits_per_sample == 16
1273 strip_size *= image_data->samples_per_pixel; 1273 || image_data->bits_per_sample == 32
1274 if (! TIFFWriteEncodedStrip (tif, strip, pixel_fvec, strip_size)) 1274 || image_data->bits_per_sample == 64)
1275 error ("Failed to rite strip data"); 1275 {
1276 pixel_fvec += strip_size; 1276 strip_size = rows_in_strip * image_data->width * sizeof (P);
1277 if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
1278 strip_size *= image_data->samples_per_pixel;
1279 if (! TIFFWriteEncodedStrip (tif, strip, pixel_fvec, strip_size))
1280 error ("Failed to rite strip data");
1281 pixel_fvec += strip_size;
1282 }
1283 else if (image_data->bits_per_sample == 1)
1284 {
1285 // Create a buffer to hold the packed strip data
1286 // Unique pointers are faster than vectors for constant size buffers
1287 std::unique_ptr<uint8_t []> strip_ptr
1288 = std::make_unique<uint8_t []> (TIFFStripSize (tif));
1289 uint8_t *strip_buf = strip_ptr.get ();
1290 // According to the format specification, the row should be byte
1291 // aligned so the number of bytes is rounded up to the nearest byte
1292 uint32_t padded_width = (image_data->width + 7) / 8;
1293 // Packing the pixel data into bits
1294 for (uint32_t row = 0; row < rows_in_strip; row++)
1295 {
1296 for (uint32_t col = 0; col < image_data->width; col++)
1297 {
1298 uint8_t shift = 7 - col % 8;
1299 strip_buf[row * padded_width + col / 8]
1300 |= pixel_fvec[col] << shift;
1301 }
1302 pixel_fvec += image_data->width;
1303 }
1304 strip_size = padded_width * rows_in_strip;
1305 if (TIFFWriteEncodedStrip (tif, strip, strip_buf, strip_size) == -1)
1306 error ("Failed to write strip data to image");
1307 }
1308 else
1309 error ("Unsupported bit depth");
1277 } 1310 }
1278 } 1311 }
1279 1312
1280 template <typename T> 1313 template <typename T>
1281 void 1314 void