18 #ifndef PIC_IO_HDR_HPP 19 #define PIC_IO_HDR_HPP 24 #include "../colors/rgbe.hpp" 25 #include "../base.hpp" 41 FILE *file = fopen(nameFile.c_str(),
"rb");
50 fscanf(file,
"%s\n", tmp);
52 if(strcmp(tmp,
"#?RADIANCE") != 0) {
57 std::string line =
"";
60 char *tmp2 = fgets(tmp, 512, file);
67 size_t pos = line.find(
"\n");
69 if(pos != std::string::npos) {
74 if(line.compare(
"\n") == 0) {
79 if(line.find(
"FORMAT") != std::string::npos) {
80 if(line.find(
"32-bit_rle_rgbe") == std::string::npos) {
85 if(line.find(
"EXPOSURE=") != std::string::npos) {
91 fscanf(file,
"-Y %d +X %d", &height, &width);
95 data =
new float[width * height * 3];
99 long int s_cur = ftell(file);
100 fseek(file, 0 , SEEK_END);
101 long int s_end = ftell(file);
102 fseek(file, s_cur, SEEK_SET);
103 int total = s_end - s_cur;
106 printf(
"%d %d\n", total, width * height * 4);
110 if(total == (width * height * 4)) {
111 unsigned char colRGBE[4];
115 for(
int i = 0; i < width; i++) {
116 for(
int j = 0; j < height; j++) {
117 fread(colRGBE, 1, 4, file);
123 unsigned char *buffer =
new unsigned char[total];
124 fread(buffer,
sizeof(
unsigned char)*total, 1, file);
126 int line_width3 = width * 3;
127 int line_width4 = width * 4;
129 unsigned char *buffer_line_start;
130 unsigned char *buffer_line =
new unsigned char[line_width4];
132 int c_buffer_line = 0;
135 for(
int i = 0; i < height; i++) {
136 buffer_line_start = &buffer[c - 4];
138 int width_check = buffer_line_start[2];
139 int width_check2 = buffer_line_start[3];
141 bool b1 = buffer_line_start[0] != 2;
142 bool b2 = buffer_line_start[1] != 2;
143 bool b3 = width_check != (width >> 8);
144 bool b4 = width_check2 != (width & 0xFF);
146 if(b1 || b2 || b3 || b4) {
148 printf(
"ReadHDR ERROR: the file is not a RLE encoded .hdr file.\n");
156 for(
int j = 0; j < 4; j++) {
166 for(
int l = k; l < (k + num); l++) {
167 buffer_line[l * 4 + j] = buffer[c + 1];
173 for(
int l = 0; l < num; l++) {
174 buffer_line[(l + k) * 4 + j] = buffer[c + 1 + l];
184 for(
int j = 0; j < width; j++) {
189 c_buffer_line += line_width3;
192 delete[] buffer_line;
210 while(cur_pointer < width) {
212 int run_length_old = 0;
214 int run_start = cur_pointer;
217 while((run_length < 4 ) && (run_start < width)) {
218 run_start += run_length;
219 run_length_old = run_length;
221 int start = (run_start + 1);
222 int end =
MIN(run_start + 127, width);
223 unsigned char tmp = buffer_line[run_start];
227 for(
int i=start; i<end; i++) {
228 if(tmp == buffer_line[i]) {
237 if((run_length_old > 1) && (run_length_old == (run_start - cur_pointer))){
238 unsigned char length_to_write = run_length_old + 128;
239 unsigned char value_to_write = buffer_line[cur_pointer];
240 fwrite(&length_to_write,
sizeof(
unsigned char), 1, file);
241 fwrite(&value_to_write,
sizeof(
unsigned char), 1, file);
243 cur_pointer = run_start;
247 while(cur_pointer < run_start) {
248 int non_run_length = run_start - cur_pointer;
250 if(non_run_length > 128) {
251 unsigned char length_to_write = 128;
252 fwrite(&length_to_write,
sizeof(
unsigned char), 1, file);
253 fwrite(&buffer_line[cur_pointer],
sizeof(
unsigned char)*length_to_write, 1, file);
255 cur_pointer += length_to_write;
257 fwrite(&non_run_length,
sizeof(
unsigned char), 1, file);
258 fwrite(&buffer_line[cur_pointer],
sizeof(
unsigned char)*non_run_length, 1, file);
260 cur_pointer += non_run_length;
266 unsigned char length_to_write = run_length + 128;
267 unsigned char value_to_write = buffer_line[run_start];
268 fwrite(&length_to_write,
sizeof(
unsigned char), 1, file);
269 fwrite(&value_to_write,
sizeof(
unsigned char), 1, file);
271 cur_pointer += run_length;
289 int height,
int channels,
float appliedExposure = 1.0f,
bool bRLE =
true)
297 file = fopen(nameFile.c_str(),
"wb");
303 if((channels == 2) || (channels == 0)) {
308 fprintf(file,
"#?RADIANCE\n");
309 fprintf(file,
"#Spiced by Piccante\n");
310 fprintf(file,
"FORMAT=32-bit_rle_rgbe\n");
311 fprintf(file,
"EXPOSURE= %f\n\n", appliedExposure);
312 fprintf(file,
"-Y %d +X %d\n", height, width);
315 if(((width < 8) || (width > 32767)) && bRLE) {
321 unsigned char *buffer_line =
new unsigned char[width * 4];
322 unsigned char buffer_rgbe[4];
323 unsigned char buffer_line_start[4];
326 buffer_line_start[0] = 2;
327 buffer_line_start[1] = 2;
328 buffer_line_start[2] = width >> 8;
329 buffer_line_start[3] = width & 0xFF;
331 int width2 = width * 2;
332 int width3 = width * 3;
334 for(
int i=0; i<height; i++) {
338 for(
int j = 0; j < width; j++) {
339 int ind2 = (ind + j) * channels;
347 buffer_line[ j] = buffer_rgbe[0];
348 buffer_line[width + j] = buffer_rgbe[1];
349 buffer_line[width2 + j] = buffer_rgbe[2];
350 buffer_line[width3 + j] = buffer_rgbe[3];
354 fwrite(buffer_line_start,
sizeof(
unsigned char)*4, 1, file);
357 for(
int j=0; j<4; j++) {
363 unsigned char colRGBE[4];
364 for(
int j = 0; j < height; j++) {
367 for(
int i = 0; i < width; i++) {
377 fwrite(colRGBE, 1, 4 *
sizeof(
unsigned char), file);
398 int height,
int channels,
int blockID,
int nBlocks)
402 if((file = fopen(nameFile.c_str(),
"wb")) == NULL || (buffer_line == NULL)) {
413 int blockWidth = width / nBlocks;
415 int xStart = blockWidth * blockID;
416 int xEnd = xStart + blockWidth;
422 blockWidth = xEnd - xStart;
424 fprintf(file,
"#?RADIANCE\n");
425 fprintf(file,
"#Spiced by Piccante\n");
426 fprintf(file,
"FORMAT=32-bit_rle_rgbe\n");
427 fprintf(file,
"EXPOSURE= 1.0\n\n");
428 fprintf(file,
"-Y %d +X %d\n", height, blockWidth);
430 unsigned char colRGBE[4];
432 for(
int j = 0; j < height; j++) {
435 for(
int i = xStart; i < xEnd; i++) {
445 fwrite(colRGBE, 1, 4 *
sizeof(
unsigned char), file);
PIC_INLINE void fromFloatToRGBE(float *colFloat, unsigned char *colRGBE)
fromFloatToRGBE
Definition: rgbe.hpp:38
PIC_INLINE void fromRGBEToFloat(unsigned char *colRGBE, float *colFloat)
fromRGBEToFloat
Definition: rgbe.hpp:102
PIC_INLINE void WriteLineHDR(FILE *file, unsigned char *buffer_line, int width)
WriteLineHDR writes a scanline of an image using RLE and RGBE encoding.
Definition: hdr.hpp:206
PIC_INLINE bool WriteHDR(std::string nameFile, float *data, int width, int height, int channels, float appliedExposure=1.0f, bool bRLE=true)
WriteHDR writes a .hdr/.pic file.
Definition: hdr.hpp:288
PIC_INLINE void fromSingleFloatToRGBE(float *colFloat, unsigned char *colRGBE)
fromSingleFloatToRGBE
Definition: rgbe.hpp:74
#define PIC_INLINE
Definition: base.hpp:33
#define MIN(a, b)
Definition: math.hpp:69
Definition: bilateral_separation.hpp:25
PIC_INLINE float * ReadHDR(std::string nameFile, float *data, int &width, int &height)
ReadHDR reads a .hdr/.pic file.
Definition: hdr.hpp:38
PIC_INLINE bool WriteHDRBlock(std::string nameFile, float *buffer_line, int width, int height, int channels, int blockID, int nBlocks)
WriteHDRBlock writes a .hdr file.
Definition: hdr.hpp:397