rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

image.h (2047B)


      1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr)
      2  *
      3  * The RSys library is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published
      5  * by the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * The RSys library 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 the RSys library. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #ifndef IMAGE_H
     17 #define IMAGE_H
     18 
     19 #include "rsys.h"
     20 
     21 struct mem_allocator;
     22 
     23 enum image_format {
     24   IMAGE_RGB8,
     25   IMAGE_RGB16
     26 };
     27 
     28 struct image {
     29   size_t width;
     30   size_t height;
     31   size_t pitch;
     32   enum image_format format;
     33   char* pixels;
     34 
     35   /* Internal data */
     36   struct mem_allocator* allocator;
     37 };
     38 
     39 static FINLINE size_t
     40 sizeof_image_format(const enum image_format fmt)
     41 {
     42   switch(fmt) {
     43     case IMAGE_RGB8: return sizeof(uint8_t[3]);
     44     case IMAGE_RGB16: return sizeof(uint16_t[3]);
     45     default: FATAL("Unreachable code.\n"); break;
     46   }
     47 }
     48 
     49 BEGIN_DECLS
     50 
     51 RSYS_API res_T
     52 image_init
     53   (struct mem_allocator* allocator, /* May be NULL */
     54    struct image* img);
     55 
     56 RSYS_API res_T
     57 image_release
     58   (struct image* img);
     59 
     60 RSYS_API res_T
     61 image_setup
     62   (struct image* image,
     63    const size_t width,
     64    const size_t height,
     65    const size_t pitch,
     66    const enum image_format format,
     67    const char* pixels); /* May be NULL */
     68 
     69 RSYS_API res_T
     70 image_read_ppm
     71   (struct image* image,
     72    const char* filename);
     73 
     74 RSYS_API res_T
     75 image_read_ppm_stream
     76   (struct image* image,
     77    FILE* stream);
     78 
     79 RSYS_API res_T
     80 image_write_ppm
     81   (const struct image* image,
     82    const int binary,
     83    const char* filename);
     84 
     85 RSYS_API res_T
     86 image_write_ppm_stream
     87   (const struct image* image,
     88    const int binary,
     89    FILE* stream);
     90 
     91 END_DECLS
     92 
     93 #endif /* IMAGE_H */