🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.0 KiB

  1. // https://gist.github.com/mmp/21ea8ac6d7f682b6252b
  2. #include "tinyexr/tinyexr.h"
  3. #include "tinyexr/tinyexr.cc"
  4. #include <stdio.h>
  5. #include <assert.h>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <ImfInputFile.h>
  9. #include <ImfRgbaFile.h>
  10. #include <ImfChannelList.h>
  11. #include <ImfFrameBuffer.h>
  12. #include <half.h>
  13. using namespace Imf;
  14. using namespace Imath;
  15. static float *OpenExrLoad(const char *name, int *width, int *height) {
  16. try {
  17. RgbaInputFile file (name);
  18. Box2i dw = file.dataWindow();
  19. *width = dw.max.x - dw.min.x + 1;
  20. *height = dw.max.y - dw.min.y + 1;
  21. std::vector<Rgba> pixels(*width * *height);
  22. file.setFrameBuffer(&pixels[0] - dw.min.x - dw.min.y * *width, 1, *width);
  23. file.readPixels(dw.min.y, dw.max.y);
  24. printf("OpenExr\n datawindow: (%d %d) - (%d %d)\n", dw.min.x, dw.min.y,
  25. dw.max.x, dw.max.y);
  26. printf(" line order %s\n", (file.lineOrder() == INCREASING_Y) ?
  27. "increasing y" : ((file.lineOrder() == DECREASING_Y) ? "decreasing y"
  28. : "random y"));
  29. printf(" compression: ");
  30. switch (file.compression()) {
  31. case NO_COMPRESSION: printf("none"); break;
  32. case RLE_COMPRESSION: printf("RLE"); break;
  33. case ZIPS_COMPRESSION: printf("zip"); break;
  34. case ZIP_COMPRESSION: printf("zips"); break;
  35. case PIZ_COMPRESSION: printf("piz"); break;
  36. case PXR24_COMPRESSION: printf("pxr24"); break;
  37. case B44_COMPRESSION: printf("b44"); break;
  38. case B44A_COMPRESSION: printf("b44a"); break;
  39. default: printf("unknown!");
  40. }
  41. printf("\n");
  42. printf(" channels: ");
  43. RgbaChannels channels = file.channels();
  44. if (channels & WRITE_R) printf("R");
  45. if (channels & WRITE_G) printf("G");
  46. if (channels & WRITE_B) printf("B");
  47. if (channels & WRITE_A) printf("A");
  48. if (channels & WRITE_Y) printf("Y");
  49. if (channels & WRITE_C) printf("C");
  50. printf("\n");
  51. float *ret = new float[4 * *width * *height];
  52. for (int i = 0; i < *width * *height; ++i) {
  53. ret[4*i] = pixels[i].r;
  54. ret[4*i+1] = pixels[i].g;
  55. ret[4*i+2] = pixels[i].b;
  56. ret[4*i+3] = pixels[i].a;
  57. }
  58. return ret;
  59. } catch (const std::exception &e) {
  60. return NULL;
  61. }
  62. }
  63. static void WriteImageEXR(const char *name, float *rgba,
  64. int xRes, int yRes) {
  65. Rgba *hrgba = new Rgba[xRes * yRes];
  66. for (int i = 0; i < xRes * yRes; ++i)
  67. hrgba[i] = Rgba(rgba[4*i], rgba[4*i+1], rgba[4*i+2], rgba[4*i+3]);
  68. Box2i displayWindow(V2i(0,0), V2i(xRes-1, yRes-1));
  69. Box2i dataWindow(V2i(0, 0), V2i(xRes - 1, yRes - 1));
  70. try {
  71. RgbaOutputFile file(name, displayWindow, dataWindow, WRITE_RGBA);
  72. file.setFrameBuffer(hrgba, 1, xRes);
  73. file.writePixels(yRes);
  74. }
  75. catch (const std::exception &e) {
  76. fprintf(stderr, "Unable to write image file \"%s\": %s", name,
  77. e.what());
  78. }
  79. delete[] hrgba;
  80. }
  81. int main(int argc, char *argv[]) {
  82. if (argc != 3) {
  83. fprintf(stderr, "usage: exrwritetest <tinyexr-filename.exr> <openexr-filename.exr>\n");
  84. return 1;
  85. }
  86. int w = 1;
  87. int h = 2;
  88. float *rgba = new float[4 * w * h];
  89. for (int i = 0; i < 4 * w * h; ++i) {
  90. rgba[i] = drand48();
  91. }
  92. WriteImageEXR(argv[2], rgba, w, h);
  93. const char *err;
  94. SaveEXR(rgba, w, h, argv[1], /* fp16 */1, &err);
  95. int ow, oh;
  96. float *orgba = OpenExrLoad(argv[2], &ow, &oh);
  97. int tw, th;
  98. float *trgba;
  99. if (LoadEXR(&trgba, &tw, &th, argv[1], &err) != 0) {
  100. fprintf(stderr, "exrwritetest: %s %s\n", argv[2], err);
  101. return 1;
  102. }
  103. int offset = 0;
  104. for (int y = 0; y < h; ++y) {
  105. for (int x = 0; x < w; ++x)
  106. for (int c = 0; c < 4; ++c, ++offset)
  107. if (orgba[offset] != trgba[offset])
  108. fprintf(stderr, "Mismatch at (%d,%d), component %d: "
  109. "orig %g, OpenEXR %g (err %g), tinyexr %g (err %g)\n",
  110. x, y, c, rgba[offset],
  111. orgba[offset], fabsf(rgba[offset] - orgba[offset]),
  112. trgba[offset], fabsf(rgba[offset] - trgba[offset]));
  113. }
  114. return 0;
  115. }