| 1 | #include "cvd/internal/io/png.h" |
| 2 | #include "cvd/image_io.h" |
| 3 | #include "cvd_src/config_internal.h" |
| 4 | |
| 5 | #include <cstdlib> |
| 6 | #include <iostream> |
| 7 | #include <png.h> |
| 8 | |
| 9 | using namespace CVD; |
| 10 | using namespace CVD::Exceptions; |
| 11 | using namespace CVD::Exceptions::Image_IO; |
| 12 | using namespace PNG; |
| 13 | using namespace std; |
| 14 | |
| 15 | static void png_set_swap_if_necessary(png_structp png_ptr, int depth) |
| 16 | { |
| 17 | #ifdef CVD_INTERNAL_ARCH_LITTLE_ENDIAN |
| 18 | if(depth == 16) |
| 19 | png_set_swap(png_ptr); |
| 20 | #elif defined CVD_INTERNAL_ARCH_BIG_ENDIAN |
| 21 | #else |
| 22 | #error No endianness specified |
| 23 | #endif |
| 24 | } |
| 25 | |
| 26 | //////////////////////////////////////////////////////////////////////////////// |
| 27 | // |
| 28 | // C++ istreams based I/O functions |
| 29 | // |
| 30 | static void error_fn(png_structp png_ptr, png_const_charp error_msg) |
| 31 | { |
| 32 | *(string*)png_get_error_ptr(png_ptr) = error_msg; |
| 33 | } |
| 34 | |
| 35 | static void warn_fn(png_structp, png_const_charp) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | static void read_fn(png_structp png_ptr, unsigned char* data, size_t numbytes) |
| 40 | { |
| 41 | istream* i = (istream*)png_get_io_ptr(png_ptr); |
| 42 | i->read((char*)data, numbytes); |
| 43 | |
| 44 | //What to do on stream failure? |
| 45 | //There is no return value, and I do not know if longjmp is safe here. |
| 46 | //Anyway, multiple rereads of the data will cause the PNG read |
| 47 | //to fail because it has internal checksums |
| 48 | } |
| 49 | |
| 50 | static void write_fn(png_structp png_ptr, unsigned char* data, size_t numbytes) |
| 51 | { |
| 52 | ostream* o = (ostream*)png_get_io_ptr(png_ptr); |
| 53 | o->write((char*)data, numbytes); |
| 54 | } |
| 55 | |
| 56 | static void flush_fn(png_structp png_ptr) |
| 57 | { |
| 58 | ostream* o = (ostream*)png_get_io_ptr(png_ptr); |
| 59 | (*o) << flush; |
| 60 | } |
| 61 | |
| 62 | //////////////////////////////////////////////////////////////////////////////// |
| 63 | // |
| 64 | // Implementation of PNGPimpl |
| 65 | // |
| 66 | |
| 67 | class CVD::PNG::PNGPimpl |
| 68 | { |
| 69 | public: |
| 70 | template <class C> |
| 71 | void read_pixels(C*); |
| 72 | PNGPimpl(std::istream& in); |
| 73 | ~PNGPimpl(); |
| 74 | std::string datatype(); |
| 75 | std::string name(); |
| 76 | ImageRef size(); |
| 77 | |
| 78 | private: |
| 79 | std::istream& i; |
| 80 | std::string type; |
| 81 | unsigned long row; |
| 82 | png_structp png_ptr; |
| 83 | png_infop info_ptr, end_info; |
| 84 | |
| 85 | std::string error_string; |
| 86 | ImageRef my_size; |
| 87 | }; |
| 88 | |
| 89 | //////////////////////////////////////////////////////////////////////////////// |
| 90 | // |
| 91 | // PNG reading functions |
| 92 | // |
| 93 | |
| 94 | string PNGPimpl::datatype() |
| 95 | { |
| 96 | return type; |
| 97 | } |
| 98 | |
| 99 | string PNGPimpl::name() |
| 100 | { |
| 101 | return "PNG"; |
| 102 | } |
| 103 | |
| 104 | ImageRef PNGPimpl::size() |
| 105 | { |
| 106 | return my_size; |
| 107 | } |
| 108 | |
| 109 | #ifdef CVD_INTERNAL_VERBOSE_PNG |
| 110 | #include <map> |
| 111 | |
| 112 | #define LOG(X) \ |
| 113 | do \ |
| 114 | { \ |
| 115 | cerr << X; \ |
| 116 | } while(0) |
| 117 | |
| 118 | static string lookup_color_type(int i) |
| 119 | { |
| 120 | map<int, string> m; |
| 121 | #define ADD(X) m[X] = #X |
| 122 | ADD(PNG_COLOR_TYPE_GRAY); |
| 123 | ADD(PNG_COLOR_TYPE_GRAY_ALPHA); |
| 124 | ADD(PNG_COLOR_TYPE_PALETTE); |
| 125 | ADD(PNG_COLOR_TYPE_RGB); |
| 126 | ADD(PNG_COLOR_TYPE_RGB_ALPHA); |
| 127 | ADD(PNG_COLOR_MASK_PALETTE); |
| 128 | ADD(PNG_COLOR_MASK_COLOR); |
| 129 | ADD(PNG_COLOR_MASK_ALPHA); |
| 130 | |
| 131 | return m[i]; |
| 132 | } |
| 133 | #else |
| 134 | #define LOG(X) |
| 135 | #endif |
| 136 | |
| 137 | template <class P> |
| 138 | void PNGPimpl::read_pixels(P* data) |
| 139 | { |
| 140 | if(datatype() != PNM::type_name<P>::name()) |
| 141 | throw ReadTypeMismatch(datatype(), PNM::type_name<P>::name()); |
| 142 | |
| 143 | if(row > (unsigned long)my_size.y) |
| 144 | throw InternalLibraryError("CVD", "Read past end of image."); |
| 145 | |
| 146 | if(setjmp(png_jmpbuf(png_ptr))) |
| 147 | throw Exceptions::Image_IO::MalformedImage(error_string); |
| 148 | |
| 149 | unsigned char* cptr = reinterpret_cast<unsigned char*>(data); |
| 150 | unsigned char** row_ptr = &cptr; |
| 151 | |
| 152 | png_read_rows(png_ptr, row_ptr, NULL, 1); |
| 153 | } |
| 154 | |
| 155 | template <class T> |
| 156 | int safe_cast(const T&) = delete; |
| 157 | int safe_cast(png_uint_32 t) |
| 158 | { |
| 159 | if(t > numeric_limits<int>::max()) |
| 160 | throw Exceptions::Image_IO::UnsupportedImageSubType("PNG", "dimension over INT_MAX"); |
| 161 | |
| 162 | return static_cast<int>(t); |
| 163 | } |
| 164 | |
| 165 | PNGPimpl::PNGPimpl(std::istream& in) |
| 166 | : i(in) |
| 167 | , type("") |
| 168 | , row(0) |
| 169 | , png_ptr(0) |
| 170 | , info_ptr(0) |
| 171 | , end_info(0) |
| 172 | { |
| 173 | //Read the header and make sure it really is a PNG... |
| 174 | unsigned char header[8]; |
| 175 | |
| 176 | in.read((char*)header, 8); |
| 177 | |
| 178 | if(png_sig_cmp(header, 0, 8)) |
| 179 | throw Exceptions::Image_IO::MalformedImage("Not a PNG image"); |
| 180 | |
| 181 | LOG("PNG header found\n"); |
| 182 | |
| 183 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 184 | |
| 185 | if(!png_ptr) |
| 186 | throw Exceptions::OutOfMemory(); |
| 187 | |
| 188 | info_ptr = png_create_info_struct(png_ptr); |
| 189 | if(!info_ptr) |
| 190 | { |
| 191 | png_destroy_read_struct(&png_ptr, NULL, NULL); |
| 192 | throw Exceptions::OutOfMemory(); |
| 193 | } |
| 194 | |
| 195 | end_info = png_create_info_struct(png_ptr); |
| 196 | if(!info_ptr) |
| 197 | { |
| 198 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
| 199 | throw Exceptions::OutOfMemory(); |
| 200 | } |
| 201 | |
| 202 | if(setjmp(png_jmpbuf(png_ptr))) |
| 203 | { |
| 204 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 205 | throw Exceptions::Image_IO::MalformedImage(error_string); |
| 206 | } |
| 207 | |
| 208 | png_set_error_fn(png_ptr, &error_string, error_fn, warn_fn); |
| 209 | |
| 210 | //png_init_io(png_ptr, stdin); |
| 211 | |
| 212 | png_set_read_fn(png_ptr, &i, read_fn); |
| 213 | |
| 214 | png_set_sig_bytes(png_ptr, 8); |
| 215 | |
| 216 | png_read_info(png_ptr, info_ptr); |
| 217 | |
| 218 | png_uint_32 w, h; |
| 219 | int colour, interlace, dummy, depth; |
| 220 | |
| 221 | png_get_IHDR(png_ptr, info_ptr, &w, &h, &depth, &colour, &interlace, &dummy, &dummy); |
| 222 | |
| 223 | LOG("w = " << w << endl); |
| 224 | LOG("h = " << h << endl); |
| 225 | LOG("depth = " << depth << endl); |
| 226 | LOG("colour = " << colour << ": " << lookup_color_type(colour) << endl); |
| 227 | LOG(" palette = " << (colour & PNG_COLOR_MASK_PALETTE) << endl); |
| 228 | LOG(" color = " << (colour & PNG_COLOR_MASK_COLOR) << endl); |
| 229 | LOG(" alpha = " << (colour & PNG_COLOR_MASK_ALPHA) << endl); |
| 230 | LOG("interlace = " << interlace << endl); |
| 231 | LOG("channels = " << (int)png_get_channels(png_ptr, info_ptr) << endl); |
| 232 | |
| 233 | LOG("tRNS? = " << png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) << endl); |
| 234 | |
| 235 | my_size.x = safe_cast(w); |
| 236 | my_size.y = safe_cast(h); |
| 237 | |
| 238 | //Figure out the type name, and what processing to to. |
| 239 | if(depth == 1) |
| 240 | { |
| 241 | //Unpack bools to bytes to ease loading. |
| 242 | png_set_packing(png_ptr); |
| 243 | type = PNM::type_name<bool>::name(); |
| 244 | } |
| 245 | else if(depth <= 8) |
| 246 | { |
| 247 | //Expand nonbool colour depths up to 8bpp |
| 248 | if(depth < 8) |
| 249 | png_set_expand_gray_1_2_4_to_8(png_ptr); |
| 250 | |
| 251 | type = PNM::type_name<unsigned char>::name(); |
| 252 | } |
| 253 | else |
| 254 | type = PNM::type_name<unsigned short>::name(); |
| 255 | |
| 256 | //Get rid of palette, by transforming it to RGB |
| 257 | if(colour == PNG_COLOR_TYPE_PALETTE) |
| 258 | { |
| 259 | png_set_palette_to_rgb(png_ptr); |
| 260 | |
| 261 | //Check to see if there is a tRNS palette chunk. Note that the PNG_COLOR_MASK_ALPHA is |
| 262 | //only valid for non indexed images, not paletted ones. So, we need to check here for |
| 263 | //transparency data. |
| 264 | if(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) |
| 265 | { |
| 266 | colour |= PNG_COLOR_MASK_ALPHA; |
| 267 | png_set_tRNS_to_alpha(png_ptr); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if(colour & PNG_COLOR_MASK_COLOR) |
| 272 | if(colour & PNG_COLOR_MASK_ALPHA) |
| 273 | type = "CVD::Rgba<" + type + ">"; |
| 274 | else |
| 275 | type = "CVD::Rgb<" + type + ">"; |
| 276 | else if(colour & PNG_COLOR_MASK_ALPHA) |
| 277 | type = "CVD::GreyAlpha<" + type + ">"; |
| 278 | |
| 279 | if(interlace != PNG_INTERLACE_NONE) |
| 280 | throw Exceptions::Image_IO::UnsupportedImageSubType("PNG", "Interlace not yet supported"); |
| 281 | |
| 282 | png_set_swap_if_necessary(png_ptr, depth); |
| 283 | } |
| 284 | |
| 285 | PNGPimpl::~PNGPimpl() |
| 286 | { |
| 287 | //Clear the stream of any remaining PNG bits. |
| 288 | //It doesn't matter if there's an error here |
| 289 | if(!setjmp(png_jmpbuf(png_ptr))) |
| 290 | png_read_end(png_ptr, end_info); |
| 291 | |
| 292 | //Destroy all PNG structs |
| 293 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 294 | } |
| 295 | |
| 296 | //////////////////////////////////////////////////////////////////////////////// |
| 297 | // |
| 298 | // Implementation of PNG reader class |
| 299 | // |
| 300 | |
| 301 | Reader::~Reader() |
| 302 | { |
| 303 | } |
| 304 | |
| 305 | Reader::Reader(istream& i) |
| 306 | : p(new PNGPimpl(i)) |
| 307 | { |
| 308 | } |
| 309 | |
| 310 | string Reader::datatype() |
| 311 | { |
| 312 | return p->datatype(); |
| 313 | } |
| 314 | |
| 315 | string Reader::name() |
| 316 | { |
| 317 | return p->name(); |
| 318 | } |
| 319 | |
| 320 | ImageRef Reader::size() |
| 321 | { |
| 322 | return p->size(); |
| 323 | } |
| 324 | |
| 325 | bool Reader::top_row_first() |
| 326 | { |
| 327 | return true; |
| 328 | } |
| 329 | //Mechanically generate the pixel reading calls. |
| 330 | #define GEN1(X) \ |
| 331 | void Reader::get_raw_pixel_line(X* d) { p->read_pixels(d); } |
| 332 | #define GEN3(X) \ |
| 333 | GEN1(X) \ |
| 334 | GEN1(Rgb<X>) \ |
| 335 | GEN1(Rgba<X>) |
| 336 | GEN1(bool) |
| 337 | GEN3(unsigned char) |
| 338 | GEN3(unsigned short) |
| 339 | |
| 340 | //////////////////////////////////////////////////////////////////////////////// |
| 341 | // |
| 342 | // PNG writing functions. |
| 343 | // |
| 344 | |
| 345 | class CVD::PNG::WriterPimpl |
| 346 | { |
| 347 | public: |
| 348 | WriterPimpl(std::ostream&, ImageRef size, const std::string& type); |
| 349 | ~WriterPimpl(); |
| 350 | template <class P> |
| 351 | void write_line(const P*); |
| 352 | |
| 353 | private: |
| 354 | long row; |
| 355 | std::ostream& o; |
| 356 | ImageRef size; |
| 357 | std::string type; |
| 358 | std::string error_string; |
| 359 | |
| 360 | png_structp png_ptr; |
| 361 | png_infop info_ptr; |
| 362 | }; |
| 363 | |
| 364 | WriterPimpl::WriterPimpl(ostream& out, ImageRef sz, const string& type_) |
| 365 | : row(0) |
| 366 | , o(out) |
| 367 | , size(sz) |
| 368 | , type(type_) |
| 369 | { |
| 370 | //Create required structs |
| 371 | png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, &error_string, error_fn, warn_fn); |
| 372 | if(!png_ptr) |
| 373 | throw Exceptions::OutOfMemory(); |
| 374 | |
| 375 | info_ptr = png_create_info_struct(png_ptr); |
| 376 | if(!info_ptr) |
| 377 | { |
| 378 | png_destroy_write_struct(&png_ptr, NULL); |
| 379 | throw Exceptions::OutOfMemory(); |
| 380 | } |
| 381 | |
| 382 | //Set up error handling |
| 383 | if(setjmp(png_jmpbuf(png_ptr))) |
| 384 | { |
| 385 | png_destroy_write_struct(&png_ptr, &info_ptr); |
| 386 | throw Exceptions::Image_IO::WriteError(error_string); |
| 387 | } |
| 388 | |
| 389 | //Set up stream IO |
| 390 | png_set_write_fn(png_ptr, &o, write_fn, flush_fn); |
| 391 | |
| 392 | int c_type = 0; |
| 393 | int depth = 0; |
| 394 | |
| 395 | if(type == "bool") |
| 396 | { |
| 397 | c_type = PNG_COLOR_TYPE_GRAY; |
| 398 | depth = 1; |
| 399 | } |
| 400 | else if(type == "unsigned char") |
| 401 | { |
| 402 | c_type = PNG_COLOR_TYPE_GRAY; |
| 403 | depth = 8; |
| 404 | } |
| 405 | else if(type == "unsigned short") |
| 406 | { |
| 407 | c_type = PNG_COLOR_TYPE_GRAY; |
| 408 | depth = 16; |
| 409 | } |
| 410 | else if(type == "CVD::Rgb<unsigned char>") |
| 411 | { |
| 412 | c_type = PNG_COLOR_TYPE_RGB; |
| 413 | depth = 8; |
| 414 | } |
| 415 | else if(type == "CVD::Rgb8") |
| 416 | { |
| 417 | c_type = PNG_COLOR_TYPE_RGB; |
| 418 | depth = 8; |
| 419 | //Note the existence of meaningless filler. |
| 420 | png_set_filler(png_ptr, 0, PNG_FILLER_AFTER); |
| 421 | } |
| 422 | else if(type == "CVD::Rgb<unsigned short>") |
| 423 | { |
| 424 | c_type = PNG_COLOR_TYPE_RGB; |
| 425 | depth = 16; |
| 426 | } |
| 427 | else if(type == "CVD::Rgba<unsigned char>") |
| 428 | { |
| 429 | c_type = PNG_COLOR_TYPE_RGB_ALPHA; |
| 430 | depth = 8; |
| 431 | } |
| 432 | else if(type == "CVD::Rgba<unsigned short>") |
| 433 | { |
| 434 | c_type = PNG_COLOR_TYPE_RGB_ALPHA; |
| 435 | depth = 16; |
| 436 | } |
| 437 | else |
| 438 | throw UnsupportedImageSubType("TIFF", type); |
| 439 | |
| 440 | //Set up the image type |
| 441 | png_set_IHDR(png_ptr, info_ptr, size.x, size.y, depth, c_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
| 442 | |
| 443 | //Write the header |
| 444 | png_write_info(png_ptr, info_ptr); |
| 445 | |
| 446 | //Write the transformations |
| 447 | png_set_swap_if_necessary(png_ptr, depth); |
| 448 | |
| 449 | //Pack from C++ bools to packed PNG bools |
| 450 | //This has to be done _after_ writing the info struct. |
| 451 | if(type == "bool") |
| 452 | png_set_packing(png_ptr); |
| 453 | } |
| 454 | |
| 455 | //////////////////////////////////////////////////////////////////////////////// |
| 456 | // |
| 457 | // Main interface funtions |
| 458 | // |
| 459 | |
| 460 | template <class P> |
| 461 | void WriterPimpl::write_line(const P* data) |
| 462 | { |
| 463 | unsigned char* chardata = const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(data)); |
| 464 | |
| 465 | //Do some type checking |
| 466 | if(type != PNM::type_name<P>::name()) |
| 467 | throw WriteTypeMismatch(type, PNM::type_name<P>::name()); |
| 468 | |
| 469 | //Set up error handling |
| 470 | if(setjmp(png_jmpbuf(png_ptr))) |
| 471 | throw Exceptions::Image_IO::WriteError(error_string); |
| 472 | |
| 473 | //Do some sanity checking |
| 474 | if(row > size.y) |
| 475 | throw InternalLibraryError("CVD", "Write past end of image."); |
| 476 | |
| 477 | unsigned char** row_ptr = &chardata; |
| 478 | png_write_rows(png_ptr, row_ptr, 1); |
| 479 | |
| 480 | row++; |
| 481 | } |
| 482 | |
| 483 | WriterPimpl::~WriterPimpl() |
| 484 | { |
| 485 | png_write_end(png_ptr, info_ptr); |
| 486 | png_destroy_write_struct(&png_ptr, &info_ptr); |
| 487 | } |
| 488 | |
| 489 | png_writer::png_writer(std::ostream& o, ImageRef size, const std::string& type, const std::map<std::string, Parameter<>>&) |
| 490 | : p(new WriterPimpl(o, size, type)) |
| 491 | { |
| 492 | } |
| 493 | |
| 494 | png_writer::~png_writer() |
| 495 | { |
| 496 | } |
| 497 | //Mechanically generate the pixel writing calls. |
| 498 | #undef GEN1 |
| 499 | #undef GEN3 |
| 500 | #define GEN1(X) \ |
| 501 | void png_writer::write_raw_pixel_line(const X* d) { p->write_line(d); } |
| 502 | #define GEN3(X) \ |
| 503 | GEN1(X) \ |
| 504 | GEN1(Rgb<X>) \ |
| 505 | GEN1(Rgba<X>) |
| 506 | GEN1(bool) |
| 507 | GEN1(Rgb8) |
| 508 | GEN3(unsigned char) |
| 509 | GEN3(unsigned short) |