diff options
Diffstat (limited to 'ml/dlib/dlib/image_loader')
-rw-r--r-- | ml/dlib/dlib/image_loader/image_loader.h | 863 | ||||
-rw-r--r-- | ml/dlib/dlib/image_loader/image_loader_abstract.h | 136 | ||||
-rw-r--r-- | ml/dlib/dlib/image_loader/jpeg_loader.cpp | 173 | ||||
-rw-r--r-- | ml/dlib/dlib/image_loader/jpeg_loader.h | 109 | ||||
-rw-r--r-- | ml/dlib/dlib/image_loader/jpeg_loader_abstract.h | 133 | ||||
-rw-r--r-- | ml/dlib/dlib/image_loader/load_image.h | 226 | ||||
-rw-r--r-- | ml/dlib/dlib/image_loader/load_image_abstract.h | 37 | ||||
-rw-r--r-- | ml/dlib/dlib/image_loader/png_loader.cpp | 222 | ||||
-rw-r--r-- | ml/dlib/dlib/image_loader/png_loader.h | 223 | ||||
-rw-r--r-- | ml/dlib/dlib/image_loader/png_loader_abstract.h | 162 |
10 files changed, 0 insertions, 2284 deletions
diff --git a/ml/dlib/dlib/image_loader/image_loader.h b/ml/dlib/dlib/image_loader/image_loader.h deleted file mode 100644 index 4fa29dab2..000000000 --- a/ml/dlib/dlib/image_loader/image_loader.h +++ /dev/null @@ -1,863 +0,0 @@ -// Copyright (C) 2006 Davis E. King (davis@dlib.net) -// License: Boost Software License See LICENSE.txt for the full license. -#ifndef DLIB_IMAGE_LOADEr_ -#define DLIB_IMAGE_LOADEr_ - -#include "image_loader_abstract.h" -#include <iostream> -#include <sstream> -#include "../algs.h" -#include "../pixel.h" -#include "../image_saver/dng_shared.h" -#include "../entropy_decoder_model.h" -#include "../entropy_decoder.h" -#include "../uintn.h" -#include "../image_transforms/assign_image.h" -#include <algorithm> -#include "../vectorstream.h" - -namespace dlib -{ - -// ---------------------------------------------------------------------------------------- - - class image_load_error : public dlib::error { - public: image_load_error(const std::string& str) : error(EIMAGE_LOAD,str){} - }; - -// ---------------------------------------------------------------------------------------- - - template < - typename image_type - > - void load_bmp ( - image_type& image_, - std::istream& in_ - ) - { - image_view<image_type> image(image_); - try - { - unsigned long bytes_read_so_far = 0; - unsigned long bfSize; - unsigned long bfOffBits; - unsigned long bfReserved; - unsigned long biSize; - unsigned long biWidth; - unsigned long biHeight; - unsigned short biBitCount; - unsigned long biCompression; - /* - unsigned long biSizeImage; - unsigned long biClrUsed; - unsigned long biClrImportant; - */ - unsigned long a, b, c, d, i; - - using namespace std; - - streambuf& in = *in_.rdbuf(); - // streamsize num; - unsigned char buf[100]; - - - // first make sure the BMP starts with BM - if (in.sgetn(reinterpret_cast<char*>(buf),2) != 2) - throw image_load_error("bmp load error 1: header error"); - bytes_read_so_far += 2; - - if (buf[0] != 'B' || buf[1] != 'M') - throw image_load_error("bmp load error 2: header error"); - - // now read the BITMAPFILEHEADER - if (in.sgetn(reinterpret_cast<char*>(buf),12) != 12) - throw image_load_error("bmp load error 3: header error"); - - bytes_read_so_far += 12; - - i = 0; - a = buf[i]; b = buf[i+1]; c = buf[i+2]; d = buf[i+3]; - bfSize = a | (b<<8) | (c<<16) | (d<<24); - - i = 4; - a = buf[i]; b = buf[i+1]; c = buf[i+2]; d = buf[i+3]; - bfReserved = a | (b<<8) | (c<<16) | (d<<24); - - i = 8; - a = buf[i]; b = buf[i+1]; c = buf[i+2]; d = buf[i+3]; - bfOffBits = a | (b<<8) | (c<<16) | (d<<24); - - // if this value isn't zero then there is something wrong - // with this bitmap. - if (bfReserved != 0) - throw image_load_error("bmp load error 4: reserved area not zero"); - - - // load the BITMAPINFOHEADER - if (in.sgetn(reinterpret_cast<char*>(buf),40) != 40) - throw image_load_error("bmp load error 5: file too short"); - bytes_read_so_far += 40; - - - i = 0; - a = buf[i]; b = buf[i+1]; c = buf[i+2]; d = buf[i+3]; - biSize = a | (b<<8) | (c<<16) | (d<<24); - - i += 4; - a = buf[i]; b = buf[i+1]; c = buf[i+2]; d = buf[i+3]; - biWidth = a | (b<<8) | (c<<16) | (d<<24); - - i += 4; - a = buf[i]; b = buf[i+1]; c = buf[i+2]; d = buf[i+3]; - biHeight = a | (b<<8) | (c<<16) | (d<<24); - - i += 4+2; - a = buf[i]; b = buf[i+1]; - biBitCount = static_cast<unsigned short>(a | (b<<8)); - - i += 2; - a = buf[i]; b = buf[i+1]; c = buf[i+2]; d = buf[i+3]; - biCompression = a | (b<<8) | (c<<16) | (d<<24); - - /* - i += 4; - a = buf[i]; b = buf[i+1]; c = buf[i+2]; d = buf[i+3]; - biSizeImage = a | (b<<8) | (c<<16) | (d<<24); - - i += 4+4+4; - a = buf[i]; b = buf[i+1]; c = buf[i+2]; d = buf[i+3]; - biClrUsed = a | (b<<8) | (c<<16) | (d<<24); - - i += 4; - a = buf[i]; b = buf[i+1]; c = buf[i+2]; d = buf[i+3]; - biClrImportant = a | (b<<8) | (c<<16) | (d<<24); - */ - - - if (biSize != 40) - throw image_load_error("bmp load error 6: header too small"); - - // read and discard any extra bytes that are part of the header - if (biSize > 40) - { - if (in.sgetn(reinterpret_cast<char*>(buf),biSize-40) != static_cast<long>(biSize - 40)) - { - throw image_load_error("bmp load error 7: header too small"); - } - bytes_read_so_far += biSize-40; - } - - image.set_size(biHeight, biWidth); - - switch (biBitCount) - { - case 1: - { - // figure out how the pixels are packed - long padding; - if (bfSize - bfOffBits == biWidth*biHeight/8) - padding = 0; - else - padding = 4 - ((biWidth+7)/8)%4; - - const unsigned int palette_size = 2; - unsigned char red[palette_size]; - unsigned char green[palette_size]; - unsigned char blue[palette_size]; - - for (unsigned int i = 0; i < palette_size; ++i) - { - if (in.sgetn(reinterpret_cast<char*>(buf),4) != 4) - { - throw image_load_error("bmp load error 20: color palette missing"); - } - bytes_read_so_far += 4; - blue[i] = buf[0]; - green[i] = buf[1]; - red[i] = buf[2]; - } - - - // seek to the start of the pixel data - while (bytes_read_so_far != bfOffBits) - { - const long to_read = (long)std::min(bfOffBits - bytes_read_so_far, (unsigned long)sizeof(buf)); - if (in.sgetn(reinterpret_cast<char*>(buf), to_read) != to_read) - { - throw image_load_error("bmp load error: missing data"); - } - bytes_read_so_far += to_read; - } - - // load the image data - for (long row = biHeight-1; row >= 0; --row) - { - for (unsigned long col = 0; col < biWidth; col+=8) - { - if (in.sgetn(reinterpret_cast<char*>(buf),1) != 1) - { - throw image_load_error("bmp load error 21.6: file too short"); - } - - unsigned char pixels[8]; - - pixels[0] = (buf[0]>>7); - pixels[1] = ((buf[0]>>6)&0x01); - pixels[2] = ((buf[0]>>5)&0x01); - pixels[3] = ((buf[0]>>4)&0x01); - pixels[4] = ((buf[0]>>3)&0x01); - pixels[5] = ((buf[0]>>2)&0x01); - pixels[6] = ((buf[0]>>1)&0x01); - pixels[7] = ((buf[0])&0x01); - - for (int i = 0; i < 8 && col+i < biWidth; ++i) - { - rgb_pixel p; - p.red = red[pixels[i]]; - p.green = green[pixels[i]]; - p.blue = blue[pixels[i]]; - assign_pixel(image[row][col+i],p); - } - } - if (in.sgetn(reinterpret_cast<char*>(buf),padding) != padding) - throw image_load_error("bmp load error 9: file too short"); - } - - - - } break; - case 4: - { - // figure out how the pixels are packed - long padding; - if (bfSize - bfOffBits == biWidth*biHeight/2) - padding = 0; - else - padding = 4 - ((biWidth+1)/2)%4; - - const unsigned int palette_size = 16; - unsigned char red[palette_size]; - unsigned char green[palette_size]; - unsigned char blue[palette_size]; - - for (unsigned int i = 0; i < palette_size; ++i) - { - if (in.sgetn(reinterpret_cast<char*>(buf),4) != 4) - { - throw image_load_error("bmp load error 20: color palette missing"); - } - bytes_read_so_far += 4; - blue[i] = buf[0]; - green[i] = buf[1]; - red[i] = buf[2]; - } - - - // seek to the start of the pixel data - while (bytes_read_so_far != bfOffBits) - { - const long to_read = (long)std::min(bfOffBits - bytes_read_so_far, (unsigned long)sizeof(buf)); - if (in.sgetn(reinterpret_cast<char*>(buf), to_read) != to_read) - { - throw image_load_error("bmp load error: missing data"); - } - bytes_read_so_far += to_read; - } - - // load the image data - for (long row = biHeight-1; row >= 0; --row) - { - for (unsigned long col = 0; col < biWidth; col+=2) - { - if (in.sgetn(reinterpret_cast<char*>(buf),1) != 1) - { - throw image_load_error("bmp load error 21.7: file too short"); - } - - const unsigned char pixel1 = (buf[0]>>4); - const unsigned char pixel2 = (buf[0]&0x0F); - - rgb_pixel p; - p.red = red[pixel1]; - p.green = green[pixel1]; - p.blue = blue[pixel1]; - assign_pixel(image[row][col], p); - - if (col+1 < biWidth) - { - p.red = red[pixel2]; - p.green = green[pixel2]; - p.blue = blue[pixel2]; - assign_pixel(image[row][col+1], p); - } - } - if (in.sgetn(reinterpret_cast<char*>(buf),padding) != padding) - throw image_load_error("bmp load error 9: file too short"); - } - - - - } break; - case 8: - { - // figure out how the pixels are packed - long padding; - if (bfSize - bfOffBits == biWidth*biHeight) - padding = 0; - else - padding = 4 - biWidth%4; - - // check for this case. It shouldn't happen but some BMP writers screw up the files - // so we have to do this. - if (biHeight*(biWidth+padding) > bfSize - bfOffBits) - padding = 0; - - const unsigned int palette_size = 256; - unsigned char red[palette_size]; - unsigned char green[palette_size]; - unsigned char blue[palette_size]; - - for (unsigned int i = 0; i < palette_size; ++i) - { - if (in.sgetn(reinterpret_cast<char*>(buf),4) != 4) - { - throw image_load_error("bmp load error 20: color palette missing"); - } - bytes_read_so_far += 4; - blue[i] = buf[0]; - green[i] = buf[1]; - red[i] = buf[2]; - } - - - // seek to the start of the pixel data - while (bytes_read_so_far != bfOffBits) - { - const long to_read = (long)std::min(bfOffBits - bytes_read_so_far, (unsigned long)sizeof(buf)); - if (in.sgetn(reinterpret_cast<char*>(buf), to_read) != to_read) - { - throw image_load_error("bmp load error: missing data"); - } - bytes_read_so_far += to_read; - } - - // Next we load the image data. - - // if there is no RLE compression - if (biCompression == 0) - { - for (long row = biHeight-1; row >= 0; --row) - { - for (unsigned long col = 0; col < biWidth; ++col) - { - if (in.sgetn(reinterpret_cast<char*>(buf),1) != 1) - { - throw image_load_error("bmp load error 21.8: file too short"); - } - - rgb_pixel p; - p.red = red[buf[0]]; - p.green = green[buf[0]]; - p.blue = blue[buf[0]]; - assign_pixel(image[row][col],p); - } - if (in.sgetn(reinterpret_cast<char*>(buf),padding) != padding) - throw image_load_error("bmp load error 9: file too short"); - } - } - else - { - // Here we deal with the psychotic RLE used by BMP files. - - // First zero the image since the RLE sometimes jumps over - // pixels and assumes the image has been zero initialized. - assign_all_pixels(image, 0); - - long row = biHeight-1; - long col = 0; - while (true) - { - if (in.sgetn(reinterpret_cast<char*>(buf),2) != 2) - { - throw image_load_error("bmp load error 21.9: file too short"); - } - - const unsigned char count = buf[0]; - const unsigned char command = buf[1]; - - if (count == 0 && command == 0) - { - // This is an escape code that means go to the next row - // of the image - --row; - col = 0; - continue; - } - else if (count == 0 && command == 1) - { - // This is the end of the image. So quit this loop. - break; - } - else if (count == 0 && command == 2) - { - // This is the escape code for the command to jump to - // a new part of the image relative to where we are now. - if (in.sgetn(reinterpret_cast<char*>(buf),2) != 2) - { - throw image_load_error("bmp load error 21.1: file too short"); - } - col += buf[0]; - row -= buf[1]; - continue; - } - else if (count == 0) - { - // This is the escape code for a run of uncompressed bytes - - if (row < 0 || col + command > image.nc()) - { - // If this is just some padding bytes at the end then ignore them - if (row >= 0 && col + count <= image.nc() + padding) - continue; - - throw image_load_error("bmp load error 21.2: file data corrupt"); - } - - // put the bytes into the image - for (unsigned int i = 0; i < command; ++i) - { - if (in.sgetn(reinterpret_cast<char*>(buf),1) != 1) - { - throw image_load_error("bmp load error 21.3: file too short"); - } - rgb_pixel p; - p.red = red[buf[0]]; - p.green = green[buf[0]]; - p.blue = blue[buf[0]]; - assign_pixel(image[row][col],p); - - ++col; - } - - // if we read an uneven number of bytes then we need to read and - // discard the next byte. - if ((command&1) != 1) - { - if (in.sgetn(reinterpret_cast<char*>(buf),1) != 1) - { - throw image_load_error("bmp load error 21.4: file too short"); - } - } - - continue; - } - - rgb_pixel p; - - if (row < 0 || col + count > image.nc()) - { - // If this is just some padding bytes at the end then ignore them - if (row >= 0 && col + count <= image.nc() + padding) - continue; - - throw image_load_error("bmp load error 21.5: file data corrupt"); - } - - // put the bytes into the image - for (unsigned int i = 0; i < count; ++i) - { - p.red = red[command]; - p.green = green[command]; - p.blue = blue[command]; - assign_pixel(image[row][col],p); - - ++col; - } - } - } - - - - } - break; - case 16: - throw image_load_error ("16 bit BMP images not supported"); - case 24: - { - // figure out how the pixels are packed - long padding; - if (bfSize - bfOffBits == biWidth*biHeight*3) - padding = 0; - else - padding = 4 - (biWidth*3)%4; - - // check for this case. It shouldn't happen but some BMP writers screw up the files - // so we have to do this. - if (biHeight*(biWidth*3+padding) > bfSize - bfOffBits) - padding = 0; - - // seek to the start of the pixel data - while (bytes_read_so_far != bfOffBits) - { - const long to_read = (long)std::min(bfOffBits - bytes_read_so_far, (unsigned long)sizeof(buf)); - if (in.sgetn(reinterpret_cast<char*>(buf), to_read) != to_read) - { - throw image_load_error("bmp load error: missing data"); - } - bytes_read_so_far += to_read; - } - - // load the image data - for (long row = biHeight-1; row >= 0; --row) - { - for (unsigned long col = 0; col < biWidth; ++col) - { - if (in.sgetn(reinterpret_cast<char*>(buf),3) != 3) - { - throw image_load_error("bmp load error 8: file too short"); - } - - rgb_pixel p; - p.red = buf[2]; - p.green = buf[1]; - p.blue = buf[0]; - assign_pixel(image[row][col], p); - - } - if (in.sgetn(reinterpret_cast<char*>(buf),padding) != padding) - throw image_load_error("bmp load error 9: file too short"); - } - - break; - } - case 32: - throw image_load_error ("32 bit BMP images not supported"); - default: - throw image_load_error("bmp load error 10: unknown color depth"); - - } - } - catch (...) - { - image.clear(); - throw; - } - - } - -// ---------------------------------------------------------------------------------------- - - template < - typename image_type - > - void load_dng ( - image_type& image_, - std::istream& in - ) - { - image_view<image_type> image(image_); - using namespace dng_helpers_namespace; - try - { - if (in.get() != 'D' || in.get() != 'N' || in.get() != 'G') - throw image_load_error("the stream does not contain a dng image file"); - - unsigned long version; - deserialize(version,in); - if (version != 1) - throw image_load_error("You need the new version of the dlib library to read this dng file"); - - unsigned long type; - deserialize(type,in); - - long width, height; - deserialize(width,in); - deserialize(height,in); - - if (width > 0 && height > 0) - image.set_size(height,width); - else - image.clear(); - - if (type != grayscale_float) - { - typedef entropy_decoder::kernel_2a decoder_type; - decoder_type decoder; - decoder.set_stream(in); - - entropy_decoder_model<256,decoder_type>::kernel_5a edm(decoder); - unsigned long symbol; - rgb_pixel p_rgb; - rgb_alpha_pixel p_rgba; - hsi_pixel p_hsi; - switch (type) - { - case rgb_alpha_paeth: - - for (long r = 0; r < image.nr(); ++r) - { - for (long c = 0; c < image.nc(); ++c) - { - p_rgba = predictor_rgb_alpha_paeth(image,r,c); - edm.decode(symbol); - p_rgba.red += static_cast<unsigned char>(symbol); - - edm.decode(symbol); - p_rgba.green += static_cast<unsigned char>(symbol); - - edm.decode(symbol); - p_rgba.blue += static_cast<unsigned char>(symbol); - - edm.decode(symbol); - p_rgba.alpha += static_cast<unsigned char>(symbol); - - assign_pixel(image[r][c],p_rgba); - } - } - break; - - case rgb_alpha: - - for (long r = 0; r < image.nr(); ++r) - { - for (long c = 0; c < image.nc(); ++c) - { - p_rgba = predictor_rgb_alpha(image,r,c); - edm.decode(symbol); - p_rgba.red += static_cast<unsigned char>(symbol); - - edm.decode(symbol); - p_rgba.green += static_cast<unsigned char>(symbol); - - edm.decode(symbol); - p_rgba.blue += static_cast<unsigned char>(symbol); - - edm.decode(symbol); - p_rgba.alpha += static_cast<unsigned char>(symbol); - - assign_pixel(image[r][c],p_rgba); - } - } - break; - - case rgb_paeth: - - for (long r = 0; r < image.nr(); ++r) - { - for (long c = 0; c < image.nc(); ++c) - { - p_rgb = predictor_rgb_paeth(image,r,c); - edm.decode(symbol); - p_rgb.red += static_cast<unsigned char>(symbol); - - edm.decode(symbol); - p_rgb.green += static_cast<unsigned char>(symbol); - - edm.decode(symbol); - p_rgb.blue += static_cast<unsigned char>(symbol); - - assign_pixel(image[r][c],p_rgb); - } - } - break; - - case rgb: - - for (long r = 0; r < image.nr(); ++r) - { - for (long c = 0; c < image.nc(); ++c) - { - p_rgb = predictor_rgb(image,r,c); - edm.decode(symbol); - p_rgb.red += static_cast<unsigned char>(symbol); - - edm.decode(symbol); - p_rgb.green += static_cast<unsigned char>(symbol); - - edm.decode(symbol); - p_rgb.blue += static_cast<unsigned char>(symbol); - - assign_pixel(image[r][c],p_rgb); - } - } - break; - - case hsi: - - for (long r = 0; r < image.nr(); ++r) - { - for (long c = 0; c < image.nc(); ++c) - { - p_hsi = predictor_hsi(image,r,c); - edm.decode(symbol); - p_hsi.h += static_cast<unsigned char>(symbol); - - edm.decode(symbol); - p_hsi.s += static_cast<unsigned char>(symbol); - - edm.decode(symbol); - p_hsi.i += static_cast<unsigned char>(symbol); - - assign_pixel(image[r][c],p_hsi); - } - } - break; - - case grayscale: - { - unsigned char p; - for (long r = 0; r < image.nr(); ++r) - { - for (long c = 0; c < image.nc(); ++c) - { - edm.decode(symbol); - p = static_cast<unsigned char>(symbol); - p += predictor_grayscale(image,r,c); - assign_pixel(image[r][c],p); - } - } - } - break; - - case grayscale_16bit: - { - uint16 p; - for (long r = 0; r < image.nr(); ++r) - { - for (long c = 0; c < image.nc(); ++c) - { - edm.decode(symbol); - p = static_cast<uint16>(symbol); - p <<= 8; - edm.decode(symbol); - p |= static_cast<uint16>(symbol); - - p += predictor_grayscale_16(image,r,c); - assign_pixel(image[r][c],p); - } - } - } - break; - - default: - throw image_load_error("corruption detected in the dng file"); - } // switch (type) - - edm.decode(symbol); - if (symbol != dng_magic_byte) - throw image_load_error("corruption detected in the dng file"); - edm.decode(symbol); - if (symbol != dng_magic_byte) - throw image_load_error("corruption detected in the dng file"); - edm.decode(symbol); - if (symbol != dng_magic_byte) - throw image_load_error("corruption detected in the dng file"); - edm.decode(symbol); - if (symbol != dng_magic_byte) - throw image_load_error("corruption detected in the dng file"); - } - else // if this is a grayscale_float type image - { - std::vector<int64> man(image.size()); - std::vector<char> expbuf; - // get the mantissa data - for (unsigned long j = 0; j < man.size(); ++j) - deserialize(man[j], in); - // get the compressed exponent data - deserialize(expbuf, in); - typedef entropy_decoder::kernel_2a decoder_type; - typedef entropy_decoder_model<256,decoder_type>::kernel_4a edm_exp_type; - vectorstream inexp(expbuf); - decoder_type decoder; - decoder.set_stream(inexp); - - edm_exp_type edm_exp(decoder); - float_details prev; - unsigned long i = 0; - // fill out the image - for (long r = 0; r < image.nr(); ++r) - { - for (long c = 0; c < image.nc(); ++c) - { - unsigned long exp1, exp2; - edm_exp.decode(exp1); - edm_exp.decode(exp2); - - float_details cur(man[i++],(exp2<<8) | exp1); - cur.exponent += prev.exponent; - cur.mantissa += prev.mantissa; - prev = cur; - - // Only use long double precision if the target image contains long - // doubles because it's slower to use those. - if (!is_same_type<typename image_traits<image_type>::pixel_type,long double>::value) - { - double temp = cur; - assign_pixel(image[r][c],temp); - } - else - { - long double temp = cur; - assign_pixel(image[r][c],temp); - } - } - } - unsigned long symbol; - edm_exp.decode(symbol); - if (symbol != dng_magic_byte) - throw image_load_error("corruption detected in the dng file"); - edm_exp.decode(symbol); - if (symbol != dng_magic_byte) - throw image_load_error("corruption detected in the dng file"); - edm_exp.decode(symbol); - if (symbol != dng_magic_byte) - throw image_load_error("corruption detected in the dng file"); - edm_exp.decode(symbol); - if (symbol != dng_magic_byte) - throw image_load_error("corruption detected in the dng file"); - } - } - catch (...) - { - image.clear(); - throw; - } - - } - -// ---------------------------------------------------------------------------------------- - - template <typename image_type> - void load_bmp ( - image_type& image, - const std::string& file_name - ) - { - std::ifstream fin(file_name.c_str(), std::ios::binary); - if (!fin) - throw image_load_error("Unable to open " + file_name + " for reading."); - load_bmp(image, fin); - } - -// ---------------------------------------------------------------------------------------- - - template <typename image_type> - void load_dng ( - image_type& image, - const std::string& file_name - ) - { - std::ifstream fin(file_name.c_str(), std::ios::binary); - if (!fin) - throw image_load_error("Unable to open " + file_name + " for reading."); - load_dng(image, fin); - } - -// ---------------------------------------------------------------------------------------- - -} - -#endif // DLIB_IMAGE_LOADEr_ - - - diff --git a/ml/dlib/dlib/image_loader/image_loader_abstract.h b/ml/dlib/dlib/image_loader/image_loader_abstract.h deleted file mode 100644 index cd66b3699..000000000 --- a/ml/dlib/dlib/image_loader/image_loader_abstract.h +++ /dev/null @@ -1,136 +0,0 @@ -// Copyright (C) 2006 Davis E. King (davis@dlib.net) -// License: Boost Software License See LICENSE.txt for the full license. -#undef DLIB_IMAGE_LOADEr_ABSTRACT_ -#ifdef DLIB_IMAGE_LOADEr_ABSTRACT_ - -#include <iosfwd> -#include "../algs.h" -#include "../pixel.h" -#include "../image_processing/generic_image.h" - -namespace dlib -{ - class image_load_error : public dlib::error - { - /*! - WHAT THIS OBJECT REPRESENTS - This is an exception used to indicate a failure to load an image. - Its type member variable will be set to EIMAGE_LOAD. - !*/ - }; - -// ---------------------------------------------------------------------------------------- - - template < - typename image_type - > - void load_bmp ( - image_type& image, - std::istream& in - ); - /*! - requires - - image_type == an image object that implements the interface defined in - dlib/image_processing/generic_image.h - ensures - - #image == the image of the MS Windows BMP file that was available - in the input stream in. - - #image[0][0] will be the upper left corner of the image - - #image[image.nr()-1][image.nc()-1] will be the lower right - corner of the image - - Performs any color space conversion necessary to convert the - BMP image data into the pixel type used by the given image - object. - throws - - image_load_error - This exception is thrown if there is an error that prevents us - from loading the image. If this exception is thrown then - #image will have an initial value for its type. - - std::bad_alloc - If this exception is thrown then #image will have an initial - value for its type. - !*/ - -// ---------------------------------------------------------------------------------------- - - template < - typename image_type - > - void load_bmp ( - image_type& image, - const std::string& file_name - ); - /*! - requires - - image_type == an image object that implements the interface defined in - dlib/image_processing/generic_image.h - ensures - - opens the file indicated by file_name with an input file stream named fin - and performs: - load_bmp(image,fin); - !*/ - -// ---------------------------------------------------------------------------------------- - - /*! - dlib dng file format: - This is a file format I created for this library. It is a lossless - compressed image format that is similar to the PNG format but uses - the dlib PPM compression algorithms instead of the DEFLATE algorithm. - !*/ - - template < - typename image_type - > - void load_dng ( - image_type& image, - std::istream& in - ); - /*! - requires - - image_type == an image object that implements the interface defined in - dlib/image_processing/generic_image.h - ensures - - #image == the image of the dlib dng file that was available - in the input stream in. - - #image[0][0] will be the upper left corner of the image - - #image[image.nr()-1][image.nc()-1] will be the lower right - corner of the image - - Performs any color space conversion necessary to convert the - dng image data into the pixel type used by the given image - object. - throws - - image_load_error - This exception is thrown if there is an error that prevents us - from loading the image. If this exception is thrown then - #image will have an initial value for its type. - - std::bad_alloc - If this exception is thrown then #image will have an initial - value for its type. - !*/ - -// ---------------------------------------------------------------------------------------- - - template < - typename image_type - > - void load_dng ( - image_type& image, - const std::string& file_name - ); - /*! - requires - - image_type == an image object that implements the interface defined in - dlib/image_processing/generic_image.h - ensures - - opens the file indicated by file_name with an input file stream named fin - and performs: - load_dng(image,fin); - !*/ - -// ---------------------------------------------------------------------------------------- - -} - -#endif // DLIB_IMAGE_LOADEr_ABSTRACT_ - diff --git a/ml/dlib/dlib/image_loader/jpeg_loader.cpp b/ml/dlib/dlib/image_loader/jpeg_loader.cpp deleted file mode 100644 index 710d7586f..000000000 --- a/ml/dlib/dlib/image_loader/jpeg_loader.cpp +++ /dev/null @@ -1,173 +0,0 @@ -// Copyright (C) 2010 Davis E. King (davis@dlib.net), Nils Labugt -// License: Boost Software License See LICENSE.txt for the full license. -#ifndef DLIB_JPEG_LOADER_CPp_ -#define DLIB_JPEG_LOADER_CPp_ - -// only do anything with this file if DLIB_JPEG_SUPPORT is defined -#ifdef DLIB_JPEG_SUPPORT - -#include "../array2d.h" -#include "../pixel.h" -#include "../dir_nav.h" -#include "jpeg_loader.h" -#include <stdio.h> -#ifdef DLIB_JPEG_STATIC -# include "../external/libjpeg/jpeglib.h" -#else -# include <jpeglib.h> -#endif -#include <sstream> -#include <setjmp.h> - -namespace dlib -{ - -// ---------------------------------------------------------------------------------------- - - jpeg_loader:: - jpeg_loader( const char* filename ) : height_( 0 ), width_( 0 ), output_components_(0) - { - read_image( filename ); - } - -// ---------------------------------------------------------------------------------------- - - jpeg_loader:: - jpeg_loader( const std::string& filename ) : height_( 0 ), width_( 0 ), output_components_(0) - { - read_image( filename.c_str() ); - } - -// ---------------------------------------------------------------------------------------- - - jpeg_loader:: - jpeg_loader( const dlib::file& f ) : height_( 0 ), width_( 0 ), output_components_(0) - { - read_image( f.full_name().c_str() ); - } - -// ---------------------------------------------------------------------------------------- - - bool jpeg_loader::is_gray() const - { - return (output_components_ == 1); - } - -// ---------------------------------------------------------------------------------------- - - bool jpeg_loader::is_rgb() const - { - return (output_components_ == 3); - } - -// ---------------------------------------------------------------------------------------- - - bool jpeg_loader::is_rgba() const - { - return (output_components_ == 4); - } - -// ---------------------------------------------------------------------------------------- - - struct jpeg_loader_error_mgr - { - jpeg_error_mgr pub; /* "public" fields */ - jmp_buf setjmp_buffer; /* for return to caller */ - }; - - void jpeg_loader_error_exit (j_common_ptr cinfo) - { - /* cinfo->err really points to a jpeg_loader_error_mgr struct, so coerce pointer */ - jpeg_loader_error_mgr* myerr = (jpeg_loader_error_mgr*) cinfo->err; - - /* Return control to the setjmp point */ - longjmp(myerr->setjmp_buffer, 1); - } - -// ---------------------------------------------------------------------------------------- - - void jpeg_loader::read_image( const char* filename ) - { - if ( filename == NULL ) - { - throw image_load_error("jpeg_loader: invalid filename, it is NULL"); - } - FILE *fp = fopen( filename, "rb" ); - if ( !fp ) - { - throw image_load_error(std::string("jpeg_loader: unable to open file ") + filename); - } - - jpeg_decompress_struct cinfo; - jpeg_loader_error_mgr jerr; - - cinfo.err = jpeg_std_error(&jerr.pub); - - jerr.pub.error_exit = jpeg_loader_error_exit; - - /* Establish the setjmp return context for my_error_exit to use. */ - if (setjmp(jerr.setjmp_buffer)) - { - /* If we get here, the JPEG code has signaled an error. - * We need to clean up the JPEG object, close the input file, and return. - */ - jpeg_destroy_decompress(&cinfo); - fclose(fp); - throw image_load_error(std::string("jpeg_loader: error while reading ") + filename); - } - - - jpeg_create_decompress(&cinfo); - - jpeg_stdio_src(&cinfo, fp); - - jpeg_read_header(&cinfo, TRUE); - - jpeg_start_decompress(&cinfo); - - height_ = cinfo.output_height; - width_ = cinfo.output_width; - output_components_ = cinfo.output_components; - - if (output_components_ != 1 && - output_components_ != 3 && - output_components_ != 4) - { - fclose( fp ); - jpeg_destroy_decompress(&cinfo); - std::ostringstream sout; - sout << "jpeg_loader: Unsupported number of colors (" << output_components_ << ") in file " << filename; - throw image_load_error(sout.str()); - } - - std::vector<unsigned char*> rows; - rows.resize(height_); - - // size the image buffer - data.resize(height_*width_*output_components_); - - // setup pointers to each row - for (unsigned long i = 0; i < rows.size(); ++i) - rows[i] = &data[i*width_*output_components_]; - - // read the data into the buffer - while (cinfo.output_scanline < cinfo.output_height) - { - jpeg_read_scanlines(&cinfo, &rows[cinfo.output_scanline], 100); - } - - jpeg_finish_decompress(&cinfo); - jpeg_destroy_decompress(&cinfo); - - fclose( fp ); - } - -// ---------------------------------------------------------------------------------------- - -} - -#endif // DLIB_JPEG_SUPPORT - -#endif // DLIB_JPEG_LOADER_CPp_ - - diff --git a/ml/dlib/dlib/image_loader/jpeg_loader.h b/ml/dlib/dlib/image_loader/jpeg_loader.h deleted file mode 100644 index 097a461f8..000000000 --- a/ml/dlib/dlib/image_loader/jpeg_loader.h +++ /dev/null @@ -1,109 +0,0 @@ -// Copyright (C) 2010 Davis E. King (davis@dlib.net), Nils Labugt -// License: Boost Software License See LICENSE.txt for the full license. -#ifndef DLIB_JPEG_IMPORT -#define DLIB_JPEG_IMPORT - -#include <vector> - -#include "jpeg_loader_abstract.h" -#include "image_loader.h" -#include "../pixel.h" -#include "../dir_nav.h" -#include "../test_for_odr_violations.h" - -namespace dlib -{ - - class jpeg_loader : noncopyable - { - public: - - jpeg_loader( const char* filename ); - jpeg_loader( const std::string& filename ); - jpeg_loader( const dlib::file& f ); - - bool is_gray() const; - bool is_rgb() const; - bool is_rgba() const; - - template<typename T> - void get_image( T& t_) const - { -#ifndef DLIB_JPEG_SUPPORT - /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - You are getting this error because you are trying to use the jpeg_loader - object but you haven't defined DLIB_JPEG_SUPPORT. You must do so to use - this object. You must also make sure you set your build environment - to link against the libjpeg library. - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ - COMPILE_TIME_ASSERT(sizeof(T) == 0); -#endif - image_view<T> t(t_); - t.set_size( height_, width_ ); - for ( unsigned n = 0; n < height_;n++ ) - { - const unsigned char* v = get_row( n ); - for ( unsigned m = 0; m < width_;m++ ) - { - if ( is_gray() ) - { - unsigned char p = v[m]; - assign_pixel( t[n][m], p ); - } - else if ( is_rgba() ) { - rgb_alpha_pixel p; - p.red = v[m*4]; - p.green = v[m*4+1]; - p.blue = v[m*4+2]; - p.alpha = v[m*4+3]; - assign_pixel( t[n][m], p ); - } - else // if ( is_rgb() ) - { - rgb_pixel p; - p.red = v[m*3]; - p.green = v[m*3+1]; - p.blue = v[m*3+2]; - assign_pixel( t[n][m], p ); - } - } - } - } - - private: - const unsigned char* get_row( unsigned long i ) const - { - return &data[i*width_*output_components_]; - } - - void read_image( const char* filename ); - unsigned long height_; - unsigned long width_; - unsigned long output_components_; - std::vector<unsigned char> data; - }; - -// ---------------------------------------------------------------------------------------- - - template < - typename image_type - > - void load_jpeg ( - image_type& image, - const std::string& file_name - ) - { - jpeg_loader(file_name).get_image(image); - } - -// ---------------------------------------------------------------------------------------- - -} - -#ifdef NO_MAKEFILE -#include "jpeg_loader.cpp" -#endif - -#endif // DLIB_JPEG_IMPORT - - diff --git a/ml/dlib/dlib/image_loader/jpeg_loader_abstract.h b/ml/dlib/dlib/image_loader/jpeg_loader_abstract.h deleted file mode 100644 index 48b5bb031..000000000 --- a/ml/dlib/dlib/image_loader/jpeg_loader_abstract.h +++ /dev/null @@ -1,133 +0,0 @@ -// Copyright (C) 2010 Davis E. King (davis@dlib.net), Nils Labugt -// License: Boost Software License See LICENSE.txt for the full license. -#undef DLIB_JPEG_IMPORT_ABSTRACT -#ifdef DLIB_JPEG_IMPORT_ABSTRACT - -#include "image_loader_abstract.h" -#include "../algs.h" -#include "../pixel.h" -#include "../dir_nav.h" -#include "../image_processing/generic_image.h" - -namespace dlib -{ - - class jpeg_loader : noncopyable - { - /*! - INITIAL VALUE - Defined by the constructors - - WHAT THIS OBJECT REPRESENTS - This object represents a class capable of loading JPEG image files. - Once an instance of it is created to contain a JPEG file from - disk you can obtain the image stored in it via get_image(). - !*/ - - public: - - jpeg_loader( - const char* filename - ); - /*! - ensures - - loads the JPEG file with the given file name into this object - throws - - std::bad_alloc - - image_load_error - This exception is thrown if there is some error that prevents - us from loading the given JPEG file. - !*/ - - jpeg_loader( - const std::string& filename - ); - /*! - ensures - - loads the JPEG file with the given file name into this object - throws - - std::bad_alloc - - image_load_error - This exception is thrown if there is some error that prevents - us from loading the given JPEG file. - !*/ - - jpeg_loader( - const dlib::file& f - ); - /*! - ensures - - loads the JPEG file with the given file name into this object - throws - - std::bad_alloc - - image_load_error - This exception is thrown if there is some error that prevents - us from loading the given JPEG file. - !*/ - - ~jpeg_loader( - ); - /*! - ensures - - all resources associated with *this has been released - !*/ - - bool is_gray( - ) const; - /*! - ensures - - if (this object contains a grayscale image) then - - returns true - - else - - returns false - !*/ - - bool is_rgb( - ) const; - /*! - ensures - - if (this object contains a 3 channel RGB image) then - - returns true - - else - - returns false - !*/ - - template< - typename image_type - > - void get_image( - image_type& img - ) const; - /*! - requires - - image_type == an image object that implements the interface defined in - dlib/image_processing/generic_image.h - ensures - - loads the JPEG image stored in this object into img - !*/ - - }; - -// ---------------------------------------------------------------------------------------- - - template < - typename image_type - > - void load_jpeg ( - image_type& image, - const std::string& file_name - ); - /*! - requires - - image_type == an image object that implements the interface defined in - dlib/image_processing/generic_image.h - ensures - - performs: jpeg_loader(file_name).get_image(image); - !*/ - -// ---------------------------------------------------------------------------------------- - -} - -#endif // DLIB_JPEG_IMPORT_ABSTRACT - diff --git a/ml/dlib/dlib/image_loader/load_image.h b/ml/dlib/dlib/image_loader/load_image.h deleted file mode 100644 index 64ccea9f2..000000000 --- a/ml/dlib/dlib/image_loader/load_image.h +++ /dev/null @@ -1,226 +0,0 @@ -// Copyright (C) 2011 Davis E. King (davis@dlib.net), Nils Labugt, Changjiang Yang (yangcha@leidos.com) -// License: Boost Software License See LICENSE.txt for the full license. -#ifndef DLIB_LOAd_IMAGE_Hh_ -#define DLIB_LOAd_IMAGE_Hh_ - -#include "load_image_abstract.h" -#include "../string.h" -#include "png_loader.h" -#include "jpeg_loader.h" -#include "image_loader.h" -#include <fstream> -#include <sstream> -#ifdef DLIB_GIF_SUPPORT -#include <gif_lib.h> -#endif - -namespace dlib -{ - namespace image_file_type - { - enum type - { - BMP, - JPG, - PNG, - DNG, - GIF, - UNKNOWN - }; - - inline type read_type(const std::string& file_name) - { - std::ifstream file(file_name.c_str(), std::ios::in|std::ios::binary); - if (!file) - throw image_load_error("Unable to open file: " + file_name); - - char buffer[9]; - file.read((char*)buffer, 8); - buffer[8] = 0; - - // Determine the true image type using link: - // http://en.wikipedia.org/wiki/List_of_file_signatures - - if (strcmp(buffer, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") == 0) - return PNG; - else if(buffer[0]=='\xff' && buffer[1]=='\xd8' && buffer[2]=='\xff') - return JPG; - else if(buffer[0]=='B' && buffer[1]=='M') - return BMP; - else if(buffer[0]=='D' && buffer[1]=='N' && buffer[2] == 'G') - return DNG; - else if(buffer[0]=='G' && buffer[1]=='I' && buffer[2] == 'F') - return GIF; - - return UNKNOWN; - } - }; - -// ---------------------------------------------------------------------------------------- - -// handle the differences in API between libgif v5 and older. -#if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5 -#define DLIB_GIFLIB_HANDLE_DIFF_VERSIONS ,0 -#else -#define DLIB_GIFLIB_HANDLE_DIFF_VERSIONS -#endif - - template <typename image_type> - void load_image ( - image_type& image, - const std::string& file_name - ) - { - const image_file_type::type im_type = image_file_type::read_type(file_name); - switch (im_type) - { - case image_file_type::BMP: load_bmp(image, file_name); return; - case image_file_type::DNG: load_dng(image, file_name); return; -#ifdef DLIB_PNG_SUPPORT - case image_file_type::PNG: load_png(image, file_name); return; -#endif -#ifdef DLIB_JPEG_SUPPORT - case image_file_type::JPG: load_jpeg(image, file_name); return; -#endif -#ifdef DLIB_GIF_SUPPORT - case image_file_type::GIF: - { - image_view<image_type> img(image); - GifFileType* gif = DGifOpenFileName(file_name.c_str() DLIB_GIFLIB_HANDLE_DIFF_VERSIONS); - try - { - if (gif == 0) throw image_load_error("Couldn't open file " + file_name); - if (DGifSlurp(gif) != GIF_OK) - throw image_load_error("Error reading from " + file_name); - - if (gif->ImageCount != 1) throw image_load_error("Dlib only supports reading GIF files containing one image."); - if (gif->SavedImages == 0) throw image_load_error("Unsupported GIF format 1."); - - ColorMapObject* cmo=gif->SColorMap?gif->SColorMap:gif->SavedImages->ImageDesc.ColorMap; - - if (cmo==0) throw image_load_error("Unsupported GIF format 2."); - if (cmo->Colors == 0) throw image_load_error("Unsupported GIF format 3."); - if (gif->SavedImages->ImageDesc.Width != gif->SWidth) throw image_load_error("Unsupported GIF format 4."); - if (gif->SavedImages->ImageDesc.Height != gif->SHeight) throw image_load_error("Unsupported GIF format 5."); - if (gif->SavedImages->RasterBits == 0) throw image_load_error("Unsupported GIF format 6."); - if (gif->Image.Top != 0) throw image_load_error("Unsupported GIF format 7."); - if (gif->Image.Left != 0) throw image_load_error("Unsupported GIF format 8."); - - img.set_size(gif->SHeight, gif->SWidth); - unsigned char* raster = gif->SavedImages->RasterBits; - GifColorType* colormap = cmo->Colors; - if (gif->Image.Interlace) - { - const long interlaced_offset[] = { 0, 4, 2, 1 }; - const long interlaced_jumps[] = { 8, 8, 4, 2 }; - for (int i = 0; i < 4; ++i) - { - for (long r = interlaced_offset[i]; r < img.nr(); r += interlaced_jumps[i]) - { - for (long c = 0; c < img.nc(); ++c) - { - if (*raster >= cmo->ColorCount) - throw image_load_error("Invalid GIF color value"); - rgb_pixel p; - p.red = colormap[*raster].Red; - p.green = colormap[*raster].Green; - p.blue = colormap[*raster].Blue; - assign_pixel(img[r][c], p); - ++raster; - } - } - } - } - else - { - for (long r = 0; r < img.nr(); ++r) - { - for (long c = 0; c < img.nc(); ++c) - { - if (*raster >= cmo->ColorCount) - throw image_load_error("Invalid GIF color value"); - rgb_pixel p; - p.red = colormap[*raster].Red; - p.green = colormap[*raster].Green; - p.blue = colormap[*raster].Blue; - assign_pixel(img[r][c], p); - ++raster; - } - } - } - DGifCloseFile(gif DLIB_GIFLIB_HANDLE_DIFF_VERSIONS); - } - catch(...) - { - if (gif) - DGifCloseFile(gif DLIB_GIFLIB_HANDLE_DIFF_VERSIONS); - throw; - } - return; - } -#endif - default: ; - } - - if (im_type == image_file_type::JPG) - { - std::ostringstream sout; - sout << "Unable to load image in file " + file_name + ".\n" + - "You must #define DLIB_JPEG_SUPPORT and link to libjpeg to read JPEG files.\n" + - "Do this by following the instructions at http://dlib.net/compile.html.\n\n"; -#ifdef _MSC_VER - sout << "Note that you must cause DLIB_JPEG_SUPPORT to be defined for your entire project.\n"; - sout << "So don't #define it in one file. Instead, add it to the C/C++->Preprocessor->Preprocessor Definitions\n"; - sout << "field in Visual Studio's Property Pages window so it takes effect for your entire application."; -#else - sout << "Note that you must cause DLIB_JPEG_SUPPORT to be defined for your entire project.\n"; - sout << "So don't #define it in one file. Instead, use a compiler switch like -DDLIB_JPEG_SUPPORT\n"; - sout << "so it takes effect for your entire application."; -#endif - throw image_load_error(sout.str()); - } - else if (im_type == image_file_type::PNG) - { - std::ostringstream sout; - sout << "Unable to load image in file " + file_name + ".\n" + - "You must #define DLIB_PNG_SUPPORT and link to libpng to read PNG files.\n" + - "Do this by following the instructions at http://dlib.net/compile.html.\n\n"; -#ifdef _MSC_VER - sout << "Note that you must cause DLIB_PNG_SUPPORT to be defined for your entire project.\n"; - sout << "So don't #define it in one file. Instead, add it to the C/C++->Preprocessor->Preprocessor Definitions\n"; - sout << "field in Visual Studio's Property Pages window so it takes effect for your entire application.\n"; -#else - sout << "Note that you must cause DLIB_PNG_SUPPORT to be defined for your entire project.\n"; - sout << "So don't #define it in one file. Instead, use a compiler switch like -DDLIB_PNG_SUPPORT\n"; - sout << "so it takes effect for your entire application."; -#endif - throw image_load_error(sout.str()); - } - else if (im_type == image_file_type::GIF) - { - std::ostringstream sout; - sout << "Unable to load image in file " + file_name + ".\n" + - "You must #define DLIB_GIF_SUPPORT and link to libgif to read GIF files.\n\n"; -#ifdef _MSC_VER - sout << "Note that you must cause DLIB_GIF_SUPPORT to be defined for your entire project.\n"; - sout << "So don't #define it in one file. Instead, add it to the C/C++->Preprocessor->Preprocessor Definitions\n"; - sout << "field in Visual Studio's Property Pages window so it takes effect for your entire application.\n"; -#else - sout << "Note that you must cause DLIB_GIF_SUPPORT to be defined for your entire project.\n"; - sout << "So don't #define it in one file. Instead, use a compiler switch like -DDLIB_GIF_SUPPORT\n"; - sout << "so it takes effect for your entire application."; -#endif - throw image_load_error(sout.str()); - } - else - { - throw image_load_error("Unknown image file format: Unable to load image in file " + file_name); - } - } - -// ---------------------------------------------------------------------------------------- - -} - -#endif // DLIB_LOAd_IMAGE_Hh_ - diff --git a/ml/dlib/dlib/image_loader/load_image_abstract.h b/ml/dlib/dlib/image_loader/load_image_abstract.h deleted file mode 100644 index f357bb278..000000000 --- a/ml/dlib/dlib/image_loader/load_image_abstract.h +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (C) 2011 Davis E. King (davis@dlib.net), Nils Labugt -// License: Boost Software License See LICENSE.txt for the full license. -#undef DLIB_LOAd_IMAGE_ABSTRACT_ -#ifdef DLIB_LOAd_IMAGE_ABSTRACT_ - -#include "../image_processing/generic_image.h" - -namespace dlib -{ - template <typename image_type> - void load_image ( - image_type& image, - const std::string& file_name - ); - /*! - requires - - image_type == an image object that implements the interface defined in - dlib/image_processing/generic_image.h - ensures - - This function loads an image from disk, in the indicated file file_name, and - writes it to the indicated image object. - - It is capable of reading the PNG, JPEG, BMP, GIF, and DNG image formats. It - is always capable of reading BMP and DNG images. However, for PNG, JPEG, and - GIF you must #define DLIB_PNG_SUPPORT, DLIB_JPEG_SUPPORT, and - DLIB_GIF_SUPPORT respectively and link your program to libpng, libjpeg, and - libgif respectively. - throws - - image_load_error - This exception is thrown if there is some error that prevents - us from loading the given image file. - !*/ - -} - -#endif // DLIB_LOAd_IMAGE_ABSTRACT_ - - diff --git a/ml/dlib/dlib/image_loader/png_loader.cpp b/ml/dlib/dlib/image_loader/png_loader.cpp deleted file mode 100644 index 3346ddb6a..000000000 --- a/ml/dlib/dlib/image_loader/png_loader.cpp +++ /dev/null @@ -1,222 +0,0 @@ -// Copyright (C) 2008 Davis E. King (davis@dlib.net), Nils Labugt -// License: Boost Software License See LICENSE.txt for the full license. -#ifndef DLIB_PNG_LOADER_CPp_ -#define DLIB_PNG_LOADER_CPp_ - -// only do anything with this file if DLIB_PNG_SUPPORT is defined -#ifdef DLIB_PNG_SUPPORT - -#include "../array2d.h" -#include "../pixel.h" -#include "../dir_nav.h" -#include "png_loader.h" -#include <png.h> -#include "../string.h" -#include "../byte_orderer.h" -#include <sstream> -#include <cstring> - -namespace dlib -{ - -// ---------------------------------------------------------------------------------------- - - struct LibpngData - { - png_bytep* row_pointers_; - png_structp png_ptr_; - png_infop info_ptr_; - png_infop end_info_; - }; - -// ---------------------------------------------------------------------------------------- - - png_loader:: - png_loader( const char* filename ) : height_( 0 ), width_( 0 ) - { - read_image( filename ); - } - -// ---------------------------------------------------------------------------------------- - - png_loader:: - png_loader( const std::string& filename ) : height_( 0 ), width_( 0 ) - { - read_image( filename.c_str() ); - } - -// ---------------------------------------------------------------------------------------- - - png_loader:: - png_loader( const dlib::file& f ) : height_( 0 ), width_( 0 ) - { - read_image( f.full_name().c_str() ); - } - -// ---------------------------------------------------------------------------------------- - - const unsigned char* png_loader::get_row( unsigned i ) const - { - return ld_->row_pointers_[i]; - } - -// ---------------------------------------------------------------------------------------- - - png_loader::~png_loader() - { - if ( ld_ && ld_->row_pointers_ != NULL ) - png_destroy_read_struct( &( ld_->png_ptr_ ), &( ld_->info_ptr_ ), &( ld_->end_info_ ) ); - } - -// ---------------------------------------------------------------------------------------- - - bool png_loader::is_gray() const - { - return ( color_type_ == PNG_COLOR_TYPE_GRAY ); - } - -// ---------------------------------------------------------------------------------------- - - bool png_loader::is_graya() const - { - return ( color_type_ == PNG_COLOR_TYPE_GRAY_ALPHA ); - } - -// ---------------------------------------------------------------------------------------- - - bool png_loader::is_rgb() const - { - return ( color_type_ == PNG_COLOR_TYPE_RGB ); - } - -// ---------------------------------------------------------------------------------------- - - bool png_loader::is_rgba() const - { - return ( color_type_ == PNG_COLOR_TYPE_RGB_ALPHA ); - } - -// ---------------------------------------------------------------------------------------- - - // Don't do anything when libpng calls us to tell us about an error. Just return to - // our own code and throw an exception (at the long jump target). - void png_loader_user_error_fn_silent(png_structp png_struct, png_const_charp ) - { - longjmp(png_jmpbuf(png_struct),1); - } - void png_loader_user_warning_fn_silent(png_structp , png_const_charp ) - { - } - - void png_loader::read_image( const char* filename ) - { - ld_.reset(new LibpngData); - if ( filename == NULL ) - { - throw image_load_error("png_loader: invalid filename, it is NULL"); - } - FILE *fp = fopen( filename, "rb" ); - if ( !fp ) - { - throw image_load_error(std::string("png_loader: unable to open file ") + filename); - } - png_byte sig[8]; - if (fread( sig, 1, 8, fp ) != 8) - { - fclose( fp ); - throw image_load_error(std::string("png_loader: error reading file ") + filename); - } - if ( png_sig_cmp( sig, 0, 8 ) != 0 ) - { - fclose( fp ); - throw image_load_error(std::string("png_loader: format error in file ") + filename); - } - ld_->png_ptr_ = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, &png_loader_user_error_fn_silent, &png_loader_user_warning_fn_silent ); - if ( ld_->png_ptr_ == NULL ) - { - fclose( fp ); - std::ostringstream sout; - sout << "Error, unable to allocate png structure while opening file " << filename << std::endl; - const char* runtime_version = png_get_header_ver(NULL); - if (runtime_version && std::strcmp(PNG_LIBPNG_VER_STRING, runtime_version) != 0) - { - sout << "This is happening because you compiled against one version of libpng, but then linked to another." << std::endl; - sout << "Compiled against libpng version: " << PNG_LIBPNG_VER_STRING << std::endl; - sout << "Linking to this version of libpng: " << runtime_version << std::endl; - } - throw image_load_error(sout.str()); - } - ld_->info_ptr_ = png_create_info_struct( ld_->png_ptr_ ); - if ( ld_->info_ptr_ == NULL ) - { - fclose( fp ); - png_destroy_read_struct( &( ld_->png_ptr_ ), ( png_infopp )NULL, ( png_infopp )NULL ); - throw image_load_error(std::string("png_loader: parse error in file ") + filename); - } - ld_->end_info_ = png_create_info_struct( ld_->png_ptr_ ); - if ( ld_->end_info_ == NULL ) - { - fclose( fp ); - png_destroy_read_struct( &( ld_->png_ptr_ ), &( ld_->info_ptr_ ), ( png_infopp )NULL ); - throw image_load_error(std::string("png_loader: parse error in file ") + filename); - } - - if (setjmp(png_jmpbuf(ld_->png_ptr_))) - { - // If we get here, we had a problem writing the file - fclose(fp); - png_destroy_read_struct( &( ld_->png_ptr_ ), &( ld_->info_ptr_ ), &( ld_->end_info_ ) ); - throw image_load_error(std::string("png_loader: parse error in file ") + filename); - } - - png_set_palette_to_rgb(ld_->png_ptr_); - - png_init_io( ld_->png_ptr_, fp ); - png_set_sig_bytes( ld_->png_ptr_, 8 ); - // flags force one byte per channel output - byte_orderer bo; - int png_transforms = PNG_TRANSFORM_PACKING; - if (bo.host_is_little_endian()) - png_transforms |= PNG_TRANSFORM_SWAP_ENDIAN; - png_read_png( ld_->png_ptr_, ld_->info_ptr_, png_transforms, NULL ); - height_ = png_get_image_height( ld_->png_ptr_, ld_->info_ptr_ ); - width_ = png_get_image_width( ld_->png_ptr_, ld_->info_ptr_ ); - bit_depth_ = png_get_bit_depth( ld_->png_ptr_, ld_->info_ptr_ ); - color_type_ = png_get_color_type( ld_->png_ptr_, ld_-> info_ptr_ ); - - - if (color_type_ != PNG_COLOR_TYPE_GRAY && - color_type_ != PNG_COLOR_TYPE_RGB && - color_type_ != PNG_COLOR_TYPE_RGB_ALPHA && - color_type_ != PNG_COLOR_TYPE_GRAY_ALPHA) - { - fclose( fp ); - png_destroy_read_struct( &( ld_->png_ptr_ ), &( ld_->info_ptr_ ), &( ld_->end_info_ ) ); - throw image_load_error(std::string("png_loader: unsupported color type in file ") + filename); - } - - if (bit_depth_ != 8 && bit_depth_ != 16) - { - fclose( fp ); - png_destroy_read_struct( &( ld_->png_ptr_ ), &( ld_->info_ptr_ ), &( ld_->end_info_ ) ); - throw image_load_error("png_loader: unsupported bit depth of " + cast_to_string(bit_depth_) + " in file " + std::string(filename)); - } - - ld_->row_pointers_ = png_get_rows( ld_->png_ptr_, ld_->info_ptr_ ); - - fclose( fp ); - if ( ld_->row_pointers_ == NULL ) - { - png_destroy_read_struct( &( ld_->png_ptr_ ), &( ld_->info_ptr_ ), &( ld_->end_info_ ) ); - throw image_load_error(std::string("png_loader: parse error in file ") + filename); - } - } - -// ---------------------------------------------------------------------------------------- - -} - -#endif // DLIB_PNG_SUPPORT - -#endif // DLIB_PNG_LOADER_CPp_ - diff --git a/ml/dlib/dlib/image_loader/png_loader.h b/ml/dlib/dlib/image_loader/png_loader.h deleted file mode 100644 index 291d3fddd..000000000 --- a/ml/dlib/dlib/image_loader/png_loader.h +++ /dev/null @@ -1,223 +0,0 @@ -// Copyright (C) 2008 Davis E. King (davis@dlib.net), Nils Labugt -// License: Boost Software License See LICENSE.txt for the full license. -#ifndef DLIB_PNG_IMPORT -#define DLIB_PNG_IMPORT - -#include <memory> - -#include "png_loader_abstract.h" -#include "image_loader.h" -#include "../pixel.h" -#include "../dir_nav.h" -#include "../test_for_odr_violations.h" - -namespace dlib -{ - - struct LibpngData; - class png_loader : noncopyable - { - public: - - png_loader( const char* filename ); - png_loader( const std::string& filename ); - png_loader( const dlib::file& f ); - ~png_loader(); - - bool is_gray() const; - bool is_graya() const; - bool is_rgb() const; - bool is_rgba() const; - - unsigned int bit_depth () const { return bit_depth_; } - - template<typename T> - void get_image( T& t_) const - { -#ifndef DLIB_PNG_SUPPORT - /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - You are getting this error because you are trying to use the png_loader - object but you haven't defined DLIB_PNG_SUPPORT. You must do so to use - this object. You must also make sure you set your build environment - to link against the libpng library. - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ - COMPILE_TIME_ASSERT(sizeof(T) == 0); -#endif - - typedef typename image_traits<T>::pixel_type pixel_type; - image_view<T> t(t_); - t.set_size( height_, width_ ); - - - if (is_gray() && bit_depth_ == 8) - { - for ( unsigned n = 0; n < height_;n++ ) - { - const unsigned char* v = get_row( n ); - for ( unsigned m = 0; m < width_;m++ ) - { - unsigned char p = v[m]; - assign_pixel( t[n][m], p ); - } - } - } - else if (is_gray() && bit_depth_ == 16) - { - for ( unsigned n = 0; n < height_;n++ ) - { - const uint16* v = (uint16*)get_row( n ); - for ( unsigned m = 0; m < width_;m++ ) - { - dlib::uint16 p = v[m]; - assign_pixel( t[n][m], p ); - } - } - } - else if (is_graya() && bit_depth_ == 8) - { - for ( unsigned n = 0; n < height_;n++ ) - { - const unsigned char* v = get_row( n ); - for ( unsigned m = 0; m < width_; m++ ) - { - unsigned char p = v[m*2]; - if (!pixel_traits<pixel_type>::has_alpha) - { - assign_pixel( t[n][m], p ); - } - else - { - unsigned char pa = v[m*2+1]; - rgb_alpha_pixel pix; - assign_pixel(pix, p); - assign_pixel(pix.alpha, pa); - assign_pixel(t[n][m], pix); - } - } - } - } - else if (is_graya() && bit_depth_ == 16) - { - for ( unsigned n = 0; n < height_;n++ ) - { - const uint16* v = (uint16*)get_row( n ); - for ( unsigned m = 0; m < width_; m++ ) - { - dlib::uint16 p = v[m*2]; - if (!pixel_traits<pixel_type>::has_alpha) - { - assign_pixel( t[n][m], p ); - } - else - { - dlib::uint16 pa = v[m*2+1]; - rgb_alpha_pixel pix; - assign_pixel(pix, p); - assign_pixel(pix.alpha, pa); - assign_pixel(t[n][m], pix); - } - } - } - } - else if (is_rgb() && bit_depth_ == 8) - { - for ( unsigned n = 0; n < height_;n++ ) - { - const unsigned char* v = get_row( n ); - for ( unsigned m = 0; m < width_;m++ ) - { - rgb_pixel p; - p.red = v[m*3]; - p.green = v[m*3+1]; - p.blue = v[m*3+2]; - assign_pixel( t[n][m], p ); - } - } - } - else if (is_rgb() && bit_depth_ == 16) - { - for ( unsigned n = 0; n < height_;n++ ) - { - const uint16* v = (uint16*)get_row( n ); - for ( unsigned m = 0; m < width_;m++ ) - { - rgb_pixel p; - p.red = static_cast<uint8>(v[m*3]); - p.green = static_cast<uint8>(v[m*3+1]); - p.blue = static_cast<uint8>(v[m*3+2]); - assign_pixel( t[n][m], p ); - } - } - } - else if (is_rgba() && bit_depth_ == 8) - { - if (!pixel_traits<pixel_type>::has_alpha) - assign_all_pixels(t,0); - - for ( unsigned n = 0; n < height_;n++ ) - { - const unsigned char* v = get_row( n ); - for ( unsigned m = 0; m < width_;m++ ) - { - rgb_alpha_pixel p; - p.red = v[m*4]; - p.green = v[m*4+1]; - p.blue = v[m*4+2]; - p.alpha = v[m*4+3]; - assign_pixel( t[n][m], p ); - } - } - } - else if (is_rgba() && bit_depth_ == 16) - { - if (!pixel_traits<pixel_type>::has_alpha) - assign_all_pixels(t,0); - - for ( unsigned n = 0; n < height_;n++ ) - { - const uint16* v = (uint16*)get_row( n ); - for ( unsigned m = 0; m < width_;m++ ) - { - rgb_alpha_pixel p; - p.red = static_cast<uint8>(v[m*4]); - p.green = static_cast<uint8>(v[m*4+1]); - p.blue = static_cast<uint8>(v[m*4+2]); - p.alpha = static_cast<uint8>(v[m*4+3]); - assign_pixel( t[n][m], p ); - } - } - } - } - - private: - const unsigned char* get_row( unsigned i ) const; - void read_image( const char* filename ); - unsigned height_, width_; - unsigned bit_depth_; - int color_type_; - std::unique_ptr<LibpngData> ld_; - }; - -// ---------------------------------------------------------------------------------------- - - template < - typename image_type - > - void load_png ( - image_type& image, - const std::string& file_name - ) - { - png_loader(file_name).get_image(image); - } - -// ---------------------------------------------------------------------------------------- - -} - -#ifdef NO_MAKEFILE -#include "png_loader.cpp" -#endif - -#endif // DLIB_PNG_IMPORT - diff --git a/ml/dlib/dlib/image_loader/png_loader_abstract.h b/ml/dlib/dlib/image_loader/png_loader_abstract.h deleted file mode 100644 index d81e7f83a..000000000 --- a/ml/dlib/dlib/image_loader/png_loader_abstract.h +++ /dev/null @@ -1,162 +0,0 @@ -// Copyright (C) 2008 Davis E. King (davis@dlib.net), Nils Labugt -// License: Boost Software License See LICENSE.txt for the full license. -#undef DLIB_PNG_IMPORT_ABSTRACT -#ifdef DLIB_PNG_IMPORT_ABSTRACT - -#include "image_loader_abstract.h" -#include "../algs.h" -#include "../pixel.h" -#include "../dir_nav.h" -#include "../image_processing/generic_image.h" - -namespace dlib -{ - - class png_loader : noncopyable - { - /*! - INITIAL VALUE - Defined by the constructors - - WHAT THIS OBJECT REPRESENTS - This object represents a class capable of loading PNG image files. - Once an instance of it is created to contain a PNG file from - disk you can obtain the image stored in it via get_image(). - !*/ - - public: - - png_loader( - const char* filename - ); - /*! - ensures - - loads the PNG file with the given file name into this object - throws - - std::bad_alloc - - image_load_error - This exception is thrown if there is some error that prevents - us from loading the given PNG file. - !*/ - - png_loader( - const std::string& filename - ); - /*! - ensures - - loads the PNG file with the given file name into this object - throws - - std::bad_alloc - - image_load_error - This exception is thrown if there is some error that prevents - us from loading the given PNG file. - !*/ - - png_loader( - const dlib::file& f - ); - /*! - ensures - - loads the PNG file with the given file name into this object - throws - - std::bad_alloc - - image_load_error - This exception is thrown if there is some error that prevents - us from loading the given PNG file. - !*/ - - ~png_loader( - ); - /*! - ensures - - all resources associated with *this has been released - !*/ - - bool is_gray( - ) const; - /*! - ensures - - if (this object contains a grayscale image without an alpha channel) then - - returns true - - else - - returns false - !*/ - - bool is_graya( - ) const; - /*! - ensures - - if (this object contains a grayscale image with an alpha channel) then - - returns true - - else - - returns false - !*/ - - bool is_rgb( - ) const; - /*! - ensures - - if (this object contains a 3 channel RGB image) then - - returns true - - else - - returns false - !*/ - - bool is_rgba( - ) const; - /*! - ensures - - if (this object contains a 4 channel RGB alpha image) then - - returns true - - else - - returns false - !*/ - - unsigned int bit_depth ( - ) const; - /*! - ensures - - returns the number of bits per channel in the image contained by this - object. The possible values are 8 or 16. - !*/ - - template< - typename image_type - > - void get_image( - image_type& img - ) const; - /*! - requires - - image_type == an image object that implements the interface defined in - dlib/image_processing/generic_image.h - ensures - - loads the PNG image stored in this object into img - !*/ - - }; - -// ---------------------------------------------------------------------------------------- - - template < - typename image_type - > - void load_png ( - image_type& image, - const std::string& file_name - ); - /*! - requires - - image_type == an image object that implements the interface defined in - dlib/image_processing/generic_image.h - ensures - - performs: png_loader(file_name).get_image(image); - !*/ - -// ---------------------------------------------------------------------------------------- - -} - -#endif // DLIB_PNG_IMPORT_ABSTRACT - - |