diff options
| author | Clement Sibille <clements+git@lisible.xyz> | 2024-03-05 20:43:33 +0900 | 
|---|---|---|
| committer | Clement Sibille <clements+git@lisible.xyz> | 2024-03-05 20:43:33 +0900 | 
| commit | 1769d0007b79ce5a054a22d39409dab22f2889b2 (patch) | |
| tree | 528810198e69d0bf132a591b3d51bd078db8739d | |
| parent | 7d5613f1e16430fcfc8ddb843be9435e10879bc0 (diff) | |
Add accessor methods for PNG data
| -rw-r--r-- | CHANGELOG.md | 7 | ||||
| -rw-r--r-- | lisiblepng-bin/src/main.c | 6 | ||||
| -rw-r--r-- | lisiblepng/src/lisiblepng.c | 77 | ||||
| -rw-r--r-- | lisiblepng/src/lisiblepng.h | 26 | ||||
| -rw-r--r-- | meson.build | 2 | 
5 files changed, 76 insertions, 42 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d762fde..844d462 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,11 @@  # Changelog +## 0.2.0 (2024-03-05) +- Rename Png type to LisPng +- Replaced function prefix lis_Png_ to LisPng_ +- Expose LisPngColourType enum +- Add LisPngColourType_sample_count() function +- Add LisPng accessor functions +  ## 0.1.0 (2024-03-05)  - Add PNG decoding support for colour types Greyscale and Truecolour diff --git a/lisiblepng-bin/src/main.c b/lisiblepng-bin/src/main.c index 73aa972..7e99bfe 100644 --- a/lisiblepng-bin/src/main.c +++ b/lisiblepng-bin/src/main.c @@ -20,9 +20,9 @@ int main(int argc, char **argv) {      goto err;    } -  Png *png = lis_Png_decode(png_file); -  lis_Png_dump_ppm(png); -  lis_Png_destroy(png); +  LisPng *png = LisPng_decode(png_file); +  LisPng_dump_ppm(png); +  LisPng_destroy(png);    if (fclose(png_file) != 0) {      const char *error_message = strerror(errno); diff --git a/lisiblepng/src/lisiblepng.c b/lisiblepng/src/lisiblepng.c index 92edc3d..538ad16 100644 --- a/lisiblepng/src/lisiblepng.c +++ b/lisiblepng/src/lisiblepng.c @@ -61,41 +61,52 @@ const uint32_t CRC32_TABLE[256] = {      0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,      0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D}; -typedef enum { -  ColourType_Greyscale = 0, -  ColourType_Truecolour = 2, -  ColourType_IndexedColour = 3, -  ColourType_GreyscaleWithAlpha = 4, -  ColourType_TruecolourWithAlpha = 6, -  ColourType_Unknown -} ColourType; - -uint8_t ColourType_sample_count(const ColourType colour_type) { +struct LisPng { +  uint8_t *data; +  size_t width; +  size_t height; +  LisPngColourType colour_type; +  uint8_t bits_per_sample; +}; + +LisPngColourType LisPng_colour_type(const LisPng *png) { +  ASSERT(png != NULL); +  return png->colour_type; +} +uint8_t LisPng_bits_per_sample(const LisPng *png) { +  ASSERT(png != NULL); +  return png->bits_per_sample; +} +uint8_t *LisPng_data_ptr(const LisPng *png) { +  ASSERT(png != NULL); +  return png->data; +} +uint32_t LisPng_width(const LisPng *png) { +  ASSERT(png != NULL); +  return png->width; +} +uint32_t LisPng_height(const LisPng *png) { +  ASSERT(png != NULL); +  return png->height; +} + +uint8_t LisPngColourType_sample_count(const LisPngColourType colour_type) {    switch (colour_type) { -  case ColourType_Greyscale: +  case LisPngColourType_Greyscale:      return 1; -  case ColourType_Truecolour: +  case LisPngColourType_Truecolour:      return 3; -  case ColourType_IndexedColour: +  case LisPngColourType_IndexedColour:      return 1; -  case ColourType_GreyscaleWithAlpha: +  case LisPngColourType_GreyscaleWithAlpha:      return 2; -  case ColourType_TruecolourWithAlpha: +  case LisPngColourType_TruecolourWithAlpha:      return 4;    default:      LPNG_LOG_ERR0("Unknown colour type");      abort();    }  } - -struct Png { -  uint8_t *data; -  size_t width; -  size_t height; -  ColourType colour_type; -  uint8_t bits_per_sample; -}; -  typedef struct {    FILE *stream;  #ifdef LISIBLE_PNG_COMPUTE_CRC @@ -390,11 +401,11 @@ void apply_reconstruction_functions_to_scanline(    }  } -void apply_reconstruction_functions(Png *image, +void apply_reconstruction_functions(LisPng *image,                                      const uint8_t *decompressed_data_buffer) {    ASSERT(image != NULL);    ASSERT(decompressed_data_buffer != NULL); -  size_t sample_count = ColourType_sample_count(image->colour_type); +  size_t sample_count = LisPngColourType_sample_count(image->colour_type);    size_t bits_per_pixel = (image->bits_per_sample * sample_count);    size_t bytes_per_pixel = bits_per_pixel / 8;    if (image->bits_per_sample < 8) { @@ -419,8 +430,8 @@ void apply_reconstruction_functions(Png *image,    }  } -Png *lis_Png_decode(FILE *stream) { -  Png *png = malloc(sizeof(Png)); +LisPng *LisPng_decode(FILE *stream) { +  LisPng *png = malloc(sizeof(LisPng));    DeflateDecompressor ctx;    DeflateDecompressor_init(&ctx, stream); @@ -511,16 +522,16 @@ err:  }  #undef PARSE_FIELD -void lis_Png_destroy(Png *png) { +void LisPng_destroy(LisPng *png) {    free(png->data);    free(png);  } -void lis_Png_dump_ppm(const Png *png) { +void LisPng_dump_ppm(const LisPng *png) {    ASSERT(png != NULL);    printf("P3\n");    printf("%zu %zu\n", png->width, png->height);    printf("%d\n", (1 << png->bits_per_sample) - 1); -  size_t sample_count = ColourType_sample_count(png->colour_type); +  size_t sample_count = LisPngColourType_sample_count(png->colour_type);    size_t bits_per_pixel = png->bits_per_sample * sample_count;    size_t bytes_per_pixel = bits_per_pixel / 8;    if (png->bits_per_sample < 8) { @@ -529,7 +540,7 @@ void lis_Png_dump_ppm(const Png *png) {    for (size_t pixel_index = 0; pixel_index < png->height * png->width;         pixel_index++) {      size_t pixel_base = pixel_index * bytes_per_pixel; -    if (png->colour_type == ColourType_Greyscale) { +    if (png->colour_type == LisPngColourType_Greyscale) {        if (bits_per_pixel == 16) {          uint16_t grey =              (png->data[pixel_base] << 8) | png->data[pixel_base + 1]; @@ -543,7 +554,7 @@ void lis_Png_dump_ppm(const Png *png) {                         ((1 << bits_per_pixel) - 1);          printf("%u %u %u\n", grey, grey, grey);        } -    } else if (png->colour_type == ColourType_Truecolour) { +    } else if (png->colour_type == LisPngColourType_Truecolour) {        if (png->bits_per_sample == 16) {          uint16_t r = (png->data[pixel_base] << 8) | png->data[pixel_base + 1];          uint16_t g = diff --git a/lisiblepng/src/lisiblepng.h b/lisiblepng/src/lisiblepng.h index 86a8364..a23ac91 100644 --- a/lisiblepng/src/lisiblepng.h +++ b/lisiblepng/src/lisiblepng.h @@ -1,10 +1,21 @@  #ifndef LISIBLE_PNG_H  #define LISIBLE_PNG_H +#include <stdint.h>  #include <stdio.h> -struct Png; -typedef struct Png Png; +typedef enum { +  LisPngColourType_Greyscale = 0, +  LisPngColourType_Truecolour = 2, +  LisPngColourType_IndexedColour = 3, +  LisPngColourType_GreyscaleWithAlpha = 4, +  LisPngColourType_TruecolourWithAlpha = 6, +  LisPngColourType_Unknown +} LisPngColourType; +uint8_t LisPngColourType_sample_count(const LisPngColourType colour_type); + +struct LisPng; +typedef struct LisPng LisPng;  /// Parses the provided PNG stream  /// @@ -12,12 +23,17 @@ typedef struct Png Png;  /// @returns The parsed PNG as a Png struct pointer or NULL if an error occured.  /// The returned PNG is owned by the caller and must be destroyed with  /// Png_destroy. -Png *lis_Png_decode(FILE *stream); +LisPng *LisPng_decode(FILE *stream);  /// Outputs the provided Png struct as a PPM image to stdout  ///  /// @param png The png -void lis_Png_dump_ppm(const Png *png); +void LisPng_dump_ppm(const LisPng *png); +LisPngColourType LisPng_colour_type(const LisPng *png); +uint8_t LisPng_bits_per_sample(const LisPng *png); +uint8_t *LisPng_data_ptr(const LisPng *png); +uint32_t LisPng_width(const LisPng *png); +uint32_t LisPng_height(const LisPng *png);  /// Destroys a Png instance -void lis_Png_destroy(Png *png); +void LisPng_destroy(LisPng *png);  #endif // LISIBLE_PNG_H diff --git a/meson.build b/meson.build index de866e5..3ce9711 100644 --- a/meson.build +++ b/meson.build @@ -1,4 +1,4 @@ -project('lisiblepng', 'c', default_options: ['c_std=c18', 'warning_level=3'], version:  '0.1.0') +project('lisiblepng', 'c', default_options: ['c_std=c18', 'warning_level=3'], version:  '0.2.0')  subdir('lisiblepng')  subdir('lisiblepng-bin')  | 
