64 std::ifstream file(name);
74 size_t i=name.rfind(
'.');
75 if (i != name.npos && name.size()-i <= 4)
77 suffix=name.substr(i);
78 name=name.substr(0, i);
87 s << name <<
"_" << n << suffix;
107 class IOException :
public std::exception
111 IOException(
const std::string &_msg) { msg=_msg; }
112 virtual const char *what()
const noexcept {
return msg.c_str(); }
119 std::string storeImagePNM(
const std::string &name,
const Image &image,
size_t yoffset,
125 if (height == 0) height=real_height;
127 yoffset=std::min(yoffset, real_height);
128 height=std::min(height, real_height-yoffset);
130 const unsigned char *p=
static_cast<const unsigned char *
>(image.
getPixels());
135 std::string full_name;
144 std::ofstream out(full_name, std::ios::binary);
146 out <<
"P5" << std::endl;
147 out << width <<
" " << height << std::endl;
150 std::streambuf *sb=out.rdbuf();
152 p+=(width+px)*yoffset;
153 for (
size_t k=0; k<height && out.good(); k++)
155 for (
size_t i=0; i<width; i++)
157 sb->sputc(static_cast<char>(*p++));
171 std::ofstream out(full_name, std::ios::binary);
173 out <<
"P5" << std::endl;
174 out << width <<
" " << height << std::endl;
175 out << 65535 <<
"\n";
177 std::streambuf *sb=out.rdbuf();
181 p+=(2*width+px)*yoffset;
184 for (
size_t k=0; k<height && out.good(); k++)
186 for (
size_t i=0; i<width; i++)
188 sb->sputc(static_cast<char>(*p++));
189 sb->sputc(static_cast<char>(*p++));
197 for (
size_t k=0; k<height && out.good(); k++)
199 for (
size_t i=0; i<width; i++)
201 sb->sputc(static_cast<char>(p[1]));
202 sb->sputc(static_cast<char>(p[0]));
217 std::ofstream out(full_name, std::ios::binary);
219 out <<
"P6" << std::endl;
220 out << width <<
" " << height << std::endl;
223 std::streambuf *sb=out.rdbuf();
225 size_t pstep=(width>>2)*6+px;
227 for (
size_t k=0; k<height && out.good(); k++)
229 for (
size_t i=0; i<width; i+=4)
234 for (
int j=0; j<12; j++)
236 sb->sputc(static_cast<char>(rgb[j]));
249 std::unique_ptr<uint8_t []> rgb_pixel(
new uint8_t [3*width*height]);
253 p+=(3*width+px)*yoffset;
257 p+=(width+px)*yoffset;
260 if (
convertImage(rgb_pixel.get(), 0, p, format, width, height, px))
265 std::ofstream out(full_name, std::ios::binary);
267 out <<
"P6" << std::endl;
268 out << width <<
" " << height << std::endl;
271 std::streambuf *sb=out.rdbuf();
273 for (
size_t k=0; k<height && out.good(); k++)
275 for (
size_t i=0; i<width; i++)
277 sb->sputc(static_cast<char>(*p++));
278 sb->sputc(static_cast<char>(*p++));
279 sb->sputc(static_cast<char>(*p++));
287 throw IOException(std::string(
"storeImage(): Unsupported pixel format: ")+
288 GetPixelFormatName(static_cast<PfncFormat>(image.
getPixelFormat())));
299 std::string storeImagePNG(
const std::string &name,
const Image &image,
size_t yoffset,
305 if (height == 0) height=real_height;
307 yoffset=std::min(yoffset, real_height);
308 height=std::min(height, real_height-yoffset);
310 const unsigned char *p=
static_cast<const unsigned char *
>(image.
getPixels());
315 std::string full_name;
326 FILE *out=fopen(full_name.c_str(),
"wb");
330 throw new IOException(
"Cannot store file: "+full_name);
333 png_structp png=png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
334 png_infop info=png_create_info_struct(png);
335 setjmp(png_jmpbuf(png));
339 png_init_io(png, out);
340 png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_GRAY,
341 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
342 PNG_FILTER_TYPE_DEFAULT);
343 png_write_info(png, info);
347 p+=(width+px)*yoffset;
348 for (
size_t k=0; k<height; k++)
350 png_write_row(png, const_cast<png_bytep>(p));
356 png_write_end(png, info);
358 png_destroy_write_struct(&png, &info);
368 FILE *out=fopen(full_name.c_str(),
"wb");
372 throw new IOException(
"Cannot store file: "+full_name);
375 png_structp png=png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
376 png_infop info=png_create_info_struct(png);
377 setjmp(png_jmpbuf(png));
381 png_init_io(png, out);
382 png_set_IHDR(png, info, width, height, 16, PNG_COLOR_TYPE_GRAY,
383 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
384 PNG_FILTER_TYPE_DEFAULT);
385 png_write_info(png, info);
394 p+=(2*width+px)*yoffset;
395 for (
size_t k=0; k<height; k++)
397 png_write_row(png, const_cast<png_bytep>(p));
403 png_write_end(png, info);
405 png_destroy_write_struct(&png, &info);
414 FILE *out=fopen(full_name.c_str(),
"wb");
418 throw new IOException(
"Cannot store file: "+full_name);
421 png_structp png=png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
422 png_infop info=png_create_info_struct(png);
423 setjmp(png_jmpbuf(png));
427 png_init_io(png, out);
428 png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGB,
429 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
430 PNG_FILTER_TYPE_DEFAULT);
431 png_write_info(png, info);
435 uint8_t *tmp=
new uint8_t [3*width];
437 size_t pstep=(width>>2)*6+px;
440 for (
size_t k=0; k<height; k++)
442 for (
size_t i=0; i<width; i+=4)
447 png_write_row(png, tmp);
453 png_write_end(png, info);
455 png_destroy_write_struct(&png, &info);
461 std::unique_ptr<uint8_t []> rgb_pixel(
new uint8_t [3*width*height]);
465 p+=(3*width+px)*yoffset;
469 p+=(width+px)*yoffset;
472 if (
convertImage(rgb_pixel.get(), 0, p, format, width, height, px))
479 FILE *out=fopen(full_name.c_str(),
"wb");
483 throw new IOException(
"Cannot store file: "+full_name);
486 png_structp png=png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
487 png_infop info=png_create_info_struct(png);
488 setjmp(png_jmpbuf(png));
492 png_init_io(png, out);
493 png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGB,
494 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
495 PNG_FILTER_TYPE_DEFAULT);
496 png_write_info(png, info);
500 for (
size_t k=0; k<height; k++)
502 png_write_row(png, p);
508 png_write_end(png, info);
510 png_destroy_write_struct(&png, &info);
514 throw IOException(std::string(
"storeImage(): Unsupported pixel format: ")+
515 GetPixelFormatName(static_cast<PfncFormat>(image.
getPixelFormat())));
529 size_t yoffset,
size_t height)
537 ret=storeImagePNG(name, image, yoffset, height);
539 throw IOException(
"storeImage(): Support for PNG image file format is not compiled in!");
545 ret=storeImagePNM(name, image, yoffset, height);
553 float scale,
float offset)
557 throw IOException(std::string(
"storeImageAsDisparityPFM(): Format Coord3D_C16 expected: ")+
558 GetPixelFormatName(static_cast<PfncFormat>(image.
getPixelFormat())));
563 throw IOException(std::string(
"storeImageAsDisparityPFM(): Scale must not be 0!"));
571 const unsigned char *p=
static_cast<const unsigned char *
>(image.
getPixels())+
572 2*(width+px)*(height+1);
575 std::ofstream out(full_name, std::ios::binary);
577 out <<
"Pf" << std::endl;
578 out << width <<
" " << height << std::endl;
581 std::streambuf *sb=out.rdbuf();
589 char *cc=
reinterpret_cast<char *
>(&pp);
590 msbfirst=(cc[0] != 1);
593 for (
size_t k=0; k<height && out.good(); k++)
596 for (
size_t i=0; i<width; i++)
601 val=(
static_cast<int>(p[0])<<8)|p[1];
605 val=(
static_cast<int>(p[1])<<8)|p[0];
610 float d=std::numeric_limits<float>::infinity();
613 d=
static_cast<float>(val*scale+offset);
616 char *c=
reinterpret_cast<char *
>(&d);
std::string ensureNewFileName(std::string name)
This method checks if the given file name already exists and produces a new file name if this happens...
The image class encapsulates image information.
std::string storeImage(const std::string &name, ImgFmt fmt, const Image &image, size_t yoffset, size_t height)
Stores the given image.
uint64_t getPixelFormat() const
void convYCbCr411toQuadRGB(uint8_t rgb[12], const uint8_t *row, int i)
Conversion of a group of four pixels from YCbCr411 format (6 bytes for four pixels) to RGB...
bool convertImage(uint8_t *rgb_out, uint8_t *mono_out, const uint8_t *raw, uint64_t pixelformat, size_t width, size_t height, size_t xpadding)
Converts image to RGB and monochrome format.
const uint8_t * getPixels() const
Pointer to pixel information of the image.
size_t getXPadding() const
std::string storeImageAsDisparityPFM(const std::string &name, const Image &image, int inv, float scale, float offset)
Stores the given image as disparity.