| 1 | // Copyright (C) 2006-2009 by Tor Andersson. |
| 2 | // Copyright (C) 2010 by Ben Cressey. |
| 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 | // Image scaling, based on pnmscale.c... |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <cmath> |
| 24 | #include <cstdint> |
| 25 | #include <memory> |
| 26 | #include <utility> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include "glk.h" |
| 30 | #include "garglk.h" |
| 31 | |
| 32 | #ifdef GARGLK_CONFIG_SCALERS |
| 33 | #include "hqx.h" |
| 34 | #include "xbrz.h" |
| 35 | #endif |
| 36 | |
| 37 | std::shared_ptr<picture_t> gli_picture_scale(const picture_t *src, int newcols, int newrows) |
| 38 | { |
| 39 | // pnmscale.c - read a portable anymap and scale it |
| 40 | // |
| 41 | // Copyright (C) 1989, 1991 by Jef Poskanzer. |
| 42 | // |
| 43 | // Permission to use, copy, modify, and distribute this software and its |
| 44 | // documentation for any purpose and without fee is hereby granted, provided |
| 45 | // that the above copyright notice appear in all copies and that both that |
| 46 | // copyright notice and this permission notice appear in supporting |
| 47 | // documentation. This software is provided "as is" without express or |
| 48 | // implied warranty. |
| 49 | |
| 50 | constexpr int SCALE = 4096; |
| 51 | constexpr int HALFSCALE = 2048; |
| 52 | constexpr long maxval = 255; |
| 53 | |
| 54 | auto dst = gli_picture_retrieve(src->id, true); |
| 55 | if (dst != nullptr && dst->w == newcols && dst->h == newrows) { |
| 56 | return dst; |
| 57 | } |
| 58 | |
| 59 | if (newcols == 0 || newrows == 0) { |
| 60 | dst = std::make_shared<picture_t>(src->id, Canvas<4>{}, true); |
| 61 | gli_picture_store(dst); |
| 62 | return dst; |
| 63 | } |
| 64 | |
| 65 | #ifdef GARGLK_CONFIG_SCALERS |
| 66 | int scaleby = std::ceil(std::max(static_cast<double>(newcols) / src->w, static_cast<double>(newrows) / src->h)); |
| 67 | |
| 68 | dst.reset(); |
| 69 | |
| 70 | // Don't apply a scaler to 1x1 images. Ideally scaling a 1x1 image |
| 71 | // would result in, for example, a 4x4 grid of the original pixel. |
| 72 | // But xBRZ adds alpha channels regardless of the input size, and if |
| 73 | // a 1x1 image is scaled by xBRZ here, then blown up below to a |
| 74 | // large size, it looks completely wrong. This is visible in |
| 75 | // Counterfeit Monkey's map, at the very least. Users should disable |
| 76 | // scalers for all but Infocom games, but that's not forced, nor |
| 77 | // should it be, so at least catch one known-bad case. The same |
| 78 | // issues will apply to other small images, but how to scale them is |
| 79 | // ambiguous. Scaling a 1x1 image by simply duplicating the pixel is |
| 80 | // obviously the correct approach. |
| 81 | if (scaleby > 1 && (src->w > 1 || src->h > 1)) { |
| 82 | if (gli_conf_scaler == Scaler::HQX) { |
| 83 | scaleby = std::min(scaleby, 4); |
| 84 | |
| 85 | static bool hqx_initialized = false; |
| 86 | if (!hqx_initialized) { |
| 87 | hqxInit(); |
| 88 | hqx_initialized = true; |
| 89 | } |
| 90 | |
| 91 | auto hqx = scaleby == 4 ? hq4x_32 : |
| 92 | scaleby == 3 ? hq3x_32 : |
| 93 | hq2x_32; |
| 94 | |
| 95 | Canvas<4> scaled_canvas(src->w * scaleby, src->h * scaleby); |
| 96 | hqx(reinterpret_cast<const std::uint32_t *>(src->rgba.data()), reinterpret_cast<std::uint32_t *>(scaled_canvas.data()), src->w, src->h); |
| 97 | dst = std::make_unique<picture_t>(src->id, std::move(scaled_canvas), true); |
| 98 | src = dst.get(); |
| 99 | } else if (gli_conf_scaler == Scaler::XBRZ) { |
| 100 | scaleby = std::min(scaleby, xbrz::SCALE_FACTOR_MAX); |
| 101 | |
| 102 | Canvas<4> scaled_canvas(src->w * scaleby, src->h * scaleby); |
| 103 | if (xbrz::scale(scaleby, reinterpret_cast<const std::uint32_t *>(src->rgba.data()), reinterpret_cast<std::uint32_t *>(scaled_canvas.data()), src->w, src->h, xbrz::ColorFormat::ARGB)) { |
| 104 | dst = std::make_unique<picture_t>(src->id, std::move(scaled_canvas), true); |
| 105 | src = dst.get(); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (dst != nullptr && dst->w == newcols && dst->h == newrows) { |
| 111 | gli_picture_store(dst); |
| 112 | return dst; |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | int row, col; |
| 117 | |
| 118 | int rowsread; |
| 119 | bool needtoreadrow; |
| 120 | |
| 121 | int cols = src->w; |
| 122 | int rows = src->h; |
| 123 | |
| 124 | double xscale, yscale; |
| 125 | long sxscale, syscale; |
| 126 | |
| 127 | long fracrowtofill, fracrowleft; |
| 128 | |
| 129 | // Allocate destination image and scratch space |
| 130 | |
| 131 | Canvas<4> rgba(newcols, newrows); |
| 132 | |
| 133 | std::vector<Pixel<4>> tempxelrow(cols, Pixel<4>(0, 0, 0, 0)); |
| 134 | std::vector<long> rs(cols, HALFSCALE); |
| 135 | std::vector<long> gs(cols, HALFSCALE); |
| 136 | std::vector<long> bs(cols, HALFSCALE); |
| 137 | std::vector<long> as(cols, HALFSCALE); |
| 138 | |
| 139 | // Compute all sizes and scales. |
| 140 | |
| 141 | xscale = static_cast<double>(newcols) / static_cast<double>(cols); |
| 142 | yscale = static_cast<double>(newrows) / static_cast<double>(rows); |
| 143 | sxscale = xscale * SCALE; |
| 144 | syscale = yscale * SCALE; |
| 145 | |
| 146 | rowsread = 1; |
| 147 | fracrowleft = syscale; |
| 148 | needtoreadrow = false; |
| 149 | |
| 150 | fracrowtofill = SCALE; |
| 151 | |
| 152 | for (row = 0; row < newrows; ++row) { |
| 153 | // First scale Y from src->rgba into tempxelrow. |
| 154 | { |
| 155 | while (fracrowleft < fracrowtofill) { |
| 156 | if (needtoreadrow && rowsread < rows) { |
| 157 | ++rowsread; |
| 158 | } |
| 159 | |
| 160 | for (col = 0; col < cols; ++col) { |
| 161 | auto alpha = src->rgba[rowsread - 1][col][3]; |
| 162 | rs[col] += fracrowleft * src->rgba[rowsread - 1][col][0] * alpha; |
| 163 | gs[col] += fracrowleft * src->rgba[rowsread - 1][col][1] * alpha; |
| 164 | bs[col] += fracrowleft * src->rgba[rowsread - 1][col][2] * alpha; |
| 165 | as[col] += fracrowleft * alpha; |
| 166 | } |
| 167 | |
| 168 | fracrowtofill -= fracrowleft; |
| 169 | fracrowleft = syscale; |
| 170 | needtoreadrow = true; |
| 171 | } |
| 172 | |
| 173 | // Now fracrowleft is >= fracrowtofill, so we can produce a row. |
| 174 | if (needtoreadrow && rowsread < rows) { |
| 175 | ++rowsread; |
| 176 | needtoreadrow = false; |
| 177 | } |
| 178 | |
| 179 | for (col = 0; col < cols; ++col) { |
| 180 | auto alpha = src->rgba[rowsread - 1][col][3]; |
| 181 | long r, g, b, a; |
| 182 | |
| 183 | a = as[col] + fracrowtofill * alpha; |
| 184 | |
| 185 | if (a == 0) { |
| 186 | r = g = b = a; |
| 187 | } else { |
| 188 | r = rs[col] + fracrowtofill * src->rgba[rowsread - 1][col][0] * alpha; |
| 189 | r = std::min(r / a, maxval); |
| 190 | |
| 191 | g = gs[col] + fracrowtofill * src->rgba[rowsread - 1][col][1] * alpha; |
| 192 | g = std::min(g / a, maxval); |
| 193 | |
| 194 | b = bs[col] + fracrowtofill * src->rgba[rowsread - 1][col][2] * alpha; |
| 195 | b = std::min(b / a, maxval); |
| 196 | |
| 197 | a = std::min(a / SCALE, maxval); |
| 198 | } |
| 199 | |
| 200 | tempxelrow[col] = Pixel<4>(r, g, b, a); |
| 201 | rs[col] = gs[col] = bs[col] = as[col] = HALFSCALE; |
| 202 | } |
| 203 | |
| 204 | fracrowleft -= fracrowtofill; |
| 205 | if (fracrowleft == 0) { |
| 206 | fracrowleft = syscale; |
| 207 | needtoreadrow = true; |
| 208 | } |
| 209 | fracrowtofill = SCALE; |
| 210 | } |
| 211 | |
| 212 | // Now scale X from tempxelrow into dst->rgba and write it out. |
| 213 | { |
| 214 | long r, g, b, a; |
| 215 | long fraccoltofill, fraccolleft; |
| 216 | bool needcol; |
| 217 | |
| 218 | fraccoltofill = SCALE; |
| 219 | r = g = b = a = HALFSCALE; |
| 220 | needcol = false; |
| 221 | |
| 222 | int dstcol = 0; |
| 223 | for (col = 0; col < cols; ++col) { |
| 224 | auto alpha = tempxelrow[col][3]; |
| 225 | auto tempxel_blended_r = tempxelrow[col][0] * alpha; |
| 226 | auto tempxel_blended_g = tempxelrow[col][1] * alpha; |
| 227 | auto tempxel_blended_b = tempxelrow[col][2] * alpha; |
| 228 | |
| 229 | fraccolleft = sxscale; |
| 230 | while (fraccolleft >= fraccoltofill) { |
| 231 | if (needcol) { |
| 232 | dstcol++; |
| 233 | r = g = b = a = HALFSCALE; |
| 234 | } |
| 235 | |
| 236 | a += fraccoltofill * alpha; |
| 237 | |
| 238 | if (a == 0) { |
| 239 | r = g = b = a; |
| 240 | } else { |
| 241 | r += fraccoltofill * tempxel_blended_r; |
| 242 | r = std::min(r / a, maxval); |
| 243 | |
| 244 | g += fraccoltofill * tempxel_blended_g; |
| 245 | g = std::min(g / a, maxval); |
| 246 | |
| 247 | b += fraccoltofill * tempxel_blended_b; |
| 248 | b = std::min(b / a, maxval); |
| 249 | |
| 250 | a = std::min(a / SCALE, maxval); |
| 251 | } |
| 252 | |
| 253 | rgba[row][dstcol] = Pixel<4>(r, g, b, a); |
| 254 | |
| 255 | fraccolleft -= fraccoltofill; |
| 256 | fraccoltofill = SCALE; |
| 257 | needcol = true; |
| 258 | } |
| 259 | |
| 260 | if (fraccolleft > 0) { |
| 261 | if (needcol) { |
| 262 | dstcol++; |
| 263 | r = g = b = a = HALFSCALE; |
| 264 | needcol = false; |
| 265 | } |
| 266 | |
| 267 | r += fraccolleft * tempxel_blended_r; |
| 268 | g += fraccolleft * tempxel_blended_g; |
| 269 | b += fraccolleft * tempxel_blended_b; |
| 270 | a += fraccolleft * alpha; |
| 271 | |
| 272 | fraccoltofill -= fraccolleft; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | if (fraccoltofill > 0) { |
| 277 | r += fraccoltofill * tempxelrow[cols - 1][0] * tempxelrow[cols - 1][3]; |
| 278 | g += fraccoltofill * tempxelrow[cols - 1][1] * tempxelrow[cols - 1][3]; |
| 279 | b += fraccoltofill * tempxelrow[cols - 1][2] * tempxelrow[cols - 1][3]; |
| 280 | a += fraccoltofill * tempxelrow[cols - 1][3]; |
| 281 | } |
| 282 | |
| 283 | if (!needcol) { |
| 284 | if (a == 0) { |
| 285 | r = g = b = a; |
| 286 | } else { |
| 287 | r = std::min(r / a, maxval); |
| 288 | g = std::min(g / a, maxval); |
| 289 | b = std::min(b / a, maxval); |
| 290 | a = std::min(a / SCALE, maxval); |
| 291 | } |
| 292 | |
| 293 | rgba[row][dstcol] = Pixel<4>(r, g, b, a); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | dst = std::make_shared<picture_t>(src->id, rgba, true); |
| 299 | |
| 300 | gli_picture_store(dst); |
| 301 | |
| 302 | return dst; |
| 303 | } |