tiffperf.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <tiffio.h>
  6. #include <unistd.h>
  7. #include <glib.h>
  8. typedef struct {
  9. unsigned width;
  10. unsigned height;
  11. unsigned bits;
  12. unsigned num;
  13. } ImageSize;
  14. static void
  15. write_tiff_data (TIFF *tiff, uint16_t *data, ImageSize *size)
  16. {
  17. TIFFSetField (tiff, TIFFTAG_IMAGEWIDTH, size->width);
  18. TIFFSetField (tiff, TIFFTAG_IMAGELENGTH, size->height);
  19. TIFFSetField (tiff, TIFFTAG_BITSPERSAMPLE, size->bits);
  20. TIFFSetField (tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
  21. TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, 1);
  22. TIFFSetField (tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  23. TIFFSetField (tiff, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize (tiff, 0));
  24. for (unsigned y = 0; y < size->height; y++, data += size->width) {
  25. TIFFWriteScanline (tiff, data, y, 0);
  26. }
  27. }
  28. static void
  29. write_multi_tiff (uint16_t *data, ImageSize *size)
  30. {
  31. TIFF *tiff;
  32. tiff = TIFFOpen ("multi.tiff", "w");
  33. for (unsigned i = 0; i < size->num; i++) {
  34. TIFFSetField (tiff, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
  35. TIFFSetField (tiff, TIFFTAG_PAGENUMBER, i, size->num);
  36. write_tiff_data (tiff, data, size);
  37. TIFFWriteDirectory (tiff);
  38. }
  39. /* Flush to disk to outsmart Linux */
  40. fsync (TIFFFileno (tiff));
  41. TIFFClose (tiff);
  42. }
  43. static void
  44. write_single_tiffs (uint16_t *data, ImageSize *size)
  45. {
  46. char fname[256];
  47. for (unsigned i = 0; i < size->num; i++) {
  48. TIFF *tiff;
  49. snprintf (fname, 256, "single-%05i.tiff", i);
  50. tiff = TIFFOpen (fname, "w");
  51. write_tiff_data (tiff, data, size);
  52. /* Flush to disk to outsmart Linux */
  53. fsync (TIFFFileno (tiff));
  54. TIFFClose (tiff);
  55. }
  56. }
  57. static void
  58. print_time (const char *fmt, ImageSize *size, GTimer *timer)
  59. {
  60. size_t total;
  61. total = size->width * size->height * size->num * 2;
  62. printf (fmt, ((double) total) / 1024. / 1024. / g_timer_elapsed (timer, NULL));
  63. }
  64. int
  65. main(int argc, char const* argv[])
  66. {
  67. GTimer *timer;
  68. ImageSize size = {
  69. .width = 2048,
  70. .height = 2048,
  71. .bits = 16,
  72. .num = 220
  73. };
  74. uint16_t *data;
  75. data = malloc (size.width * size.height * 2);
  76. timer = g_timer_new ();
  77. g_timer_start (timer);
  78. write_multi_tiff (data, &size);
  79. g_timer_stop (timer);
  80. print_time ("Multi TIFF: %f MB/s\n", &size, timer);
  81. g_timer_start (timer);
  82. write_single_tiffs (data, &size);
  83. g_timer_stop (timer);
  84. print_time ("Single TIFF: %f MB/s\n", &size, timer);
  85. g_timer_destroy (timer);
  86. free (data);
  87. return 0;
  88. }