| 1 | |
| 2 | #include <stdio.h> |
| 3 | extern "C" |
| 4 | { |
| 5 | #include <jpeglib.h> |
| 6 | } |
| 7 | #include "cvd/internal/io/jpeg.h" |
| 8 | |
| 9 | #include "cvd/image_io.h" |
| 10 | #include "cvd_src/config_internal.h" |
| 11 | using namespace std; |
| 12 | #include <algorithm> |
| 13 | #include <iomanip> |
| 14 | #include <iostream> |
| 15 | #include <setjmp.h> |
| 16 | |
| 17 | namespace CVD |
| 18 | { |
| 19 | namespace JPEG |
| 20 | { |
| 21 | |
| 22 | class ReadPimpl |
| 23 | { |
| 24 | public: |
| 25 | ReadPimpl(std::istream&); |
| 26 | int channels() { return m_channels; } |
| 27 | int x_size() const { return xs; } |
| 28 | int y_size() const { return ys; } |
| 29 | long elements_per_line() const { return xs * m_channels; } |
| 30 | void get_raw_pixel_lines(unsigned char*, unsigned long nlines); |
| 31 | ~ReadPimpl(); |
| 32 | string datatype() |
| 33 | { |
| 34 | return type; |
| 35 | } |
| 36 | |
| 37 | template <class T> |
| 38 | void get_raw_pixel_line(T* d) |
| 39 | { |
| 40 | if(datatype() != PNM::type_name<T>::name()) |
| 41 | throw CVD::Exceptions::Image_IO::ReadTypeMismatch(datatype(), PNM::type_name<T>::name()); |
| 42 | |
| 43 | get_raw_pixel_lines((unsigned char*)d, 1); |
| 44 | //FIXME: check rows. |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | int xs, ys; |
| 49 | int m_channels; |
| 50 | struct jpeg_decompress_struct cinfo; |
| 51 | struct jpeg_error_mgr jerr; |
| 52 | std::istream& i; |
| 53 | string type; |
| 54 | }; |
| 55 | |
| 56 | ImageRef Reader::size() |
| 57 | { |
| 58 | return ImageRef(t->x_size(), t->y_size()); |
| 59 | } |
| 60 | |
| 61 | void Reader::get_raw_pixel_line(unsigned char* d) |
| 62 | { |
| 63 | t->get_raw_pixel_line(d); |
| 64 | } |
| 65 | |
| 66 | void Reader::get_raw_pixel_line(Rgb<byte>* d) |
| 67 | { |
| 68 | t->get_raw_pixel_line(d); |
| 69 | } |
| 70 | bool Reader::top_row_first() |
| 71 | { |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | string Reader::datatype() |
| 76 | { |
| 77 | return t->datatype(); |
| 78 | } |
| 79 | string Reader::name() |
| 80 | { |
| 81 | return "JPEG"; |
| 82 | } |
| 83 | |
| 84 | Reader::~Reader() |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | Reader::Reader(std::istream& i) |
| 89 | : t(new ReadPimpl(i)) |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | struct jpeg_istream_src : public jpeg_source_mgr |
| 94 | { |
| 95 | |
| 96 | istream* i; |
| 97 | bool eof; |
| 98 | static const int bufsize = CVD_INTERNAL_JPEG_BUFFER_SIZE; |
| 99 | JOCTET buf[bufsize + 2]; |
| 100 | |
| 101 | //Constructor |
| 102 | static void create(j_decompress_ptr p, istream* is) |
| 103 | { |
| 104 | //Allocate memory |
| 105 | p->src = (jpeg_istream_src*)(*p->mem->alloc_small)((jpeg_common_struct*)p, JPOOL_PERMANENT, sizeof(jpeg_istream_src)); |
| 106 | |
| 107 | jpeg_istream_src* me = (jpeg_istream_src*)p->src; |
| 108 | |
| 109 | //Set up virtual member functions |
| 110 | me->init_source = s_init_src; |
| 111 | me->fill_input_buffer = s_fill_input_buffer; |
| 112 | me->skip_input_data = s_skip_input_data; |
| 113 | me->resync_to_restart = jpeg_resync_to_restart; |
| 114 | me->term_source = s_term_source; |
| 115 | |
| 116 | //Set up data members |
| 117 | me->i = is; |
| 118 | me->bytes_in_buffer = 0; |
| 119 | me->next_input_byte = me->buf; |
| 120 | me->eof = 0; |
| 121 | } |
| 122 | |
| 123 | static void s_init_src(j_decompress_ptr p) |
| 124 | { |
| 125 | jpeg_istream_src* me = (jpeg_istream_src*)p->src; |
| 126 | |
| 127 | me->bytes_in_buffer = 0; |
| 128 | } |
| 129 | |
| 130 | static boolean s_fill_input_buffer(j_decompress_ptr p) |
| 131 | { |
| 132 | |
| 133 | int n = 0; |
| 134 | jpeg_istream_src* me = (jpeg_istream_src*)p->src; |
| 135 | me->next_input_byte = me->buf; |
| 136 | |
| 137 | if(me->eof) |
| 138 | { |
| 139 | //Output JPEG end-of-image flags |
| 140 | me->buf[0] = 0xff; |
| 141 | me->buf[1] = JPEG_EOI; |
| 142 | me->bytes_in_buffer = 2; |
| 143 | return TRUE; |
| 144 | } |
| 145 | |
| 146 | int c; |
| 147 | for(n = 0; n < bufsize; n++) |
| 148 | { |
| 149 | //Get a byte... |
| 150 | c = me->i->get(); |
| 151 | |
| 152 | //Check for EOF... |
| 153 | if(c == EOF) |
| 154 | { |
| 155 | me->eof = 1; |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | //Store the byte... |
| 160 | me->buf[n] = static_cast<uint8_t>(c); |
| 161 | } |
| 162 | |
| 163 | me->bytes_in_buffer = n; |
| 164 | |
| 165 | if(me->i->eof()) |
| 166 | me->eof = 1; |
| 167 | |
| 168 | if(me->bytes_in_buffer == 0) |
| 169 | s_fill_input_buffer(p); |
| 170 | |
| 171 | return TRUE; |
| 172 | } |
| 173 | |
| 174 | static void s_skip_input_data(j_decompress_ptr p, long num) |
| 175 | { |
| 176 | jpeg_istream_src* me = (jpeg_istream_src*)p->src; |
| 177 | |
| 178 | if(num > (long)(me->bytes_in_buffer)) |
| 179 | { |
| 180 | me->i->ignore(num - me->bytes_in_buffer); |
| 181 | me->bytes_in_buffer = 0; |
| 182 | } |
| 183 | |
| 184 | else |
| 185 | { |
| 186 | me->next_input_byte += num; |
| 187 | me->bytes_in_buffer -= num; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static void s_term_source(j_decompress_ptr) |
| 192 | { |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | void jumpy_error_exit(j_common_ptr cinfo) |
| 197 | { |
| 198 | //Exceptions don't work from C code, so use setjmp/longjmp to jump back |
| 199 | //in to C++ code. |
| 200 | longjmp(*(jmp_buf*)(cinfo->client_data), 1); |
| 201 | } |
| 202 | |
| 203 | struct jpeg_error_mgr* jumpy_error_manager(struct jpeg_error_mgr* err) |
| 204 | { |
| 205 | //Set up most of the useful defaults |
| 206 | jpeg_std_error(err); |
| 207 | //Except we don't want to exit on an error. |
| 208 | err->error_exit = jumpy_error_exit; |
| 209 | return err; |
| 210 | } |
| 211 | |
| 212 | ReadPimpl::ReadPimpl(istream& in) |
| 213 | : i(in) |
| 214 | { |
| 215 | cinfo.err = jumpy_error_manager(&jerr); |
| 216 | |
| 217 | jmp_buf env; |
| 218 | cinfo.client_data = &env; |
| 219 | |
| 220 | //Catch "exceptions" and throw proper exceptions |
| 221 | if(setjmp(env)) |
| 222 | { |
| 223 | //longjmp called |
| 224 | char buffer[JMSG_LENGTH_MAX]; |
| 225 | (cinfo.err->format_message)((jpeg_common_struct*)&cinfo, buffer); |
| 226 | throw CVD::Exceptions::Image_IO::MalformedImage(string("Error in JPEG image: ") + buffer); |
| 227 | } |
| 228 | |
| 229 | jpeg_create_decompress(&cinfo); |
| 230 | jpeg_istream_src::create(&cinfo, &i); |
| 231 | |
| 232 | jpeg_read_header(&cinfo, TRUE); |
| 233 | jpeg_start_decompress(&cinfo); |
| 234 | |
| 235 | xs = cinfo.output_width; |
| 236 | ys = cinfo.output_height; |
| 237 | |
| 238 | m_channels = cinfo.out_color_components; |
| 239 | |
| 240 | if(m_channels == 1) |
| 241 | type = "unsigned char"; |
| 242 | else |
| 243 | type = "CVD::Rgb<unsigned char>"; |
| 244 | } |
| 245 | |
| 246 | void ReadPimpl::get_raw_pixel_lines(unsigned char* data, unsigned long nlines) |
| 247 | { |
| 248 | jmp_buf env; |
| 249 | cinfo.client_data = &env; |
| 250 | |
| 251 | //Catch "exceptions" and throw proper exceptions |
| 252 | if(setjmp(env)) |
| 253 | { |
| 254 | //longjmp called |
| 255 | char buffer[JMSG_LENGTH_MAX]; |
| 256 | (cinfo.err->format_message)((jpeg_common_struct*)&cinfo, buffer); |
| 257 | throw CVD::Exceptions::Image_IO::MalformedImage(string("Error in JPEG image: ") + buffer); |
| 258 | } |
| 259 | |
| 260 | unsigned char** datap = &data; |
| 261 | for(unsigned int i = 0; i < nlines; i++) |
| 262 | { |
| 263 | jpeg_read_scanlines(&cinfo, datap, 1); |
| 264 | data += elements_per_line(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | ReadPimpl::~ReadPimpl() |
| 269 | { |
| 270 | jpeg_finish_decompress(&cinfo); |
| 271 | jpeg_destroy_decompress(&cinfo); |
| 272 | } |
| 273 | |
| 274 | //////////////////////////////////////////////////////////////////////////////// |
| 275 | // |
| 276 | // Compression |
| 277 | // |
| 278 | |
| 279 | struct jpeg_ostream_dest : public jpeg_destination_mgr |
| 280 | { |
| 281 | ostream* o; |
| 282 | static const int bufsize = 262144; |
| 283 | JOCTET buf[bufsize]; |
| 284 | |
| 285 | static void create(j_compress_ptr p, ostream* os) |
| 286 | { |
| 287 | //Allocate memory |
| 288 | p->dest = (jpeg_ostream_dest*)(*p->mem->alloc_small)((jpeg_common_struct*)p, JPOOL_PERMANENT, sizeof(jpeg_ostream_dest)); |
| 289 | |
| 290 | jpeg_ostream_dest* me = (jpeg_ostream_dest*)p->dest; |
| 291 | |
| 292 | //Set up virtual member functions |
| 293 | me->init_destination = s_init_destination; |
| 294 | me->empty_output_buffer = s_empty_output_buffer; |
| 295 | me->term_destination = s_term_destination; |
| 296 | |
| 297 | //Set up data members |
| 298 | me->o = os; |
| 299 | } |
| 300 | |
| 301 | static void s_init_destination(j_compress_ptr cinfo) |
| 302 | { |
| 303 | jpeg_ostream_dest* me = (jpeg_ostream_dest*)cinfo->dest; |
| 304 | |
| 305 | me->next_output_byte = &me->buf[0]; |
| 306 | me->free_in_buffer = bufsize; |
| 307 | } |
| 308 | |
| 309 | static boolean s_empty_output_buffer(j_compress_ptr cinfo) |
| 310 | { |
| 311 | jpeg_ostream_dest* me = (jpeg_ostream_dest*)cinfo->dest; |
| 312 | |
| 313 | //Docs say we should do this: |
| 314 | me->o->write((const char*)me->buf, bufsize); |
| 315 | |
| 316 | s_init_destination(cinfo); |
| 317 | return TRUE; |
| 318 | } |
| 319 | |
| 320 | static void s_term_destination(j_compress_ptr cinfo) |
| 321 | { |
| 322 | jpeg_ostream_dest* me = (jpeg_ostream_dest*)cinfo->dest; |
| 323 | me->o->write((const char*)me->buf, bufsize - me->free_in_buffer); |
| 324 | } |
| 325 | }; |
| 326 | |
| 327 | //////////////////////////////////////////////////////////////////////////////// |
| 328 | // |
| 329 | // JPEG writing. |
| 330 | // |
| 331 | |
| 332 | class WritePimpl |
| 333 | { |
| 334 | public: |
| 335 | WritePimpl(std::ostream&, int xsize, int ysize, const string& type, const std::map<std::string, Parameter<>>& p, const std::string& comm = ""); |
| 336 | int channels() { return m_channels; } |
| 337 | int x_size() const { return xs; } |
| 338 | int y_size() const { return ys; } |
| 339 | long elements_per_line() const { return xs * m_channels; } |
| 340 | void write_raw_pixel_lines(const unsigned char*, unsigned long); |
| 341 | template <class C> |
| 342 | void write_raw_pixel_line(const C*); |
| 343 | ~WritePimpl(); |
| 344 | |
| 345 | private: |
| 346 | int xs, ys, row; |
| 347 | int m_channels; |
| 348 | struct jpeg_compress_struct cinfo; |
| 349 | struct jpeg_error_mgr jerr; |
| 350 | std::ostream& o; |
| 351 | string type; |
| 352 | }; |
| 353 | |
| 354 | WritePimpl::WritePimpl(std::ostream& out, int xsize, int ysize, const string& t, const std::map<std::string, Parameter<>>& p, const string& comm) |
| 355 | : o(out) |
| 356 | { |
| 357 | xs = xsize; |
| 358 | ys = ysize; |
| 359 | type = t; |
| 360 | row = 0; |
| 361 | |
| 362 | if(type == "unsigned char") |
| 363 | m_channels = 1; |
| 364 | else if(type == "CVD::Rgb<unsigned char>") |
| 365 | m_channels = 3; |
| 366 | else |
| 367 | throw Exceptions::Image_IO::UnsupportedImageSubType("JPEG", type); |
| 368 | |
| 369 | //Set up setjmp/lonjmp error handling |
| 370 | cinfo.err = jumpy_error_manager(&jerr); |
| 371 | jmp_buf env; |
| 372 | cinfo.client_data = &env; |
| 373 | |
| 374 | //Catch "exceptions" and throw proper exceptions |
| 375 | if(setjmp(env)) |
| 376 | { |
| 377 | //longjmp called |
| 378 | char buffer[JMSG_LENGTH_MAX]; |
| 379 | (cinfo.err->format_message)((jpeg_common_struct*)&cinfo, buffer); |
| 380 | throw CVD::Exceptions::Image_IO::WriteError(string("JPEG: ") + buffer); |
| 381 | } |
| 382 | |
| 383 | //Create the compression object |
| 384 | jpeg_create_compress(&cinfo); |
| 385 | |
| 386 | //Get the jpeg_ostream_dest class to handle output. |
| 387 | jpeg_ostream_dest::create(&cinfo, &o); |
| 388 | |
| 389 | //Setup parameters |
| 390 | cinfo.image_width = xs; |
| 391 | cinfo.image_height = ys; |
| 392 | cinfo.input_components = m_channels; |
| 393 | cinfo.in_color_space = (m_channels == 3) ? JCS_RGB : JCS_GRAYSCALE; |
| 394 | |
| 395 | int quality = 95; |
| 396 | if(p.count("jpeg.quality")) |
| 397 | { |
| 398 | try |
| 399 | { |
| 400 | quality = p.find("jpeg.quality")->second.get<int>(); |
| 401 | quality = max(0, min(100, quality)); |
| 402 | } |
| 403 | catch(const std::bad_cast&) |
| 404 | { |
| 405 | cerr << "Warning jpeg.quality is not an int.\n"; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | jpeg_set_defaults(&cinfo); |
| 410 | jpeg_set_quality(&cinfo, quality, TRUE); |
| 411 | |
| 412 | jpeg_start_compress(&cinfo, TRUE); |
| 413 | |
| 414 | string comment = comm; |
| 415 | //NB: not 65536 Marker looks like: |
| 416 | // marker_byte length_high length_low length*bytes |
| 417 | // length includes the block header (3 bytes) |
| 418 | comment.resize(std::min(comment.length(), size_t(65533))); |
| 419 | //Written without zero termination, since the length is also written |
| 420 | jpeg_write_marker(&cinfo, JPEG_COM, (JOCTET*)comm.c_str(), static_cast<unsigned int>(comment.size())); |
| 421 | } |
| 422 | |
| 423 | void WritePimpl::write_raw_pixel_lines(const unsigned char* data, unsigned long nlines) |
| 424 | { |
| 425 | jmp_buf env; |
| 426 | cinfo.client_data = &env; |
| 427 | long elem = elements_per_line(); |
| 428 | |
| 429 | if(nlines + row > (unsigned long)ys) |
| 430 | throw CVD::Exceptions::Image_IO::InternalLibraryError("CVD", "Write past end of image."); |
| 431 | |
| 432 | //Catch "exceptions" and throw proper exceptions |
| 433 | if(setjmp(env)) |
| 434 | { |
| 435 | //longjmp called |
| 436 | char buffer[JMSG_LENGTH_MAX]; |
| 437 | (cinfo.err->format_message)((jpeg_common_struct*)&cinfo, buffer); |
| 438 | throw CVD::Exceptions::Image_IO::MalformedImage(string("JPEG: ") + buffer); |
| 439 | } |
| 440 | |
| 441 | const unsigned char** datap = &data; |
| 442 | for(unsigned int i = 0; i < nlines; i++) |
| 443 | { |
| 444 | jpeg_write_scanlines(&cinfo, (JSAMPLE**)datap, 1); |
| 445 | data += elem; |
| 446 | row++; |
| 447 | } |
| 448 | } |
| 449 | template <class C> |
| 450 | void WritePimpl::write_raw_pixel_line(const C* d) |
| 451 | { |
| 452 | if(type != PNM::type_name<C>::name()) |
| 453 | throw CVD::Exceptions::Image_IO::WriteTypeMismatch(type, PNM::type_name<C>::name()); |
| 454 | |
| 455 | write_raw_pixel_lines((const unsigned char*)d, 1); |
| 456 | } |
| 457 | |
| 458 | WritePimpl::~WritePimpl() |
| 459 | { |
| 460 | jpeg_finish_compress(&cinfo); |
| 461 | jpeg_destroy_compress(&cinfo); |
| 462 | } |
| 463 | |
| 464 | //////////////////////////////////////////////////////////////////////////////// |
| 465 | // |
| 466 | // Public interfaces to image writing. |
| 467 | // |
| 468 | |
| 469 | writer::writer(ostream& o, ImageRef size, const string& s, const std::map<std::string, Parameter<>>& p) |
| 470 | : t(new WritePimpl(o, size.x, size.y, s, p)) |
| 471 | { |
| 472 | } |
| 473 | |
| 474 | writer::~writer() |
| 475 | { |
| 476 | } |
| 477 | |
| 478 | void writer::write_raw_pixel_line(const byte* data) |
| 479 | { |
| 480 | t->write_raw_pixel_line(data); |
| 481 | } |
| 482 | |
| 483 | void writer::write_raw_pixel_line(const Rgb<byte>* data) |
| 484 | { |
| 485 | t->write_raw_pixel_line(data); |
| 486 | } |
| 487 | |
| 488 | } |
| 489 | } |