00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <algorithm>
00025 #include <fstream>
00026
00027 #include "lux.h"
00028 #include "error.h"
00029 #include "color.h"
00030 #include "spectrum.h"
00031
00032 #include "igiio.h"
00033
00034
00035 using namespace lux;
00036
00037
00038 namespace lux {
00039
00040 Spectrum *ReadIgiImage(const string &name, int *width, int *height) {
00041
00042 printf("IGI file format input not implemented yet");
00043 return NULL;
00044 }
00045
00046 void WriteIgiImage(const string &name, float *pixels,
00047 float *alpha, int xRes, int yRes,
00048 int totalXRes, int totalYRes,
00049 int xOffset, int yOffset) {
00050
00051 IndigoImageHeader header;
00052
00053
00054 u_int xyzSize = xRes * yRes * 3;
00055 float *xyz = new float[xyzSize];
00056 for (u_int i = 0; i < xyzSize; i+=3) {
00057
00058 xyz[i] = 0.436052025f*pixels[i] + 0.385081593f*pixels[i+1] + 0.143087414f *pixels[i+2];
00059 xyz[i+1] = 0.222491598f*pixels[i] + 0.71688606f *pixels[i+1] + 0.060621486f *pixels[i+2];
00060 xyz[i+2] = 0.013929122f*pixels[i] + 0.097097002f*pixels[i+1] + 0.71418547f *pixels[i+2];
00061 }
00062
00063 std::ofstream file(name.c_str(), std::ios::binary);
00064 if(!file) {
00065 std::stringstream ss;
00066 ss<< "Cannot open file '"<<name<<"' for output";
00067 luxError(LUX_SYSTEM, LUX_SEVERE, ss.str().c_str());
00068 return;
00069 }
00070
00071
00072 memset(&header, 0, sizeof(header));
00073 header.magic_number = INDIGO_IMAGE_MAGIC_NUMBER;
00074 header.format_version = 1;
00075
00076 header.num_samples = 1.;
00077 header.width = xRes;
00078 header.height = yRes;
00079 header.supersample_factor = 1;
00080 header.zipped = false;
00081 header.image_data_size = xyzSize * 4;
00082
00083
00084 file.write((const char*)&header, sizeof(header));
00085
00086 file.write((const char*)&xyz[0], header.image_data_size);
00087
00088 if(!file.good()) {
00089 std::stringstream ss;
00090 ss<< "Error writing IGI output file '"<<name<<"'";
00091 luxError(LUX_SYSTEM, LUX_SEVERE, ss.str().c_str());
00092 return;
00093 }
00094
00095 file.close();
00096 delete xyz;
00097 }
00098
00099 }