| 1 | // Copyright (C) 2006-2009 by Tor Andersson. |
| 2 | // Copyright (C) 2010 by Ben Cressey, Chris Spiegel. |
| 3 | // |
| 4 | // This file is part of Gargoyle. |
| 5 | // |
| 6 | // Gargoyle is free software; you can redistribute it and/or modify |
| 7 | // it under the terms of the GNU General Public License as published by |
| 8 | // the Free Software Foundation; either version 2 of the License, or |
| 9 | // (at your option) any later version. |
| 10 | // |
| 11 | // Gargoyle is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | // |
| 16 | // You should have received a copy of the GNU General Public License |
| 17 | // along with Gargoyle; if not, write to the Free Software |
| 18 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | |
| 20 | #include <functional> |
| 21 | #include <memory> |
| 22 | #include <stdexcept> |
| 23 | #include <string> |
| 24 | #include <unordered_map> |
| 25 | #include <vector> |
| 26 | |
| 27 | #ifdef GARGLK_CONFIG_JPEG_TURBO |
| 28 | #include <turbojpeg.h> |
| 29 | #else |
| 30 | #include <array> |
| 31 | #include <cstdio> |
| 32 | |
| 33 | // jpeglib.h requires these to be defined. |
| 34 | using std::size_t; |
| 35 | using std::FILE; |
| 36 | |
| 37 | #include <jpeglib.h> |
| 38 | #endif |
| 39 | |
| 40 | #include <png.h> |
| 41 | |
| 42 | #include "format.h" |
| 43 | |
| 44 | #include "glk.h" |
| 45 | #include "garglk.h" |
| 46 | #include "gi_blorb.h" |
| 47 | |
| 48 | static std::shared_ptr<picture_t> load_image_png(const std::vector<unsigned char> &buf, unsigned long id); |
| 49 | static std::shared_ptr<picture_t> load_image_jpeg(const std::vector<unsigned char> &buf, unsigned long id); |
| 50 | |
| 51 | namespace { |
| 52 | |
| 53 | struct LoadError : public std::runtime_error { |
| 54 | LoadError(const std::string &format, const std::string &msg) : std::runtime_error(Format("{} [{}]", msg, format)) { |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | struct PicturePair { |
| 59 | std::shared_ptr<picture_t> picture; |
| 60 | std::shared_ptr<picture_t> scaled; |
| 61 | }; |
| 62 | |
| 63 | } |
| 64 | |
| 65 | static std::unordered_map<unsigned long, PicturePair> picstore; |
| 66 | |
| 67 | static int gli_piclist_refcount = 0; // count references to loaded pictures |
| 68 | |
| 69 | void gli_piclist_increment() |
| 70 | { |
| 71 | gli_piclist_refcount++; |
| 72 | } |
| 73 | |
| 74 | void gli_piclist_decrement() |
| 75 | { |
| 76 | if (gli_piclist_refcount > 0 && --gli_piclist_refcount == 0) { |
| 77 | picstore.clear(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void gli_picture_store_original(const std::shared_ptr<picture_t> &pic) |
| 82 | { |
| 83 | picstore[pic->id] = PicturePair{pic, nullptr}; |
| 84 | } |
| 85 | |
| 86 | static void gli_picture_store_scaled(const std::shared_ptr<picture_t> &pic) |
| 87 | { |
| 88 | try { |
| 89 | auto &picpair = picstore.at(pic->id); |
| 90 | picpair.scaled = pic; |
| 91 | } catch (const std::out_of_range &) { |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void gli_picture_store(const std::shared_ptr<picture_t> &pic) |
| 96 | { |
| 97 | if (!pic) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | if (!pic->scaled) { |
| 102 | gli_picture_store_original(pic); |
| 103 | } else { |
| 104 | gli_picture_store_scaled(pic); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | std::shared_ptr<picture_t> gli_picture_retrieve(unsigned long id, bool scaled) |
| 109 | { |
| 110 | try { |
| 111 | const auto &picpair = picstore.at(id); |
| 112 | return scaled ? picpair.scaled : picpair.picture; |
| 113 | } catch (const std::out_of_range &) { |
| 114 | return nullptr; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | std::shared_ptr<picture_t> gli_picture_load(unsigned long id) |
| 119 | { |
| 120 | glui32 chunktype; |
| 121 | |
| 122 | auto pic = gli_picture_retrieve(id, false); |
| 123 | if (pic) { |
| 124 | return pic; |
| 125 | } |
| 126 | |
| 127 | std::vector<unsigned char> buf; |
| 128 | |
| 129 | if (giblorb_get_resource_map() != nullptr) { |
| 130 | if (!giblorb_copy_resource(giblorb_ID_Pict, id, chunktype, buf)) { |
| 131 | return nullptr; |
| 132 | } |
| 133 | } else { |
| 134 | const auto &resource_map = gli_get_resource_map(giblorb_ID_Pict); |
| 135 | if (!resource_map.empty()) { |
| 136 | try { |
| 137 | buf = resource_map.at(id); |
| 138 | } catch (const std::out_of_range &) { |
| 139 | return nullptr; |
| 140 | } |
| 141 | } else { |
| 142 | auto filename = Format("{}/PIC{}", gli_workdir, id); |
| 143 | |
| 144 | if (!garglk::read_file(filename, buf)) { |
| 145 | return nullptr; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (buf.size() < 8) { |
| 150 | return nullptr; |
| 151 | } |
| 152 | |
| 153 | if (png_sig_cmp(buf.data(), 0, 8) == 0) { |
| 154 | chunktype = giblorb_ID_PNG; |
| 155 | } else if (buf[0] == 0xFF && buf[1] == 0xD8 && buf[2] == 0xFF) { |
| 156 | chunktype = giblorb_ID_JPEG; |
| 157 | } else { |
| 158 | // Not a readable file. Forget it. |
| 159 | return nullptr; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | const std::unordered_map<int, std::function<std::shared_ptr<picture_t>(const std::vector<unsigned char> &, unsigned long)>> loaders = { |
| 164 | {giblorb_ID_PNG, load_image_png}, |
| 165 | {giblorb_ID_JPEG, load_image_jpeg}, |
| 166 | }; |
| 167 | |
| 168 | try { |
| 169 | pic = loaders.at(chunktype)(buf, id); |
| 170 | if (pic != nullptr) { |
| 171 | gli_picture_store(pic); |
| 172 | return pic; |
| 173 | } |
| 174 | } catch (const LoadError &e) { |
| 175 | gli_strict_warning(Format("unable to load image {}: {}", id, e.what())); |
| 176 | } catch (const std::out_of_range &) { |
| 177 | } |
| 178 | |
| 179 | return nullptr; |
| 180 | } |
| 181 | |
| 182 | static std::shared_ptr<picture_t> load_image_jpeg(const std::vector<unsigned char> &buf, unsigned long id) |
| 183 | { |
| 184 | #ifdef GARGLK_CONFIG_JPEG_TURBO |
| 185 | auto tj = garglk::unique(tjInitDecompress(), tjDestroy); |
| 186 | |
| 187 | int w, h, subsamp, colorspace; |
| 188 | |
| 189 | if (tjDecompressHeader3(tj.get(), buf.data(), buf.size(), &w, &h, &subsamp, &colorspace) != 0) { |
| 190 | throw LoadError("jpg", tjGetErrorStr2(tj.get())); |
| 191 | } |
| 192 | |
| 193 | Canvas<4> rgba(w, h); |
| 194 | |
| 195 | if (colorspace == TJCS_CMYK || colorspace == TJCS_YCCK) { |
| 196 | if (tjDecompress2(tj.get(), buf.data(), buf.size(), rgba.data(), w, w * tjPixelSize[TJPF_CMYK], h, TJPF_CMYK, TJFLAG_ACCURATEDCT) != 0) { |
| 197 | throw LoadError("jpg", tjGetErrorStr2(tj.get())); |
| 198 | } |
| 199 | |
| 200 | auto *p = rgba.data(); |
| 201 | for (int i = 0; i < w * h; i++) { |
| 202 | double K = p[i * 4 + 3]; |
| 203 | p[i * 4 + 0] = p[i * 4 + 0] * K / 255.0; |
| 204 | p[i * 4 + 1] = p[i * 4 + 1] * K / 255.0; |
| 205 | p[i * 4 + 2] = p[i * 4 + 2] * K / 255.0; |
| 206 | p[i * 4 + 3] = 0xff; |
| 207 | } |
| 208 | } else { |
| 209 | if (tjDecompress2(tj.get(), buf.data(), buf.size(), rgba.data(), w, w * tjPixelSize[TJPF_RGBA], h, TJPF_RGBA, TJFLAG_ACCURATEDCT) != 0) { |
| 210 | throw LoadError("jpg", tjGetErrorStr2(tj.get())); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return std::make_shared<picture_t>(id, rgba, false); |
| 215 | #else |
| 216 | jpeg_decompress_struct cinfo; |
| 217 | jpeg_error_mgr jerr; |
| 218 | std::array<JSAMPROW, 1> rowarray; |
| 219 | |
| 220 | cinfo.err = jpeg_std_error(&jerr); |
| 221 | jerr.error_exit = [](j_common_ptr cinfo) { |
| 222 | std::array<char, JMSG_LENGTH_MAX> msg; |
| 223 | cinfo->err->format_message(cinfo, msg.data()); |
| 224 | throw LoadError("jpg", msg.data()); |
| 225 | }; |
| 226 | |
| 227 | jpeg_create_decompress(&cinfo); |
| 228 | auto jpeg_free = garglk::unique(&cinfo, jpeg_destroy_decompress); |
| 229 | |
| 230 | jpeg_mem_src(&cinfo, buf.data(), buf.size()); |
| 231 | jpeg_read_header(&cinfo, TRUE); |
| 232 | jpeg_start_decompress(&cinfo); |
| 233 | |
| 234 | if (cinfo.out_color_space != JCS_GRAYSCALE && |
| 235 | cinfo.out_color_space != JCS_RGB && |
| 236 | cinfo.out_color_space != JCS_CMYK) { |
| 237 | |
| 238 | return nullptr; |
| 239 | } |
| 240 | |
| 241 | Canvas<4> rgba(cinfo.output_width, cinfo.output_height); |
| 242 | |
| 243 | std::vector<JSAMPLE> row(cinfo.output_width * cinfo.output_components); |
| 244 | rowarray[0] = row.data(); |
| 245 | |
| 246 | while (cinfo.output_scanline < cinfo.output_height) { |
| 247 | JDIMENSION y = cinfo.output_scanline; |
| 248 | jpeg_read_scanlines(&cinfo, rowarray.data(), 1); |
| 249 | if (cinfo.out_color_space == JCS_GRAYSCALE) { |
| 250 | for (int i = 0; i < rgba.width(); i++) { |
| 251 | rgba[y][i] = Pixel<4>(row[i], row[i], row[i], 0xff); |
| 252 | } |
| 253 | } else if (cinfo.out_color_space == JCS_RGB) { |
| 254 | for (int i = 0; i < rgba.width(); i++) { |
| 255 | rgba[y][i] = Pixel<4>(row[i * 3 + 0], row[i * 3 + 1], row[i * 3 + 2], 0xff); |
| 256 | } |
| 257 | } else if (cinfo.out_color_space == JCS_CMYK) { |
| 258 | for (int i = 0; i < rgba.width(); i++) { |
| 259 | double K = row[i * 4 + 3]; |
| 260 | rgba[y][i] = Pixel<4>( |
| 261 | row[i * 4 + 0] * K / 255.0, |
| 262 | row[i * 4 + 1] * K / 255.0, |
| 263 | row[i * 4 + 2] * K / 255.0, |
| 264 | 0xff); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | jpeg_finish_decompress(&cinfo); |
| 270 | |
| 271 | return std::make_shared<picture_t>(id, rgba, false); |
| 272 | #endif |
| 273 | } |
| 274 | |
| 275 | static std::shared_ptr<picture_t> load_image_png(const std::vector<unsigned char> &buf, unsigned long id) |
| 276 | { |
| 277 | png_image image; |
| 278 | |
| 279 | image.version = PNG_IMAGE_VERSION; |
| 280 | image.opaque = nullptr; |
| 281 | auto free_png = garglk::unique(&image, png_image_free); |
| 282 | |
| 283 | if (png_image_begin_read_from_memory(&image, buf.data(), buf.size()) == 0) { |
| 284 | throw LoadError("png", image.message); |
| 285 | } |
| 286 | |
| 287 | image.format = PNG_FORMAT_RGBA; |
| 288 | Canvas<4> rgba(image.width, image.height); |
| 289 | |
| 290 | if (png_image_finish_read(&image, nullptr, rgba.data(), 0, nullptr) == 0) { |
| 291 | throw LoadError("png", image.message); |
| 292 | } |
| 293 | |
| 294 | auto pic = std::make_shared<picture_t>(id, rgba, false); |
| 295 | |
| 296 | return pic; |
| 297 | } |