CPCWiki forum

General Category => Programming => Topic started by: Typhon on 20:40, 06 May 24

Title: CPCTelera 1.5 Decrunching Issue
Post by: Typhon on 20:40, 06 May 24
Allo all,
So I've got a small CPCTelera 1.5 (development branch) uncompression issue. 
I have successfully generated a compressed and packed png (160x200 for Mode 0 screen) ->bin (via image_conversion.mk):

PALETTE=0 2 3 4 6 9 11 13 14 15 17 18 20 24 25 26
$(eval $(call IMG2SP, SET_MODE        , 0));
$(eval $(call IMG2SP, SET_PALETTE_FW  , $(PALETTE)))
$(eval $(call IMG2SP, SET_OUTPUT      , bin))
$(eval $(call IMG2SP, SET_IMG_FORMAT  , screen))
$(eval $(call IMG2SP, SET_FOLDER      , tmp/))
$(eval $(call IMG2SP, CONVERT         , img/transition.png, 160, 200, transition, palette, tileset))
$(eval $(call IMG2SP, CONVERT_PALETTE , $(PALETTE), t_palette))


Which is then converted again successfully via compression.mk into three source files:

$(eval $(call ADD2PACK,transition,tmp/transition.bin))
$(eval $(call PACKZX7B,transition,src/))


With amongst other things, 3 output files, transition.h, transition.s, and transition.h.s. I include these in my code successfully:

// File 'src/transition.h' generated using cpct_pack
// Compresor used:  zx7b
// Files compressed: [ 'tmp/transition.bin' ]
// Uncompressed:    16400 bytes
// Compressed:      630 bytes
// Space saved:      15770 bytes
//
#ifndef transition_630_H
#define transition_630_H
// Declaration of the compressed array
extern const unsigned char transition[630];
// Address of the latest byte of the compressed array (for unpacking purposes)
#define transition_end      (transition + 630 - 1)
// Compressed and uncompressed sizes
#define transition_size_z  630
#define transition_size    16400
#endif

However, when I try to decompress and display onto the screen:

cpct_zx7b_decrunch_s(VIDEO_MEM_END, transition_end);

It *does* display (see attached screenshot), however, the decrunching routine doesn't seem to stop and overwrites the rest of my program with unpredictable results (crashes mainly).

Screenshot_2024-05-06_20-22-01.png

Note that (as mentioned on a previous thread on the forum.) #define VIDEO_MEM_END ((void*)0xffff)


if I don't decrunch, all is good, but the image doesn't display.

What gives?

Title: Re: CPCTelera 1.5 Decrunching Issue
Post by: ervin on 00:41, 07 May 24
This is an interesting problem.
I notice that the uncompressed file size is 16400 bytes instead of 16384. I don't know if that is a problem, but it does seem odd.
Decompressing 16400 bytes from 0xFFFF down would cause 16 bytes of the stack to be overwritten (the 16 bytes below 0xC000).

The documentation for cpct_zx7b_decrunch_s says the following:

;;    * *source_end* should be a 16-bit pointer to the latest byte of the array where compressed
;; data is held. ZX7B algorithm will read the array from this byte backwards till its start.
;; No runtime checks are performed: if this value is incorrect, undefined behaviour will follow.
;; Typically, garbage data of undefined size will be produced, potentially overwriting undesired
;; memory parts.

A few years ago @Arnaud had a slightly different problem, and @ronaldo gave a very helpful response.
Not sure if that might help you.
https://www.cpcwiki.eu/forum/programming/cpctelera-1-5-0-file-compression-problem/
Title: Re: CPCTelera 1.5 Decrunching Issue
Post by: Arnaud on 06:33, 07 May 24
Hi,
i think @ervin is right, the decompression is overwriting the stack.

To solve your problem try to set the size of your image to 0, 0 (image size by default) and no args after image:
$(eval $(call IMG2SP, CONVERT        , img/transition.png, 0, 0, transition, , ))

I have done this some weeks ago and the size file was OK.

Title: Re: CPCTelera 1.5 Decrunching Issue
Post by: ervin on 06:38, 07 May 24
This discussion may also help.
CPCtelera - png to screen (cpcwiki.eu) (https://www.cpcwiki.eu/forum/programming/cpctelera-png-to-screen/)
Title: Re: CPCTelera 1.5 Decrunching Issue
Post by: Typhon on 14:35, 07 May 24
Quote from: Arnaud on 06:33, 07 May 24Hi,
i think @ervin is right, the decompression is overwriting the stack.

To solve your problem try to set the size of your image to 0, 0 (image size by default) and no args after image:
$(eval $(call IMG2SP, CONVERT        , img/transition.png, 0, 0, transition, , ))

I have done this some weeks ago and the size file was OK.



Thank you, I'll try that later. I do notice that the video tuturial I was using from Prof.Retroman also had the same issue, though he wasn't doing anything afterwards in his code, so I don't think he realised he had the same issue.
Title: Re: CPCTelera 1.5 Decrunching Issue
Post by: Typhon on 18:29, 08 May 24
Quote from: Arnaud on 06:33, 07 May 24Hi,
i think @ervin is right, the decompression is overwriting the stack.

To solve your problem try to set the size of your image to 0, 0 (image size by default) and no args after image:
$(eval $(call IMG2SP, CONVERT        , img/transition.png, 0, 0, transition, , ))

I have done this some weeks ago and the size file was OK.


I can confirm that works! Thank you!

// File 'src/transition.h' generated using cpct_pack
// Compresor used:  zx7b
// Files compressed: [ 'tmp/transition.bin' ]
// Uncompressed:    16384 bytes
// Compressed:      612 bytes
// Space saved:      15772 bytes
//
#ifndef transition_612_H
#define transition_612_H
// Declaration of the compressed array
extern const unsigned char transition[612];
// Address of the latest byte of the compressed array (for unpacking purposes)
#define transition_end (transition + 612 - 1)
// Compressed and uncompressed sizes
#define transition_size_z 612
#define transition_size 16384
#endif
Powered by SMFPacks Menu Editor Mod