Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 1 | #define LOG_TAG "ImageIo" |
| 2 | |
| 3 | #include <private/dvr/image_io.h> |
| 4 | |
| 5 | #include <algorithm> |
| 6 | #include <memory> |
| 7 | #include <string> |
| 8 | |
| 9 | #include <private/dvr/image_io_base.h> |
| 10 | #include <private/dvr/image_io_logging.h> |
| 11 | #include <private/dvr/image_io_png.h> |
| 12 | #include <private/dvr/image_io_ppm.h> |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | // Returns true if |str| ends with |suffix|. |
| 17 | bool EndsWith(const std::string& str, const std::string& suffix) { |
| 18 | if (str.length() < suffix.length()) |
| 19 | return false; |
| 20 | |
| 21 | return std::equal(suffix.rbegin(), suffix.rend(), str.rbegin()); |
| 22 | } |
| 23 | |
| 24 | // Returns lower case copy of the input string. |
| 25 | std::string ToLower(std::string str) { |
| 26 | std::transform(str.begin(), str.end(), str.begin(), |
| 27 | [](char x) { return std::tolower(x); }); |
| 28 | return str; |
| 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
| 33 | std::unique_ptr<ImageIoReader> ImageIoReader::Create(const char* filename) { |
| 34 | std::unique_ptr<ImageIoReader> reader; |
| 35 | std::string filename_lower = ToLower(filename); |
| 36 | |
| 37 | if (EndsWith(filename_lower, ".ppm")) |
| 38 | reader.reset(new ImageIoPpmReader(filename)); |
| 39 | |
| 40 | if (!reader) { |
| 41 | ALOGE("Unknown/unsupported image file format."); |
| 42 | return nullptr; |
| 43 | } |
| 44 | |
| 45 | return reader; |
| 46 | } |
| 47 | |
| 48 | std::unique_ptr<ImageIoWriter> ImageIoWriter::Create(const char* filename, |
| 49 | int width, int height, |
| 50 | const uint8_t* image) { |
| 51 | std::unique_ptr<ImageIoWriter> writer; |
| 52 | std::string filename_lower = ToLower(filename); |
| 53 | |
| 54 | if (EndsWith(filename_lower, ".ppm")) |
| 55 | writer.reset(new ImageIoPpmWriter(filename, width, height, image)); |
| 56 | else if (EndsWith(filename_lower, ".png")) |
| 57 | writer.reset(new ImageIoPngWriter(filename, width, height, image)); |
| 58 | |
| 59 | if (!writer) { |
| 60 | ALOGE("Unknown/unsupported image file format."); |
| 61 | return nullptr; |
| 62 | } |
| 63 | |
| 64 | return writer; |
| 65 | } |
| 66 | |
| 67 | extern "C" { |
| 68 | |
| 69 | bool image_io_write_rgb888(const char* filename, int width, int height, |
| 70 | const uint8_t* image) { |
| 71 | auto writer = ImageIoWriter::Create(filename, width, height, image); |
| 72 | if (!writer) |
| 73 | return false; |
| 74 | return writer->WriteRgb888(); |
| 75 | } |
| 76 | |
| 77 | bool image_io_read_rgb888(const char* filename, int* width, int* height, |
| 78 | uint8_t** image) { |
| 79 | auto reader = ImageIoReader::Create(filename); |
| 80 | if (!reader) |
| 81 | return false; |
| 82 | if (!reader->ReadRgb888()) |
| 83 | return false; |
| 84 | *width = reader->width(); |
| 85 | *height = reader->height(); |
| 86 | *image = reader->ReleaseImage(); |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | void image_io_release_buffer(uint8_t* image) { delete[] image; } |
| 91 | |
| 92 | } // extern "C" |