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]));
219 std::ofstream out(full_name, std::ios::binary);
221 out <<
"P6" << std::endl;
222 out << width <<
" " << height << std::endl;
225 std::streambuf *sb=out.rdbuf();
230 pstep=(width>>2)*6+px;
234 pstep=(width>>2)*8+px;
238 for (
size_t k=0; k<height && out.good(); k++)
240 for (
size_t i=0; i<width; i+=4)
253 for (
int j=0; j<12; j++)
255 sb->sputc(static_cast<char>(rgb[j]));
268 std::unique_ptr<uint8_t []> rgb_pixel(
new uint8_t [3*width*height]);
272 p+=(3*width+px)*yoffset;
276 p+=(width+px)*yoffset;
279 if (
convertImage(rgb_pixel.get(), 0, p, format, width, height, px))
284 std::ofstream out(full_name, std::ios::binary);
286 out <<
"P6" << std::endl;
287 out << width <<
" " << height << std::endl;
290 std::streambuf *sb=out.rdbuf();
292 for (
size_t k=0; k<height && out.good(); k++)
294 for (
size_t i=0; i<width; i++)
296 sb->sputc(static_cast<char>(*p++));
297 sb->sputc(static_cast<char>(*p++));
298 sb->sputc(static_cast<char>(*p++));
306 throw IOException(std::string(
"storeImage(): Unsupported pixel format: ")+
307 GetPixelFormatName(static_cast<PfncFormat>(image.
getPixelFormat())));
318 std::string storeImagePNG(
const std::string &name,
const Image &image,
size_t yoffset,
324 if (height == 0) height=real_height;
326 yoffset=std::min(yoffset, real_height);
327 height=std::min(height, real_height-yoffset);
329 const unsigned char *p=
static_cast<const unsigned char *
>(image.
getPixels());
334 std::string full_name;
345 FILE *out=fopen(full_name.c_str(),
"wb");
349 throw new IOException(
"Cannot store file: "+full_name);
352 png_structp png=png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
353 png_infop info=png_create_info_struct(png);
354 setjmp(png_jmpbuf(png));
358 png_init_io(png, out);
359 png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_GRAY,
360 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
361 PNG_FILTER_TYPE_DEFAULT);
362 png_write_info(png, info);
366 p+=(width+px)*yoffset;
367 for (
size_t k=0; k<height; k++)
369 png_write_row(png, const_cast<png_bytep>(p));
375 png_write_end(png, info);
377 png_destroy_write_struct(&png, &info);
387 FILE *out=fopen(full_name.c_str(),
"wb");
391 throw new IOException(
"Cannot store file: "+full_name);
394 png_structp png=png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
395 png_infop info=png_create_info_struct(png);
396 setjmp(png_jmpbuf(png));
400 png_init_io(png, out);
401 png_set_IHDR(png, info, width, height, 16, PNG_COLOR_TYPE_GRAY,
402 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
403 PNG_FILTER_TYPE_DEFAULT);
404 png_write_info(png, info);
413 p+=(2*width+px)*yoffset;
414 for (
size_t k=0; k<height; k++)
416 png_write_row(png, const_cast<png_bytep>(p));
422 png_write_end(png, info);
424 png_destroy_write_struct(&png, &info);
435 FILE *out=fopen(full_name.c_str(),
"wb");
439 throw new IOException(
"Cannot store file: "+full_name);
442 png_structp png=png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
443 png_infop info=png_create_info_struct(png);
444 setjmp(png_jmpbuf(png));
448 png_init_io(png, out);
449 png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGB,
450 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
451 PNG_FILTER_TYPE_DEFAULT);
452 png_write_info(png, info);
456 uint8_t *tmp=
new uint8_t [3*width];
461 pstep=(width>>2)*6+px;
465 pstep=(width>>2)*8+px;
469 for (
size_t k=0; k<height; k++)
473 for (
size_t i=0; i<width; i+=4)
480 for (
size_t i=0; i<width; i+=4)
486 png_write_row(png, tmp);
492 png_write_end(png, info);
494 png_destroy_write_struct(&png, &info);
500 std::unique_ptr<uint8_t []> rgb_pixel(
new uint8_t [3*width*height]);
504 p+=(3*width+px)*yoffset;
508 p+=(width+px)*yoffset;
511 if (
convertImage(rgb_pixel.get(), 0, p, format, width, height, px))
518 FILE *out=fopen(full_name.c_str(),
"wb");
522 throw new IOException(
"Cannot store file: "+full_name);
525 png_structp png=png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
526 png_infop info=png_create_info_struct(png);
527 setjmp(png_jmpbuf(png));
531 png_init_io(png, out);
532 png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGB,
533 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
534 PNG_FILTER_TYPE_DEFAULT);
535 png_write_info(png, info);
539 for (
size_t k=0; k<height; k++)
541 png_write_row(png, p);
547 png_write_end(png, info);
549 png_destroy_write_struct(&png, &info);
553 throw IOException(std::string(
"storeImage(): Unsupported pixel format: ")+
554 GetPixelFormatName(static_cast<PfncFormat>(image.
getPixelFormat())));
568 size_t yoffset,
size_t height)
576 ret=storeImagePNG(name, image, yoffset, height);
578 throw IOException(
"storeImage(): Support for PNG image file format is not compiled in!");
584 ret=storeImagePNM(name, image, yoffset, height);
592 float scale,
float offset)
596 throw IOException(std::string(
"storeImageAsDisparityPFM(): Format Coord3D_C16 expected: ")+
597 GetPixelFormatName(static_cast<PfncFormat>(image.
getPixelFormat())));
602 throw IOException(std::string(
"storeImageAsDisparityPFM(): Scale must not be 0!"));
610 const unsigned char *p=
static_cast<const unsigned char *
>(image.
getPixels())+
611 2*(width+px)*(height+1);
614 std::ofstream out(full_name, std::ios::binary);
616 out <<
"Pf" << std::endl;
617 out << width <<
" " << height << std::endl;
620 std::streambuf *sb=out.rdbuf();
628 char *cc=
reinterpret_cast<char *
>(&pp);
629 msbfirst=(cc[0] != 1);
632 for (
size_t k=0; k<height && out.good(); k++)
635 for (
size_t i=0; i<width; i++)
640 val=(
static_cast<int>(p[0])<<8)|p[1];
644 val=(
static_cast<int>(p[1])<<8)|p[0];
649 float d=std::numeric_limits<float>::infinity();
652 d=
static_cast<float>(val*scale+offset);
655 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.
const uint8_t * getPixels() const
Pointer to pixel information of the image.
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...
void convYCbCr422toQuadRGB(uint8_t rgb[12], const uint8_t *row, int i)
Conversion of a group of four pixels from YCbCr422 format (8 bytes for 4 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.
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.