diff options
| author | Clement Sibille <clements+github@lisible.xyz> | 2024-06-10 12:52:15 +0900 | 
|---|---|---|
| committer | Clement Sibille <clements+github@lisible.xyz> | 2024-06-10 12:52:15 +0900 | 
| commit | 8552afa1a13e7eef4a0ead8736a3e732731aac6d (patch) | |
| tree | af097e52226085e0d5b17e16ce505e3d0a146f7c | |
| parent | ff11942fb7abfc68e2f04350f7d0a2dde51b1d9e (diff) | |
Revert "Make LisPng_decode use a u8* instead of a FILE*"
This reverts commit ff11942fb7abfc68e2f04350f7d0a2dde51b1d9e.
| -rw-r--r-- | cross/playdate.txt | 3 | ||||
| -rw-r--r-- | lisiblepng-bin/src/main.c | 24 | ||||
| -rw-r--r-- | lisiblepng/src/lisiblepng.c | 45 | ||||
| -rw-r--r-- | lisiblepng/src/lisiblepng.h | 8 | 
4 files changed, 31 insertions, 49 deletions
diff --git a/cross/playdate.txt b/cross/playdate.txt index c843775..c54b4ba 100644 --- a/cross/playdate.txt +++ b/cross/playdate.txt @@ -9,6 +9,3 @@ system = 'none'  cpu_family = 'arm'  cpu = 'cortex-m7'  endian = 'little' - -[properties] -is_playdate_build = true diff --git a/lisiblepng-bin/src/main.c b/lisiblepng-bin/src/main.c index 116125a..47e1722 100644 --- a/lisiblepng-bin/src/main.c +++ b/lisiblepng-bin/src/main.c @@ -1,12 +1,8 @@  #include <errno.h> -#include <fcntl.h>  #include <lisiblepng.h>  #include <lisiblestd/log.h>  #include <stdio.h> -#include <stdlib.h>  #include <string.h> -#include <sys/mman.h> -#include <sys/stat.h>  #define LOG0(msg) fprintf(stderr, msg "\n")  #define LOGN(fmt, ...) fprintf(stderr, fmt "\n", __VA_ARGS__) @@ -19,19 +15,19 @@ int main(int argc, char **argv) {    }    const char *png_filepath = argv[1]; -  int fd = open(png_filepath, O_RDONLY); -  struct stat st; -  fstat(fd, &st); -  u8 *png_file_data = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); -  LOGN("File size: %ld bytes", st.st_size); +  FILE *png_file = fopen(png_filepath, "r"); +  fseek(png_file, 0, SEEK_END); +  long file_size = ftell(png_file); +  LOGN("File size: %ld bytes", file_size); +  fseek(png_file, 0, SEEK_SET); -  if (!png_file_data) { +  if (!png_file) {      const char *error_message = strerror(errno); -    LOGN("Couldn't map PNG file: %s", error_message); +    LOGN("Couldn't open PNG file: %s", error_message);      return 1;    } -  LisPng *png = LisPng_decode(png_file_data, st.st_size); +  LisPng *png = LisPng_decode(png_file);    if (!png) {      LOG0("Couldn't decode PNG");      return 1; @@ -40,9 +36,9 @@ int main(int argc, char **argv) {    LisPng_dump_ppm(png);    LisPng_destroy(png); -  if (munmap(png_file_data, st.st_size) != 0) { +  if (fclose(png_file) != 0) {      const char *error_message = strerror(errno); -    LOGN("Couldn't unmap PNG file: %s", error_message); +    LOGN("Couldn't close PNG file: %s", error_message);      return 1;    } diff --git a/lisiblepng/src/lisiblepng.c b/lisiblepng/src/lisiblepng.c index ead4107..de6f06e 100644 --- a/lisiblepng/src/lisiblepng.c +++ b/lisiblepng/src/lisiblepng.c @@ -1,8 +1,12 @@  #include "lisiblepng.h"  #include "lisiblepng/deflate.h" +#include <errno.h>  #include <lisiblestd/assert.h>  #include <lisiblestd/log.h> -#include <lisiblestd/types.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h>  #include <string.h>  #define PNG_SIGNATURE_LENGTH 8 @@ -116,21 +120,16 @@ uint8_t LisPngColourType_sample_count(const LisPngColourType colour_type) {    }  }  typedef struct { -  const u8 *image_data; -  size_t image_data_size; -  size_t current_offset; +  FILE *stream;  #ifdef LPNG_COMPUTE_CRC    uint32_t computed_crc;  #endif // LPNG_COMPUTE_CRC  } DeflateDecompressor; -void DeflateDecompressor_init(DeflateDecompressor *ctx, const u8 *image_data, -                              usize image_data_size) { +void DeflateDecompressor_init(DeflateDecompressor *ctx, FILE *stream) {    LSTD_ASSERT(ctx != NULL); -  LSTD_ASSERT(image_data != NULL); -  ctx->current_offset = 0; -  ctx->image_data = image_data; -  ctx->image_data_size = image_data_size; +  LSTD_ASSERT(stream != NULL); +  ctx->stream = stream;  #ifdef LPNG_COMPUTE_CRC    ctx->computed_crc = 0xFFFFFFFFu;  #endif // LPNG_COMPUTE_CRC @@ -152,19 +151,13 @@ uint32_t ParsingContext_computed_crc(DeflateDecompressor *ctx) {  long ParsingContext_cursor_position(DeflateDecompressor *ctx) {    LSTD_ASSERT(ctx != NULL); -  return ctx->current_offset; +  return ftell(ctx->stream);  }  bool ParsingContext_skip_bytes(DeflateDecompressor *ctx, size_t byte_count) {    LSTD_ASSERT(ctx != NULL); -  const size_t previous_offset = ctx->current_offset; -  ctx->current_offset += byte_count; -  if (ctx->current_offset > ctx->image_data_size) { -    LOG_ERROR("current offset > image data size"); -    return false; -  } -  if (ctx->current_offset < previous_offset) { -    LOG_ERROR("current offset overflowed"); +  if (fseek(ctx->stream, byte_count, SEEK_CUR) != 0) { +    LOG_ERROR("Couldn't skip bytes: %s", strerror(errno));      return false;    } @@ -175,13 +168,9 @@ bool ParsingContext_parse_bytes(DeflateDecompressor *ctx, size_t byte_count,                                  uint8_t *output_buffer) {    LSTD_ASSERT(ctx != NULL);    LSTD_ASSERT(output_buffer != NULL); - -  for (size_t i = 0; i < byte_count; i++) { -    output_buffer[i] = ctx->image_data[ctx->current_offset++]; -    if (ctx->current_offset > ctx->image_data_size) { -      LOG_ERROR("Couldn't parse bytes, EOF reached"); -      return false; -    } +  if (fread(output_buffer, 1, byte_count, ctx->stream) < byte_count) { +    LOG_ERROR("Couldn't parse bytes, EOF reached"); +    return false;    }  #ifdef LPNG_COMPUTE_CRC @@ -488,11 +477,11 @@ void apply_reconstruction_functions(LisPng *image,    }  } -LisPng *LisPng_decode(const u8 *image_bytes, usize image_bytes_length) { +LisPng *LisPng_decode(FILE *stream) {    LisPng *png = malloc(sizeof(LisPng));    DeflateDecompressor ctx; -  DeflateDecompressor_init(&ctx, image_bytes, image_bytes_length); +  DeflateDecompressor_init(&ctx, stream);    uint8_t parsed_png_signature[PNG_SIGNATURE_LENGTH];    if (!ParsingContext_parse_bytes(&ctx, PNG_SIGNATURE_LENGTH,                                    parsed_png_signature)) { diff --git a/lisiblepng/src/lisiblepng.h b/lisiblepng/src/lisiblepng.h index 5d4bb44..e7f94ec 100644 --- a/lisiblepng/src/lisiblepng.h +++ b/lisiblepng/src/lisiblepng.h @@ -1,7 +1,8 @@  #ifndef LISIBLE_PNG_H  #define LISIBLE_PNG_H -#include <lisiblestd/types.h> +#include <stdint.h> +#include <stdio.h>  typedef enum {    LisPngColourType_Greyscale = 0, @@ -18,12 +19,11 @@ typedef struct LisPng LisPng;  /// Parses the provided PNG stream  /// -/// @param image_bytes The image data -/// @param image_bytes_length The size of the image data +/// @param stream The PNG stream  /// @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. -LisPng *LisPng_decode(const u8 *image_bytes, usize image_bytes_length); +LisPng *LisPng_decode(FILE *stream);  /// Writes the PNG image data as RGBA8 data to a buffer  ///  | 
