Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding png chunk cartridge support #2045

Merged
merged 4 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions build/tools/xplode.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ s32 main(s32 argc, char** argv)
for(s32 i = 0; i < TIC80_WIDTH * TIC80_HEIGHT; i++)
((u32*)img.data)[i] = tic_rgba(&cart->bank0.palette.vbank0.colors[tic_tool_peek4(cart->bank0.screen.data, i)]);

png_buffer png = png_write(img);
png_buffer png = png_write(img, (png_buffer){NULL, 0});
writeFile("cover.png", (FileBuffer){png.size, png.data});
printf("cover.png successfully exported\n");

Expand Down Expand Up @@ -159,7 +159,7 @@ s32 main(s32 argc, char** argv)
((u32*)img.data)[x + y * TIC_SPRITESHEET_SIZE] = tic_rgba(&cart->bank0.palette.vbank0.colors[index]);
}

png_buffer png = png_write(img);
png_buffer png = png_write(img, (png_buffer){NULL, 0});
writeFile("tiles.png", (FileBuffer){png.size, png.data});
printf("tiles.png successfully exported\n");

Expand All @@ -180,7 +180,7 @@ s32 main(s32 argc, char** argv)
((u32*)img.data)[x + y * TIC_SPRITESHEET_SIZE] = tic_rgba(&cart->bank0.palette.vbank0.colors[index]);
}

png_buffer png = png_write(img);
png_buffer png = png_write(img, (png_buffer){NULL, 0});
writeFile("sprites.png", (FileBuffer){png.size, png.data});
printf("sprites.png successfully exported\n");

Expand All @@ -195,4 +195,4 @@ s32 main(s32 argc, char** argv)
else printf("usage: xplode <cart>\n");

return 0;
}
}
57 changes: 51 additions & 6 deletions src/ext/png.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// MIT License

// Copyright (c) 2021 Vadim Grigoruk @nesbox // grigoruk@gmail.com
// Copyright (c) 2022 bzt png chunk stuff

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -28,6 +29,7 @@
#include <png.h>
#include "tic_assert.h"

#define EXTRA_CHUNK "caRt"
#define RGBA_SIZE sizeof(u32)

png_buffer png_create(s32 size)
Expand All @@ -48,7 +50,7 @@ static void pngReadCallback(png_structp png, png_bytep out, png_size_t size)
stream->pos += size;
}

png_img png_read(png_buffer buf)
png_img png_read(png_buffer buf, png_buffer *cart)
{
png_img res = { 0 };

Expand Down Expand Up @@ -102,6 +104,28 @@ png_img png_read(png_buffer buf)

free(rows);

// Read in cartridge data from chunk if possible
if (cart)
{
png_unknown_chunkp unknowns = NULL;
int num_unknowns;

png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, EXTRA_CHUNK, 1);
png_read_end(png, info);
num_unknowns = png_get_unknown_chunks(png, info, &unknowns);
for(s32 i = 0; i < num_unknowns; i++)
if (!memcmp(unknowns[i].name, EXTRA_CHUNK, 5))
{
cart->size = unknowns[i].size;
cart->data = (u8*)malloc(cart->size);
if (cart->data)
memcpy(cart->data, unknowns[i].data, cart->size);
else
cart->size = 0;
break;
}
}

png_destroy_read_struct(&png, &info, NULL);
}

Expand All @@ -121,7 +145,7 @@ static void pngWriteCallback(png_structp png, png_bytep data, png_size_t size)

static void pngFlushCallback(png_structp png) {}

png_buffer png_write(png_img src)
png_buffer png_write(png_img src, png_buffer cart)
{
png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
png_infop info = png_create_info_struct(png);
Expand All @@ -141,14 +165,25 @@ png_buffer png_write(png_img src)
PNG_FILTER_TYPE_DEFAULT
);

// Save cartridge data in a chunk too. This supports bigger cartridges than steganography
if (cart.data && cart.size > 0 && cart.size <= 0x7fffff){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you store the cart data in the caRt chunk in addition to the steganographic data and it looks redundant, we are doubling the cart data in two places. I suggest letting the user decide how to save the .png by adding console command options like save cart.png [way=(steno | caRt | both)].
What do you think?

Copy link

@dulsi dulsi Dec 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(bztsrc is having issues logging into github and asked someone to forward this response.)

Here you store the cart data in the caRt chunk in addition to the steganographic data and it looks redundant, we are doubling the cart data in two places.

Only for now to provide backward compatibility.

I suggest letting the user decide

I think it would be much wiser to abandon steganography entirely instead. My reasons:
- steganography has very limited storage capacity (98k tops)
- it silently fails if the cartridge data happens to be bigger than that
- it is extremely vulnerable to data corruption
- has no means at all to detect data corruption

On the other hand, chunk based storage
- has unlimited storage capacity (up to 2^31)
- can't silently fail on save, it always works
- has a CRC to detect data corruption (handled by libpng automatically)

Copy link
Collaborator

@joshgoebel joshgoebel Dec 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I think we need to maintain backwards compatibility - to continue to READ old steganography but if you make changes and save that cartridge it should be encoded as PNG chunk metadata. Giving the user 3 different choices is kind of the worst of all approaches.

png_unknown_chunk unknowns = { 0 };
memcpy(&unknowns.name, EXTRA_CHUNK, 5);
unknowns.data = cart.data;
unknowns.size = cart.size;
unknowns.location = PNG_AFTER_IDAT;
png_set_unknown_chunks(png, info, &unknowns, 1);
png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, EXTRA_CHUNK, 1);
}

png_write_info(png, info);

png_bytep* rows = malloc(sizeof(png_bytep) * src.height);
for (s32 i = 0; i < src.height; i++)
rows[i] = src.data + src.width * i * RGBA_SIZE;

png_write_image(png, rows);
png_write_end(png, NULL);
png_write_end(png, info);

png_destroy_write_struct(&png, &info);

Expand Down Expand Up @@ -189,7 +224,7 @@ static inline s32 ceildiv(s32 a, s32 b)

png_buffer png_encode(png_buffer cover, png_buffer cart)
{
png_img png = png_read(cover);
png_img png = png_read(cover, NULL);

const s32 cartBits = cart.size * BITS_IN_BYTE;
const s32 coverSize = png.width * png.height * RGBA_SIZE - HEADER_SIZE;
Expand All @@ -206,7 +241,7 @@ png_buffer png_encode(png_buffer cover, png_buffer cart)
for (s32 i = end; i < coverSize; i++)
bitcpy(dst, i << 3, (const u8[]){rand()}, 0, header.bits);

png_buffer out = png_write(png);
png_buffer out = png_write(png, cart);

free(png.data);

Expand All @@ -215,8 +250,18 @@ png_buffer png_encode(png_buffer cover, png_buffer cart)

png_buffer png_decode(png_buffer cover)
{
png_img png = png_read(cover);
png_buffer cart = { 0 };
png_img png = png_read(cover, &cart);

// if we have a data from a png chunk, use that
if (cart.data && cart.size > 0)
{
if (png.data)
free(png.data);
return cart;
}

// otherwise fallback to steganography
if (png.data)
{
Header header;
Expand Down
4 changes: 2 additions & 2 deletions src/ext/png.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ typedef struct

png_buffer png_create(s32 size);

png_img png_read(png_buffer buf);
png_buffer png_write(png_img src);
png_img png_read(png_buffer buf, png_buffer *cart);
png_buffer png_write(png_img src, png_buffer cart);

png_buffer png_encode(png_buffer cover, png_buffer cart);
png_buffer png_decode(png_buffer cover);
14 changes: 7 additions & 7 deletions src/studio/screens/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ static void onImportTilesBase(Console* console, const char* name, const void* bu
png_buffer png = {(u8*)buffer, size};
bool error = true;

png_img img = png_read(png);
png_img img = png_read(png, NULL);

if(img.data) SCOPE(free(img.data))
{
Expand Down Expand Up @@ -1829,7 +1829,7 @@ static void onImport_screen(Console* console, const char* name, const void* buff
png_buffer png = {(u8*)buffer, size};
bool error = true;

png_img img = png_read(png);
png_img img = png_read(png, NULL);

if(img.data) SCOPE(free(img.data))
{
Expand Down Expand Up @@ -1948,7 +1948,7 @@ static void exportSprites(Console* console, const char* filename, tic_tile* base
for(s32 i = 0; i < TIC_SPRITESHEET_SIZE * TIC_SPRITESHEET_SIZE; i++)
img.values[i] = tic_rgba(&pal->colors[getSpritePixel(base, i % TIC_SPRITESHEET_SIZE, i / TIC_SPRITESHEET_SIZE)]);

png_buffer png = png_write(img);
png_buffer png = png_write(img, (png_buffer){NULL, 0});

SCOPE(free(png.data))
{
Expand Down Expand Up @@ -2258,7 +2258,7 @@ static void onExport_mapimg(Console* console, const char* param, const char* pat
}
}

png_buffer png = png_write(img);
png_buffer png = png_write(img, (png_buffer){NULL, 0});

SCOPE(free(png.data))
{
Expand Down Expand Up @@ -2306,7 +2306,7 @@ static void onExport_screen(Console* console, const char* param, const char* nam
for(s32 i = 0; i < TIC80_WIDTH * TIC80_HEIGHT; i++)
img.values[i] = tic_rgba(&pal->colors[tic_tool_peek4(bank->screen.data, i)]);

png_buffer png = png_write(img);
png_buffer png = png_write(img, (png_buffer){NULL, 0});

SCOPE(free(png.data))
{
Expand Down Expand Up @@ -2404,7 +2404,7 @@ static CartSaveResult saveCartName(Console* console, const char* name)
};

png_buffer template = {(u8*)Cartridge, sizeof Cartridge};
png_img img = png_read(template);
png_img img = png_read(template, NULL);

// draw screen
{
Expand Down Expand Up @@ -2452,7 +2452,7 @@ static CartSaveResult saveCartName(Console* console, const char* name)
ptr[CoverWidth * y + x] = tic_rgba(pal + tic_tool_peek4(screen, y * TIC80_WIDTH + x));
}

cover = png_write(img);
cover = png_write(img, (png_buffer){NULL, 0});

free(img.data);
}
Expand Down