test_scmap_palettes.c (2645B)
1 /* Copyright (C) 2020, 2021, 2023 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #define MAP_WIDTH 480 17 #define MAP_HEIGHT 32 18 19 #include "scmap.h" 20 21 #include <rsys/image.h> 22 #include <rsys/mem_allocator.h> 23 #include <rsys/str.h> 24 #include <rsys/stretchy_array.h> 25 26 int 27 main(int argc, char** argv) 28 { 29 struct str filename; 30 FILE* fp = NULL; 31 struct scmap* scmap = NULL; 32 const struct scmap_palette* palette = NULL; 33 double* colors = NULL; 34 struct image img; 35 size_t x, y; 36 size_t i; 37 (void)argc, (void)argv; 38 39 if(argc < 2) { 40 fprintf(stderr, "Usage: %s PALETTE\n", argv[0]); 41 return 1; 42 } 43 44 CHK(scmap_get_builtin_palette("bad_name") == NULL); 45 CHK((palette = scmap_get_builtin_palette(argv[1])) != NULL); 46 CHK(scmap_create(NULL, NULL, 1, palette, &scmap) == RES_OK); 47 CHK((colors = sa_add(colors, MAP_WIDTH*3)) != NULL); 48 49 FOR_EACH(i, 0, MAP_WIDTH) { 50 const double u = (double)i / (double)(MAP_WIDTH-1); 51 CHK(scmap_fetch_color(scmap, u, SCMAP_FILTER_LINEAR, colors+i*3) == RES_OK); 52 } 53 54 CHK(image_init(&mem_default_allocator, &img) == RES_OK); 55 image_setup(&img, MAP_WIDTH, MAP_HEIGHT, 56 sizeof_image_format(IMAGE_RGB8)*MAP_WIDTH, IMAGE_RGB8, NULL); 57 58 FOR_EACH(y, 0, MAP_HEIGHT) { 59 char* row = img.pixels + img.pitch * y; 60 FOR_EACH(x, 0, MAP_WIDTH) { 61 uint8_t* pix = (uint8_t*)(row + x*sizeof_image_format(img.format)); 62 pix[0] = (uint8_t)(colors[x*3+0] * 255 + 0.5/*round*/); 63 pix[1] = (uint8_t)(colors[x*3+1] * 255 + 0.5/*round*/); 64 pix[2] = (uint8_t)(colors[x*3+2] * 255 + 0.5/*round*/); 65 } 66 } 67 68 str_init(&mem_default_allocator, &filename); 69 CHK(str_set(&filename, argv[1]) == RES_OK); 70 CHK(str_append(&filename, ".ppm") == RES_OK); 71 CHK((fp = fopen(str_cget(&filename), "w")) != NULL); 72 CHK(image_write_ppm_stream(&img, 0, fp) == RES_OK); 73 CHK(fclose(fp) == 0); 74 str_release(&filename); 75 76 sa_release(colors); 77 CHK(image_release(&img) == RES_OK); 78 CHK(scmap_ref_put(scmap) == RES_OK); 79 CHK(mem_allocated_size() == 0); 80 return 0; 81 }