42 #include "../common/ojph_img_io.h"
43 #include "../common/ojph_mem.h"
60 comps[0] = comps[1] = comps[2] = 0;
65 for (
ui32 i = 0; i < num_comps; ++i)
67 if (comps[i])
delete[] comps[i];
72 void init(
ui32 num_comps,
size_t width,
size_t height,
ui32 max_val,
75 assert(num_comps <= 3 && comps[0] == NULL);
76 this->num_comps = num_comps;
78 this->height = height;
79 this->format = format;
80 this->max_val = max_val;
81 for (
ui32 i = 0; i < num_comps; ++i)
86 downsampling[i].x = downsampling[i].y = 1;
89 downsampling[i].x = i == 0 ? 1 : 2;
90 downsampling[i].y = 1;
93 downsampling[i].x = i == 0 ? 1 : 2;
94 downsampling[i].y = i == 0 ? 1 : 2;
99 for (
ui32 i = 0; i < num_comps; ++i)
101 size_t w = (this->width + downsampling[i].x - 1) / downsampling[i].x;
102 size_t h = (this->height + downsampling[i].x - 1) / downsampling[i].x;
103 comps[i] =
new si32[w * h];
108 return comps[0] != NULL;
121 size_t len = strlen(filename);
122 if (len >= 4 && filename[len - 4] ==
'.' &&
123 toupper(filename[len - 3]) ==
'P' &&
124 (toupper(filename[len - 2])==
'P' || toupper(filename[len - 2]) ==
'G') &&
125 toupper(filename[len - 1]) ==
'M')
141 width = calc_aligned_size<si32, byte_alignment>(width);
144 line.
wrap(buffer, width, 0);
146 for (
ui32 c = 0; c < num_comps; ++c)
149 for (
ui32 h = 0; h < height; ++h)
152 memcpy(p, line.
i32, w *
sizeof(
si32));
162 const char *p = strchr(filename,
':');
163 if (p != NULL && p - filename >= 4 && p[-4] ==
'.' &&
164 toupper(p[-3]) ==
'Y' && toupper(p[-2])==
'U' && toupper(p[-1]) ==
'V')
171 const char *p = strchr(filename,
':');
172 const char *name_end = p;
174 printf(
"A .yuv that does not have the expected format, which is\n");
175 printf(
".yuv:widthxheightxbitdepthxformat, where format is\n");
176 printf(
"either 444, 422, or 420\n");
181 s.
w = (
ui32)atoi(++p);
184 printf(
"Expecting image height.\n");
185 printf(
"A .yuv that does not have the expected format, which is\n");
186 printf(
".yuv:widthxheightxbitdepthxformat, where format is\n");
187 printf(
"either 444, 422, or 420\n");
190 s.
h = (
ui32)atoi(++p);
193 printf(
"Expecting image bitdepth.\n");
194 printf(
"A .yuv that does not have the expected format, which is\n");
195 printf(
".yuv:widthxheightxbitdepthxformat, where format is\n");
196 printf(
"either 444, 422, or 420\n");
202 printf(
"Expecting color subsampling format.\n");
203 printf(
"A .yuv that does not have the expected format, which is\n");
204 printf(
".yuv:widthxheightxbitdepthxformat, where format is\n");
205 printf(
"either 444, 422, or 420\n");
210 size_t len = strlen(p);
213 printf(
"Image color format must have 3 characters, %s was supplied.\n", p);
214 printf(
"A .yuv that does not have the expected format, which is\n");
215 printf(
".yuv:widthxheightxbitdepthxformat, where format is\n");
216 printf(
"either 444, 422, or 420\n");
222 if (strcmp(p,
"444") == 0)
227 else if (strcmp(p,
"422") == 0)
231 downsampling[1].
x = downsampling[2].
x = 2;
233 else if (strcmp(p,
"420") == 0)
237 downsampling[1].
x = downsampling[2].
x = 2;
238 downsampling[1].
y = downsampling[2].
y = 2;
240 else if (strcmp(p,
"400") == 0)
246 printf(
"Unknown image color format, %s.\n", p);
251 ptrdiff_t cpy_len = name_end - filename > 2047 ? 2047 : name_end - filename;
252 strncpy(name_buf, filename, (
size_t)cpy_len);
253 name_buf[cpy_len] = 0;
256 ui32 depths[3] = {bit_depth, bit_depth, bit_depth};
261 img.
init(num_comps, s.
w, s.
h, (1 << bit_depth) - 1, format);
263 size_t w = calc_aligned_size<si32, byte_alignment>(s.
w);
266 line.
wrap(buffer, w, 0);
268 for (
ui32 c = 0; c < num_comps; ++c)
272 for (
ui32 h = 0; h < height; ++h)
275 memcpy(p, line.
i32, w *
sizeof(
si32));
284 float &psnr,
ui32 &pae)
290 printf(
"Error: mismatching images\n");
293 size_t mse[3] = { 0 };
295 size_t num_pixels = 0;
302 for (
ui32 v = 0; v < h; ++v)
306 for (
ui32 s = 0; s < w; ++s)
308 si32 err = *p0++ - *p1++;
309 ui32 ae = (
ui32)(err > 0 ? err : -err);
311 pae = ae > pae ? ae : pae;
318 t /= (float)num_pixels;
319 psnr = 10.0f * log10f((
float)img1.
max_val * (
float)img1.
max_val / t);
322 int main(
int argc,
char *argv[])
326 printf(
"psnr_pae expects two arguments <filename1, filename2>\n");
336 printf(
"psnr_pae does not know file format of %s\n", argv[1]);
337 printf(
"or a .yuv that does not have the expected format, which is\n");
338 printf(
".yuv:widthxheightxbitdepthxformat, where format is\n");
339 printf(
"either 444, 422, or 420\n");
348 printf(
"psnr_pae does not know file format of %s\n", argv[2]);
349 printf(
"or a .yuv that does not have the expected format, which is\n");
350 printf(
".yuv:widthxheightxbitdepthxformat, where format is\n");
351 printf(
"either 444, 422, or 420\n");
355 float psnr;
ui32 pae;
358 printf(
"%f %d\n", psnr, pae);
void open(const char *filename)
ui32 get_num_components()
void set_planar(bool planar)
virtual ui32 read(const line_buf *line, ui32 comp_num)
virtual ui32 read(const line_buf *line, ui32 comp_num)
void open(const char *filename)
void set_img_props(const size &s, ui32 num_components, ui32 num_downsampling, const point *downsampling)
void set_bit_depth(ui32 num_bit_depths, ui32 *bit_depth)
int main(int argc, char *argv[])
bool is_yuv(const char *filename)
void find_psnr_pae(const img_info &img1, const img_info &img2, float &psnr, ui32 &pae)
bool is_pnm(const char *filename)
void load_ppm(const char *filename, img_info &img)
void load_yuv(const char *filename, img_info &img)
void init(ui32 num_comps, size_t width, size_t height, ui32 max_val, ui32 format=FORMAT444)
void wrap(T *buffer, size_t num_ele, ui32 pre_size)