123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <string.h>
- #include <tiffio.h>
- #include <unistd.h>
- #include <glib.h>
- typedef struct {
- unsigned width;
- unsigned height;
- unsigned bits;
- unsigned num;
- } ImageSize;
- static void
- write_tiff_data (TIFF *tiff, uint16_t *data, ImageSize *size)
- {
- TIFFSetField (tiff, TIFFTAG_IMAGEWIDTH, size->width);
- TIFFSetField (tiff, TIFFTAG_IMAGELENGTH, size->height);
- TIFFSetField (tiff, TIFFTAG_BITSPERSAMPLE, size->bits);
- TIFFSetField (tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
- TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, 1);
- TIFFSetField (tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
- TIFFSetField (tiff, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize (tiff, 0));
- for (unsigned y = 0; y < size->height; y++, data += size->width) {
- TIFFWriteScanline (tiff, data, y, 0);
- }
- }
- static void
- write_multi_tiff (uint16_t *data, ImageSize *size)
- {
- TIFF *tiff;
- tiff = TIFFOpen ("multi.tiff", "w");
- for (unsigned i = 0; i < size->num; i++) {
- TIFFSetField (tiff, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
- TIFFSetField (tiff, TIFFTAG_PAGENUMBER, i, size->num);
- write_tiff_data (tiff, data, size);
- TIFFWriteDirectory (tiff);
- }
- /* Flush to disk to outsmart Linux */
- fsync (TIFFFileno (tiff));
- TIFFClose (tiff);
- }
- static void
- write_single_tiffs (uint16_t *data, ImageSize *size)
- {
- char fname[256];
- for (unsigned i = 0; i < size->num; i++) {
- TIFF *tiff;
- snprintf (fname, 256, "single-%05i.tiff", i);
- tiff = TIFFOpen (fname, "w");
- write_tiff_data (tiff, data, size);
- /* Flush to disk to outsmart Linux */
- fsync (TIFFFileno (tiff));
- TIFFClose (tiff);
- }
- }
- static void
- print_time (const char *fmt, ImageSize *size, GTimer *timer)
- {
- size_t total;
- total = size->width * size->height * size->num * 2;
- printf (fmt, ((double) total) / 1024. / 1024. / g_timer_elapsed (timer, NULL));
- }
- int
- main(int argc, char const* argv[])
- {
- GTimer *timer;
- ImageSize size = {
- .width = 2048,
- .height = 2048,
- .bits = 16,
- .num = 220
- };
- uint16_t *data;
- data = malloc (size.width * size.height * 2);
- timer = g_timer_new ();
- g_timer_start (timer);
- write_multi_tiff (data, &size);
- g_timer_stop (timer);
- print_time ("Multi TIFF: %f MB/s\n", &size, timer);
- g_timer_start (timer);
- write_single_tiffs (data, &size);
- g_timer_stop (timer);
- print_time ("Single TIFF: %f MB/s\n", &size, timer);
- g_timer_destroy (timer);
- free (data);
- return 0;
- }
|