00001
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <ImfInputFile.h>
00005 #include <ImfChannelList.h>
00006 #include <ImfFrameBuffer.h>
00007 #include <half.h>
00008
00009 using namespace Imf;
00010 using namespace Imath;
00011
00012 static bool ReadEXR(const char *name, float *&rgba, int &xRes, int &yRes, bool &hasAlpha);
00013
00014 int main(int argc, char *argv[])
00015 {
00016 if (argc < 2) {
00017 fprintf(stderr, "usage: exravg [file1.exr] <file2.exr> ...\n");
00018 return 1;
00019 }
00020
00021 float *rgba, *orig_rgba = NULL;
00022 int xRes = 0, yRes = 0;
00023 bool hasAlpha = false;
00024 float a = 0;
00025 int file;
00026
00027 for (file = 1 ; file < argc ; file++) {
00028 if (ReadEXR(argv[file], rgba, xRes, yRes, hasAlpha)) {
00029 orig_rgba = rgba;
00030 a = 0;
00031 for (int i = 0; i < xRes*yRes; ++i) {
00032 for (int j = 0; j < 3; ++j)
00033 a += rgba[j];
00034 rgba += hasAlpha ? 4 : 3;
00035 }
00036 }
00037 printf("%s: Average value %f\n", argv[file], a / (3.f * xRes * yRes));
00038 delete [] orig_rgba;
00039 }
00040 return 0;
00041 }
00042
00043 static bool ReadEXR(const char *name, float *&rgba, int &xRes, int &yRes, bool &hasAlpha)
00044 {
00045 try {
00046 InputFile file(name);
00047 Box2i dw = file.header().dataWindow();
00048 xRes = dw.max.x - dw.min.x + 1;
00049 yRes = dw.max.y - dw.min.y + 1;
00050
00051 half *hrgba = new half[4 * xRes * yRes];
00052
00053
00054 hasAlpha = true;
00055 int nChannels = 4;
00056
00057 hrgba -= 4 * (dw.min.x + dw.min.y * xRes);
00058 FrameBuffer frameBuffer;
00059 frameBuffer.insert("R", Slice(HALF, (char *)hrgba,
00060 4*sizeof(half), xRes * 4 * sizeof(half), 1, 1, 0.0));
00061 frameBuffer.insert("G", Slice(HALF, (char *)hrgba+sizeof(half),
00062 4*sizeof(half), xRes * 4 * sizeof(half), 1, 1, 0.0));
00063 frameBuffer.insert("B", Slice(HALF, (char *)hrgba+2*sizeof(half),
00064 4*sizeof(half), xRes * 4 * sizeof(half), 1, 1, 0.0));
00065 frameBuffer.insert("A", Slice(HALF, (char *)hrgba+3*sizeof(half),
00066 4*sizeof(half), xRes * 4 * sizeof(half), 1, 1, 1.0));
00067
00068 file.setFrameBuffer(frameBuffer);
00069 file.readPixels(dw.min.y, dw.max.y);
00070
00071 hrgba += 4 * (dw.min.x + dw.min.y * xRes);
00072 rgba = new float[nChannels * xRes * yRes];
00073 for (int i = 0; i < nChannels * xRes * yRes; ++i)
00074 rgba[i] = hrgba[i];
00075 delete[] hrgba;
00076 } catch (const std::exception &e) {
00077 fprintf(stderr, "Unable to read image file \"%s\": %s", name, e.what());
00078 return NULL;
00079 }
00080
00081 return rgba;
00082 }