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