exr2pfm.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * This file is part of exr2pfm - a command-line utility for converting
  3. * OpenEXR image files to and from the Portable FloatMap (PFM) format.
  4. *
  5. * https://projects.forum.nokia.com/exr2pfm
  6. *
  7. * Copyright (C) 2011 Tomi Aarnio (tomi.p.aarnio 'at' gmail.com)
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public License
  11. * version 2.1 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this source code; if not, write to the Free
  20. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. * This software is based on the OpenEXR image file format and library
  24. * by Industrial Light & Magic, see http://www.openexr.org.
  25. */
  26. #include <ImfRgbaFile.h> // EXR loading
  27. #include <ImfArray.h>
  28. #include <ImfOutputFile.h> // EXR saving
  29. #include <ImfChannelList.h>
  30. using namespace Imf;
  31. using namespace std;
  32. using namespace Imath;
  33. ////////////////////////////////////////////////////////////////////////////////
  34. // Loads an EXR file into an Array<Rgba>.
  35. ////////////////////////////////////////////////////////////////////////////////
  36. static Array<Rgba>* loadEXRFile (const char* filename, int& width, int& height, bool& flipY)
  37. {
  38. RgbaInputFile exrFile (filename);
  39. Box2i dw = exrFile.dataWindow();
  40. width = dw.max.x - dw.min.x + 1;
  41. height = dw.max.y - dw.min.y + 1;
  42. Array<Rgba>* imageData = new Array<Rgba>(height*width);
  43. imageData->resizeErase(height*width);
  44. exrFile.setFrameBuffer(imageData[0] - dw.min.x - dw.min.y * width, 1, width);
  45. exrFile.readPixels(dw.min.y, dw.max.y);
  46. flipY = exrFile.lineOrder()==0? true : false;
  47. return imageData;
  48. }
  49. ////////////////////////////////////////////////////////////////////////////////
  50. // Saves an EXR file from Array<Rgba> data.
  51. ////////////////////////////////////////////////////////////////////////////////
  52. static int saveEXRFile (const char *filename,
  53. const int width,
  54. const int height,
  55. Array<Rgba>* imageData)
  56. {
  57. if (filename == 0 || imageData == 0) {
  58. return 0;
  59. }
  60. // prepare header
  61. Header header (width, height);
  62. header.channels().insert ("R", Channel (HALF));
  63. header.channels().insert ("G", Channel (HALF));
  64. header.channels().insert ("B", Channel (HALF));
  65. // create file
  66. OutputFile exrFile (filename, header);
  67. // insert frame buffer
  68. FrameBuffer frameBuffer;
  69. frameBuffer.insert ("R", // name
  70. Slice (HALF, // type
  71. (char *) &(((Rgba*)imageData[0])->r), // base
  72. sizeof (Rgba), // xStride
  73. sizeof (Rgba) * width)); // yStride
  74. frameBuffer.insert ("G", // name
  75. Slice (HALF, // type
  76. (char *) &(((Rgba*)imageData[0])->g), // base
  77. sizeof (Rgba), // xStride
  78. sizeof (Rgba) * width)); // yStride
  79. frameBuffer.insert ("B", // name
  80. Slice (HALF, // type
  81. (char *) &(((Rgba*)imageData[0])->b), // base
  82. sizeof (Rgba), // xStride
  83. sizeof (Rgba) * width)); // yStride
  84. exrFile.setFrameBuffer (frameBuffer);
  85. exrFile.writePixels (height);
  86. return 1;
  87. }
  88. ////////////////////////////////////////////////////////////////////////////////
  89. // Saves a Portable FloatMap (PFM) file from raw float RGB or grayscale data.
  90. ////////////////////////////////////////////////////////////////////////////////
  91. static int savePFMFile(const char *filename,
  92. const int width,
  93. const int height,
  94. const bool flipY,
  95. Array<Rgba>* imageData)
  96. {
  97. if (filename == 0 || imageData == 0) {
  98. return 0;
  99. }
  100. printf("Writing %s...\n", filename);
  101. FILE *fp = fopen(filename, "wb");
  102. if (fp == NULL) {
  103. printf("Unable to create file %s.\n", filename);
  104. return -1;
  105. }
  106. fprintf(fp, "PF\n");
  107. printf("...saving as a color PFM file...\n");
  108. fprintf(fp, "%d %d\n", width, height);
  109. fprintf(fp, "-1\n");
  110. printf("...width %d, height %d, channels %d...\n", width, height, 3);
  111. int yStart = 0;
  112. int yStep = 1;
  113. int yStop = height;
  114. if (flipY) {
  115. printf("...flipping scanline order...\n");
  116. yStart = height-1;
  117. yStep = -1;
  118. yStop = 0;
  119. }
  120. float *imgdata = (float *)malloc(sizeof(float)*width*height*3);
  121. Rgba *rgbaData = imageData[0];
  122. for (int y=yStart, i=0; y != yStop; y += yStep) {
  123. for (int x=0; x < width; x++, i++) {
  124. float r = rgbaData[y*width + x].r;
  125. float g = rgbaData[y*width + x].g;
  126. float b = rgbaData[y*width + x].b;
  127. imgdata[i*3 ] = r;
  128. imgdata[i*3 + 1] = g;
  129. imgdata[i*3 + 2] = b;
  130. }
  131. }
  132. fwrite(imgdata, sizeof(float), width*height*3, fp);
  133. printf("...done!\n");
  134. free(imgdata);
  135. fclose(fp);
  136. return 0;
  137. }
  138. //======================================================================
  139. // Main
  140. //======================================================================
  141. int main(int argc, const char *argv[])
  142. {
  143. if (argc < 3) {
  144. printf("Usage:\texr2pfm <image.exr> <image.pfm>\n");
  145. return -1;
  146. }
  147. int orgWidth, orgHeight;
  148. bool flipY;
  149. Array<Rgba> *orgImage = loadEXRFile(argv[1], orgWidth, orgHeight, flipY);
  150. savePFMFile(argv[2], orgWidth, orgHeight, flipY, orgImage);
  151. return 0;
  152. }