|
| 1 | +/* |
| 2 | +
|
| 3 | +MIT License |
| 4 | +
|
| 5 | +Copyright (c) 2026 Mika Tuupola |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in all |
| 15 | +copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +SOFTWARE. |
| 24 | +
|
| 25 | +-cut- |
| 26 | +Helper for saving hagl_bitmap_t framebuffers as PNG files via |
| 27 | +stb_image_write. Used by the test suite for visual debugging. |
| 28 | +
|
| 29 | +This file is part of the HAGL graphics library: |
| 30 | +https://github.com/tuupola/hagl |
| 31 | +
|
| 32 | +SPDX-License-Identifier: MIT |
| 33 | +
|
| 34 | +*/ |
| 35 | + |
| 36 | +#ifndef SAVE_IMAGE_H |
| 37 | +#define SAVE_IMAGE_H |
| 38 | + |
| 39 | +#include <stdint.h> |
| 40 | +#include <string.h> |
| 41 | +#include <sys/stat.h> |
| 42 | + |
| 43 | +#include "hagl/bitmap.h" |
| 44 | +#include "stb_image_write.h" |
| 45 | + |
| 46 | +/* |
| 47 | + * Convert an RGB565 framebuffer to RGB888 and write it as a PNG file. |
| 48 | + * Creates the parent directory if it does not exist. |
| 49 | + */ |
| 50 | +static void save_image(const hagl_bitmap_t *bitmap, const char *filename) { |
| 51 | + uint32_t pixel_count = bitmap->width * bitmap->height; |
| 52 | + uint8_t rgb[pixel_count * 3]; |
| 53 | + uint16_t *src = (uint16_t *)bitmap->buffer; |
| 54 | + |
| 55 | + for (uint32_t i = 0; i < pixel_count; i++) { |
| 56 | + uint16_t pixel = src[i]; |
| 57 | + uint8_t r5 = (pixel >> 11) & 0x1F; |
| 58 | + uint8_t g6 = (pixel >> 5) & 0x3F; |
| 59 | + uint8_t b5 = pixel & 0x1F; |
| 60 | + |
| 61 | + rgb[i * 3 + 0] = (r5 * 527 + 23) >> 6; |
| 62 | + rgb[i * 3 + 1] = (g6 * 259 + 33) >> 6; |
| 63 | + rgb[i * 3 + 2] = (b5 * 527 + 23) >> 6; |
| 64 | + } |
| 65 | + |
| 66 | + /* Extract directory from filename and create it if needed. */ |
| 67 | + char dir[256]; |
| 68 | + const char *last_slash = NULL; |
| 69 | + for (const char *p = filename; *p; p++) { |
| 70 | + if (*p == '/') { |
| 71 | + last_slash = p; |
| 72 | + } |
| 73 | + } |
| 74 | + if (last_slash) { |
| 75 | + size_t len = last_slash - filename; |
| 76 | + if (len < sizeof(dir)) { |
| 77 | + memcpy(dir, filename, len); |
| 78 | + dir[len] = '\0'; |
| 79 | + mkdir(dir, 0755); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + stbi_write_png(filename, bitmap->width, bitmap->height, 3, rgb, bitmap->width * 3); |
| 84 | +} |
| 85 | + |
| 86 | +#endif /* SAVE_IMAGE_H */ |
0 commit comments