gKit2 light
image_hdr.cpp
1 
2 #include <chrono>
3 #include <cstring>
4 #include <string>
5 
6 #include "rgbe.h"
7 #include "image_hdr.h"
8 
9 
10 bool is_hdr_image( const char *filename )
11 {
12  return (std::string(filename).rfind(".hdr") != std::string::npos);
13 }
14 
15 
16 Image read_image_hdr( const char *filename )
17 {
18  FILE *in= fopen(filename, "rb");
19  if(in == NULL)
20  {
21  printf("[error] loading hdr image '%s'...\n", filename);
22  return Image::error();
23  }
24 
25  int width, height;
26  rgbe_header_info info;
27  if(RGBE_ReadHeader(in, &width, &height, &info) != RGBE_RETURN_SUCCESS)
28  {
29  fclose(in);
30  printf("[error] loading hdr image '%s'...\n", filename);
31  return Image::error();
32  }
33 
34  std::vector<float> data(width*height*3, 0.f);
35  if(RGBE_ReadPixels_RLE(in, &data.front(), width, height) != RGBE_RETURN_SUCCESS)
36  {
37  fclose(in);
38  printf("[error] loading hdr image '%s'...\n", filename);
39  return Image::error();
40  }
41 
42  fclose(in);
43 
44  //
45  printf("loading hdr image '%s' %dx%d...\n", filename, width, height);
46  Image image(width, height);
47 
48  int i= 0;
49  for(int y= 0; y < height; y++)
50  for(int x= 0; x < width; x++, i+= 3)
51  image(x, height - y -1)= Color(data[i], data[i+1], data[i+2]);
52 
53  return image;
54 }
55 
56 int write_image_hdr( const Image& image, const char *filename )
57 {
58  if(image == Image::error())
59  return -1;
60 
61  FILE *out= fopen(filename, "wb");
62  if(out == NULL)
63  {
64  printf("[error] writing hdr image '%s'...\n", filename);
65  return -1;
66  }
67 
68  int width= image.width();
69  int height= image.height();
70  if(RGBE_WriteHeader(out, width, height, NULL) != RGBE_RETURN_SUCCESS)
71  {
72  fclose(out);
73 
74  printf("[error] writing hdr image '%s'...\n", filename);
75  return -1;
76  }
77 
78  std::vector<float> data(width*height*3, 0.f);
79  int i= 0;
80  for(int y= 0; y < height; y++)
81  for(int x= 0; x < width; x++, i+= 3)
82  {
83  Color color= image(x, height - y -1);
84  data[i]= color.r;
85  data[i+1]= color.g;
86  data[i+2]= color.b;
87  }
88 
89  int code= RGBE_WritePixels_RLE(out, &data.front(), width, height);
90  fclose(out);
91 
92  if(code != RGBE_RETURN_SUCCESS)
93  {
94  printf("[error] writing hdr image '%s'...\n", filename);
95  return -1;
96  }
97 
98  printf("writing hdr image '%s'...\n", filename);
99  return 0;
100 }
101 
102 
103 Image read_image_pfm( const char *filename )
104 {
105  FILE *in= fopen(filename, "rb");
106  if(in == nullptr)
107  {
108  printf("[error] loading pfm image '%s'...\n", filename);
109  return Image();
110  }
111 
112  int w, h;
113  float endian= 0;
114  if(fscanf(in, "PF\xa%d %d\xa%f[^\xa]", &w, &h, &endian) != 3
115  || endian != -1)
116  {
117  printf("[error] loading pfm image '%s'...\n", filename);
118  return Image();
119  }
120 
121  // saute la fin de l'entete
122  unsigned char c= fgetc(in);
123  while(c != '\xa')
124  c= fgetc(in);
125 
126  // pourquoi aussi tordu ? fscanf(in, "PF\n%d %d\n%f\n") consomme les espaces apres le \n... ce qui est un poil genant pour relire les floats...
127 
128  printf("loading pfm image '%s' %dx%d...\n", filename, w, h);
129 
130  Image image(w, h);
131 
132  for(int y= 0; y < h; y++)
133  for(int x= 0; x < w; x++)
134  {
135  Color pixel;
136  if(fread(&pixel.r, sizeof(float), 3, in) == 3)
137  image(x, y)= pixel;
138  }
139  fclose(in);
140 
141  return image;
142 }
143 
144 
146 int write_image_pfm( const Image& image, const char *filename )
147 {
148  FILE *out= fopen(filename, "wb");
149  if(out == nullptr)
150  {
151  printf("[error] writing pfm image '%s'...\n", filename);
152  return -1;
153  }
154 
155  fprintf(out, "PF\xa%d %d\xa-1\xa", image.width(), image.height());
156 
157  for(int y= 0; y < image.height(); y++)
158  for(int x= 0; x < image.width(); x++)
159  {
160  Color pixel= image(x, y);
161  fwrite(&pixel.r, sizeof(float), 3, out);
162  }
163  fclose(out);
164 
165  printf("writing pfm image '%s'...\n", filename);
166  return 0;
167 }
168 
169 
171 bool is_pfm_image( const char *filename )
172 {
173  return (std::string(filename).rfind(".pfm") != std::string::npos);
174 }
representation d'une image.
Definition: image.h:21
int height() const
renvoie la hauteur de l'image.
Definition: image.h:100
static Image & error()
Definition: image.h:126
int width() const
renvoie la largeur de l'image.
Definition: image.h:98
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
Definition: text.cpp:140
Image read_image_pfm(const char *filename)
charge une image a partir d'un fichier .pfm.
Definition: image_hdr.cpp:103
int write_image_pfm(const Image &image, const char *filename)
enregistre une image dans un fichier .pfm.
Definition: image_hdr.cpp:146
bool is_pfm_image(const char *filename)
renvoie vrai si le nom de fichier se termine par .pfm.
Definition: image_hdr.cpp:171
bool is_hdr_image(const char *filename)
renvoie vrai si le nom de fichier se termine par .hdr.
Definition: image_hdr.cpp:10
Image read_image_hdr(const char *filename)
Definition: image_hdr.cpp:16
int write_image_hdr(const Image &image, const char *filename)
enregistre une image dans un fichier .hdr.
Definition: image_hdr.cpp:56
representation d'une couleur (rgba) transparente ou opaque.
Definition: color.h:14