gKit2 light
Loading...
Searching...
No Matches
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
10bool is_hdr_image( const char *filename )
11{
12 return (std::string(filename).rfind(".hdr") != std::string::npos);
13}
14
15Image read_image_pfm( const char *filename )
16{
17 FILE *in= fopen(filename, "rb");
18 if(in == nullptr)
19 {
20 printf("[error] loading pfm image '%s'...\n", filename);
21 return Image();
22 }
23
24 int w, h;
25 float endian= 0;
26 if(fscanf(in, "PF\xa%d %d\xa%f[^\xa]", &w, &h, &endian) != 3
27 || endian != -1)
28 {
29 printf("[error] loading pfm image '%s'...\n", filename);
30 return Image();
31 }
32
33 // saute la fin de l'entete
34 unsigned char c= fgetc(in);
35 while(c != '\xa')
36 c= fgetc(in);
37
38 // 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...
39
40 printf("loading pfm image '%s' %dx%d...\n", filename, w, h);
41
42 Image image(w, h);
43
44 for(int y= 0; y < h; y++)
45 for(int x= 0; x < w; x++)
46 {
47 Color pixel;
48 if(fread(&pixel.r, sizeof(float), 3, in) == 3)
49 image(x, y)= pixel;
50 }
51 fclose(in);
52
53 return image;
54}
55
56
58int write_image_pfm( const Image& image, const char *filename )
59{
60 FILE *out= fopen(filename, "wb");
61 if(out == nullptr)
62 {
63 printf("[error] writing pfm image '%s'...\n", filename);
64 return -1;
65 }
66
67 fprintf(out, "PF\xa%d %d\xa-1\xa", image.width(), image.height());
68
69 for(unsigned y= 0; y < image.height(); y++)
70 for(unsigned x= 0; x < image.width(); x++)
71 {
72 Color pixel= image(x, y);
73 fwrite(&pixel.r, sizeof(float), 3, out);
74 }
75 fclose(out);
76
77 printf("writing pfm image '%s'...\n", filename);
78 return 0;
79}
80
81
83bool is_pfm_image( const char *filename )
84{
85 return (std::string(filename).rfind(".pfm") != std::string::npos);
86}
representation d'une image.
Definition image.h:21
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
representation d'une couleur (rgba) transparente ou opaque.
Definition color.h:14