Move zlib header skipping into Bitmap.c

This commit is contained in:
UnknownShadow200 2025-11-15 11:18:03 +11:00
parent b6b1e95d37
commit 6a57a07073
4 changed files with 34 additions and 43 deletions

View File

@ -63,6 +63,34 @@ void Bitmap_Scale(struct Bitmap* dst, struct Bitmap* src,
}
/*########################################################################################################################*
*------------------------------------------------------ZLib header--------------------------------------------------------*
*#########################################################################################################################*/
enum ZlibState { ZLIB_STATE_COMPRESSION_METHOD, ZLIB_STATE_FLAGS, ZLIB_STATE_DONE };
#define Header_ReadU8(value) if ((res = s->ReadU8(s, &value))) return res;
/* ZLib header is technically a separate spec, but it's simpler to just skip the whole header here */
static cc_result ZLibHeader_Read(struct Stream* s, int* state) {
cc_uint8 tmp;
cc_result res;
switch (*state) {
case ZLIB_STATE_COMPRESSION_METHOD:
Header_ReadU8(tmp);
if ((tmp & 0x0F) != 0x08) return ZLIB_ERR_METHOD;
/* Upper 4 bits are window size (ignored) */
(*state)++;
/* FALLTHRU */
case ZLIB_STATE_FLAGS:
Header_ReadU8(tmp);
if (tmp & 0x20) return ZLIB_ERR_FLAGS;
(*state)++;
}
return 0;
}
/*########################################################################################################################*
*------------------------------------------------------PNG common---------------------------------------------------------*
*#########################################################################################################################*/
@ -371,7 +399,7 @@ cc_result Png_Decode(struct Bitmap* bmp, struct Stream* stream) {
struct InflateState* inflate = &_inflate;
#endif
struct Stream compStream, datStream;
struct ZLibHeader zlibHeader;
int zlib_state = ZLIB_STATE_COMPRESSION_METHOD;
cc_uint8* data = NULL;
bmp->width = 0; bmp->height = 0;
@ -386,7 +414,6 @@ cc_result Png_Decode(struct Bitmap* bmp, struct Stream* stream) {
for (i = 0; i < PNG_PALETTE; i++) { palette[i] = BITMAPCOLOR_BLACK; }
Inflate_MakeStream2(&compStream, inflate, stream);
ZLibHeader_Init(&zlibHeader);
for (;;) {
res = Stream_Read(stream, tmp, 8);
@ -483,8 +510,8 @@ cc_result Png_Decode(struct Bitmap* bmp, struct Stream* stream) {
inflate->Source = &datStream;
/* TODO: This assumes zlib header will be in 1 IDAT chunk */
while (!zlibHeader.done) {
if ((res = ZLibHeader_Read(&datStream, &zlibHeader))) return res;
while (zlib_state != ZLIB_STATE_DONE) {
if ((res = ZLibHeader_Read(&datStream, &zlib_state))) return res;
}
if (!bmp->scan0) return PNG_ERR_NO_DATA;

View File

@ -106,38 +106,6 @@ cc_result GZipHeader_Read(struct Stream* s, struct GZipHeader* header) {
}
/*########################################################################################################################*
*-------------------------------------------------------ZLib header-------------------------------------------------------*
*#########################################################################################################################*/
enum ZlibState { ZLIB_STATE_COMPRESSIONMETHOD, ZLIB_STATE_FLAGS, ZLIB_STATE_DONE };
void ZLibHeader_Init(struct ZLibHeader* header) {
header->state = ZLIB_STATE_COMPRESSIONMETHOD;
header->done = false;
}
cc_result ZLibHeader_Read(struct Stream* s, struct ZLibHeader* header) {
cc_uint8 tmp;
cc_result res;
switch (header->state) {
case ZLIB_STATE_COMPRESSIONMETHOD:
Header_ReadU8(tmp);
if ((tmp & 0x0F) != 0x08) return ZLIB_ERR_METHOD;
/* Upper 4 bits are window size (ignored) */
header->state++;
/* FALLTHRU */
case ZLIB_STATE_FLAGS:
Header_ReadU8(tmp);
if (tmp & 0x20) return ZLIB_ERR_FLAGS;
header->state++;
header->done = true;
}
return 0;
}
/*########################################################################################################################*
*--------------------------------------------------Inflate (decompress)---------------------------------------------------*
*#########################################################################################################################*/

View File

@ -18,10 +18,6 @@ struct GZipHeader { cc_uint8 state; cc_bool done; cc_uint8 partsRead; int flags;
void GZipHeader_Init(struct GZipHeader* header);
cc_result GZipHeader_Read(struct Stream* s, struct GZipHeader* header);
struct ZLibHeader { cc_uint8 state; cc_bool done; };
void ZLibHeader_Init(struct ZLibHeader* header);
cc_result ZLibHeader_Read(struct Stream* s, struct ZLibHeader* header);
#define INFLATE_MAX_INPUT 8192
#define INFLATE_MAX_CODELENS 19

View File

@ -45,10 +45,10 @@ cc_uint8 Platform_Flags = PLAT_FLAG_SINGLE_PROCESS | PLAT_FLAG_APP_EXIT;
*#########################################################################################################################*/
#include "../main_impl.h"
int main(int argc, char** argv) {
SetupProgram(argc, argv);
int main(void) {
SetupProgram(0, NULL);
while (Window_Main.Exists) {
RunProgram(argc, argv);
RunProgram(0, NULL);
}
Window_Free();