|
| 1 | +// This file is part of SmallBASIC |
| 2 | +// |
| 3 | +// Image handling |
| 4 | +// |
| 5 | +// This program is distributed under the terms of the GPL v2.0 or later |
| 6 | +// Download the GNU Public License (GPL) from www.gnu.org |
| 7 | +// |
| 8 | +// Copyright(C) 2002-2025 Chris Warren-Smith. |
| 9 | + |
| 10 | +#include "image_codec.h" |
| 11 | +#include "lib/lodepng/lodepng.h" |
| 12 | +#include <cstdlib> |
| 13 | +#include <cstring> |
| 14 | + |
| 15 | +static char g_last_error[256] = {0}; |
| 16 | + |
| 17 | +static float lerp(float a, float b, float t) { |
| 18 | + return a + t * (b - a); |
| 19 | +} |
| 20 | + |
| 21 | +ImageCodec::ImageCodec(const uint8_t *data, size_t size) : |
| 22 | + _width(0), |
| 23 | + _height(0), |
| 24 | + _pixels(nullptr) { |
| 25 | + auto error = lodepng_decode32(&_pixels, &_width, &_height, data, size); |
| 26 | + if (error) { |
| 27 | + snprintf(g_last_error, sizeof(g_last_error), "%s", lodepng_error_text(error)); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +ImageCodec::~ImageCodec() { |
| 32 | + free(_pixels); |
| 33 | + _pixels = nullptr; |
| 34 | +} |
| 35 | + |
| 36 | +const char *ImageCodec::getLastError(void) { |
| 37 | + return g_last_error; |
| 38 | +} |
| 39 | + |
| 40 | +bool ImageCodec::encode(uint8_t **data, size_t *size) const { |
| 41 | + bool result; |
| 42 | + if (!data || !size) { |
| 43 | + sprintf(g_last_error, "Invalid args"); |
| 44 | + result = false; |
| 45 | + } else { |
| 46 | + uint8_t *img = nullptr; |
| 47 | + size_t img_size = 0; |
| 48 | + auto error = lodepng_encode32(&img, &img_size, _pixels, _width, _height); |
| 49 | + if (error) { |
| 50 | + snprintf(g_last_error, sizeof(g_last_error), "%s", lodepng_error_text(error)); |
| 51 | + } else { |
| 52 | + *data = img; |
| 53 | + *size = img_size; |
| 54 | + } |
| 55 | + result = !error; |
| 56 | + } |
| 57 | + return result; |
| 58 | +} |
| 59 | + |
| 60 | +void ImageCodec::resize(unsigned width, unsigned height) { |
| 61 | + uint8_t *pixels = new uint8_t[width * height * 4]; |
| 62 | + |
| 63 | + for (int y = 0; y < height; y++) { |
| 64 | + float gy = ((float)y + 0.5f) * _height / height - 0.5f; |
| 65 | + int y0 = (int)gy, y1 = y0 + 1; |
| 66 | + float fy = gy - y0; |
| 67 | + y0 = y0 < 0 ? 0 : (y0 >= _height ? _height - 1 : y0); |
| 68 | + y1 = y1 < 0 ? 0 : (y1 >= _height ? _height - 1 : y1); |
| 69 | + |
| 70 | + for (int x = 0; x < width; x++) { |
| 71 | + float gx = ((float)x + 0.5f) * _width / width - 0.5f; |
| 72 | + int x0 = (int)gx, x1 = x0 + 1; |
| 73 | + float fx = gx - x0; |
| 74 | + x0 = x0 < 0 ? 0 : (x0 >= _width ? _width - 1 : x0); |
| 75 | + x1 = x1 < 0 ? 0 : (x1 >= _width ? _width - 1 : x1); |
| 76 | + |
| 77 | + int offset = 4 * (y * width + x); |
| 78 | + for (int c = 0; c < 4; c++) { |
| 79 | + // A, R, G, B |
| 80 | + float tl = _pixels[4 * (y0 * _width + x0) + c]; |
| 81 | + float tr = _pixels[4 * (y0 * _width + x1) + c]; |
| 82 | + float bl = _pixels[4 * (y1 * _width + x0) + c]; |
| 83 | + float br = _pixels[4 * (y1 * _width + x1) + c]; |
| 84 | + |
| 85 | + float top = lerp(tl, tr, fx); |
| 86 | + float bottom = lerp(bl, br, fx); |
| 87 | + float value = lerp(top, bottom, fy); |
| 88 | + |
| 89 | + pixels[offset + c] = (uint8_t)(value + 0.5f); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + free(_pixels); |
| 95 | + _pixels = pixels; |
| 96 | + _width = width; |
| 97 | + _height = height; |
| 98 | +} |
| 99 | + |
0 commit comments