GrafX2 2.9.3227
The ultimate 256-color painting program
c64formats.c File Reference

Formats for the Commodore 64. More...

#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include "engine.h"
#include "screen.h"
#include "windows.h"
#include "input.h"
#include "help.h"
#include "fileformats.h"
#include "loadsavefuncs.h"
#include "io.h"
#include "misc.h"
#include "oldies.h"
#include "c64load.h"
#include "keycodes.h"
#include "packbits.h"
#include "gfx2mem.h"
#include "gfx2log.h"
#include "c64picview_inc.h"
+ Include dependency graph for c64formats.c:

Macros

#define READU32LE(p)   ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
 

Enumerations

enum  c64_format {
  F_invalid = -1 , F_hires = 0 , F_multi = 1 , F_bitmap = 2 ,
  F_fli = 3
}
 C64 file formats. More...
 

Functions

static long C64_unpack_doodle (byte **file_buffer, long file_size)
 Unpack doodle/koala painter 2 data. More...
 
void Test_C64 (T_IO_Context *context, FILE *file)
 Test for a C64 picture file. More...
 
void Test_PRG (T_IO_Context *context, FILE *file)
 Test for a C64 auto-load machine language program which could be a picture. More...
 
static void Load_C64_hires (T_IO_Context *context, byte *bitmap, byte *screen_ram)
 Load C64 hires (320x200) More...
 
static void Load_C64_multi (T_IO_Context *context, byte *bitmap, byte *screen_ram, byte *color_ram, byte background)
 Load C64 multicolor (160x200) More...
 
void Load_C64_fli (T_IO_Context *context, byte *bitmap, byte *screen_ram, byte *color_ram, byte *background)
 Loads a C64 FLI (Flexible Line Interpretation) picture. More...
 
static long C64_unpack_get_length (const byte *buffer, long input_size, byte RLE_code, int order)
 Count the length of the unpacked data. More...
 
static void C64_unpack (byte *unpacked, const byte *buffer, long input_size, byte RLE_code, int order)
 unpack RLE packed data More...
 
static long C64_unpack_amica (byte **file_buffer, long file_size)
 Unpack the Amica Paint RLE packing. More...
 
static long C64_unpack_draz (byte **file_buffer, long file_size)
 Unpack the DRAZPAINT RLE packing. More...
 
void Load_C64 (T_IO_Context *context)
 Load C64 pictures formats. More...
 
void Load_PRG (T_IO_Context *context)
 Load C64 autoload pictures. More...
 
static int Save_C64_window (enum c64_format *saveFormat, byte *saveWhat, word *loadAddr)
 Display the dialog for C64 save parameters. More...
 
static int Encode_C64_hires (T_IO_Context *context, byte *bitmap, byte *screen_ram)
 Encode a C64 HiRes Bitmap picture. More...
 
static int Save_C64_hires (T_IO_Context *context, byte saveWhat, word loadAddr)
 Save a C64 hires picture. More...
 
int Save_C64_fli_monolayer (T_IO_Context *context, byte saveWhat, word loadAddr)
 Save a C64 FLI (Flexible Line Interpretation) picture. More...
 
static int Encode_C64_multicolor (T_IO_Context *context, byte *bitmap, byte *screen_ram, byte *color_ram, byte *background)
 Encode a picture in the C64 Multicolor bitmap format. More...
 
int Save_C64_multi (T_IO_Context *context, byte saveWhat, word loadAddr)
 Save a C64 multicolor picture. More...
 
int Save_C64_fli (T_IO_Context *context, byte saveWhat, word loadAddr)
 Save a C64 FLI (Flexible Line Interpretation) picture. More...
 
void Save_C64 (T_IO_Context *context)
 Save C64 picture. More...
 
static int C64_color_ram_pack (FILE *f, const byte *data, int count)
 Pack a stream of nibbles (ignore the high 4 bits) to a file. More...
 
void Save_PRG (T_IO_Context *context)
 Save autoloading C64 picture. More...
 
void Test_GPX (T_IO_Context *context, FILE *file)
 
void Load_GPX (T_IO_Context *context)
 

Variables

static const char * c64_format_names []
 C64 file formats names. More...
 

Detailed Description

Formats for the Commodore 64.

Macro Definition Documentation

◆ READU32LE

#define READU32LE (   p)    ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)

Enumeration Type Documentation

◆ c64_format

enum c64_format

C64 file formats.

Enumerator
F_invalid 
F_hires 

320x200

F_multi 

160x200

F_bitmap 

320x200 monochrome

F_fli 

FLI (Flexible Line Interpretation)

Function Documentation

◆ C64_unpack_doodle()

static long C64_unpack_doodle ( byte **  file_buffer,
long  file_size 
)
static

Unpack doodle/koala painter 2 data.

Returns
the unpacked data size or -1 in case of error

References C64_unpack(), C64_unpack_get_length(), GFX2_DEBUG, GFX2_Log(), GFX2_malloc, and NULL.

Referenced by Load_C64(), and Test_C64().

◆ Load_C64_hires()

static void Load_C64_hires ( T_IO_Context context,
byte bitmap,
byte screen_ram 
)
static

Load C64 hires (320x200)

Parameters
contextthe IO context
bitmapthe bitmap RAM (8000 bytes)
screen_ramthe screen RAM (1000 bytes)

If screen_ram is NULL, uses default C64 basic colors

References NULL, and Set_pixel().

Referenced by Load_C64(), and Load_PRG().

◆ Load_C64_multi()

static void Load_C64_multi ( T_IO_Context context,
byte bitmap,
byte screen_ram,
byte color_ram,
byte  background 
)
static

Load C64 multicolor (160x200)

Parameters
contextthe IO context
bitmapthe bitmap RAM (8000 bytes)
screen_ramthe screen RAM (1000 bytes)
color_ramthe color RAM (1000 bytes)
backgroundthe background color

References Set_pixel().

Referenced by Load_C64(), Load_GPX(), and Load_PRG().

◆ Load_C64_fli()

void Load_C64_fli ( T_IO_Context context,
byte bitmap,
byte screen_ram,
byte color_ram,
byte background 
)

Loads a C64 FLI (Flexible Line Interpretation) picture.

Sets 4 layers :

  • Layer 0 : filled with background colors (1 per line)
  • Layer 1 : "Color RAM" 4x8 blocks
  • Layer 2 : pixels (From Screen RAMs + Bitmap)
  • Layer 3 : Transparency layer filled with color 16
Parameters
contextthe IO context
bitmap8000 bytes buffer
screen_ram8 x 1024 bytes buffers
color_ram1000 byte buffer
background200 byte buffer

References CONTEXT_MAIN_IMAGE, NULL, Set_loading_layer(), Set_pixel(), and T_IO_Context::Type.

Referenced by Load_C64(), and Load_PRG().

◆ C64_unpack_get_length()

static long C64_unpack_get_length ( const byte buffer,
long  input_size,
byte  RLE_code,
int  order 
)
static

Count the length of the unpacked data.

RLE encoding is either ESCAPE CODE, COUNT, VALUE or ESCAPE CODE, VALUE, COUNT

Parameters
bufferthe packed data
input_sizethe packed data byte count
RLE_codethe escape code
order0 for ESCAPE, COUNT, VALUE, 1 for ESCAPE, VALUE, COUNT
Returns
the unpacked data byte count

Referenced by C64_unpack_amica(), C64_unpack_doodle(), and C64_unpack_draz().

◆ C64_unpack()

static void C64_unpack ( byte unpacked,
const byte buffer,
long  input_size,
byte  RLE_code,
int  order 
)
static

unpack RLE packed data

RLE encoding is either ESCAPE CODE, COUNT, VALUE or ESCAPE CODE, VALUE, COUNT

Parameters
unpackedbuffer to received unpacked data
bufferthe packed data
input_sizethe packed data byte count
RLE_codethe escape code
order0 for ESCAPE, COUNT, VALUE, 1 for ESCAPE, VALUE, COUNT

Referenced by C64_unpack_amica(), C64_unpack_doodle(), and C64_unpack_draz().

◆ C64_unpack_amica()

static long C64_unpack_amica ( byte **  file_buffer,
long  file_size 
)
static

Unpack the Amica Paint RLE packing.

Parameters
[in,out]file_bufferwill contain the unpacked buffer on return
[in]file_sizepacked buffer size
Returns
the unpacked data size or -1 in case of error

Ref:

References C64_unpack(), C64_unpack_get_length(), GFX2_DEBUG, GFX2_Log(), GFX2_malloc, and NULL.

Referenced by Load_C64().

◆ C64_unpack_draz()

static long C64_unpack_draz ( byte **  file_buffer,
long  file_size 
)
static

Unpack the DRAZPAINT RLE packing.

Parameters
[in,out]file_bufferwill contain the unpacked buffer on return
[in]file_sizepacked buffer size
Returns
the unpacked data size or -1 in case of error

Ref:

References C64_unpack(), C64_unpack_get_length(), GFX2_DEBUG, GFX2_Log(), GFX2_malloc, and NULL.

Referenced by Load_C64().

◆ Save_C64_window()

static int Save_C64_window ( enum c64_format saveFormat,
byte saveWhat,
word loadAddr 
)
static

Display the dialog for C64 save parameters.

Parameters
[in,out]saveFormatone of the C64 mode from c64_format
[in,out]saveWhat0=All, 1=Only bitmap, 2=Only Screen RAM, 3=Only color RAM
[in,out]loadAddractual load address or 0 for "None"
Returns
true to proceed, false to abort

References BUTTON_HELP, BUTTON_SAVE, c64_format_names, Close_window(), Display_cursor(), F_bitmap, F_fli, F_hires, F_multi, format, GFX2_DEBUG, GFX2_Log(), Is_shortcut(), Key, KEY_ESCAPE, KEY_RETURN, LEFT_SIDE, MC_Dark, MC_Light, Open_window(), Print_in_window(), Update_window_area, Window_attribute2, Window_clicked_button(), Window_dropdown_add_item(), Window_dropdown_clear_items(), Window_height, Window_help(), Window_set_dropdown_button(), Window_set_normal_button(), and Window_width.

Referenced by Save_C64().

◆ Encode_C64_hires()

static int Encode_C64_hires ( T_IO_Context context,
byte bitmap,
byte screen_ram 
)
static

Encode a C64 HiRes Bitmap picture.

320x200 pixels, with only 2 different colors per 8x8 block.

8000 bytes bitmap, 1000 bytes screen RAM

References c64state::bitmap, Get_pixel(), Warning_message(), and Warning_with_format().

Referenced by Save_C64_hires(), and Save_PRG().

◆ Save_C64_hires()

static int Save_C64_hires ( T_IO_Context context,
byte  saveWhat,
word  loadAddr 
)
static

Save a C64 hires picture.

c64 hires is 320x200 with only 2 colors per 8x8 block.

References c64state::bitmap, Encode_C64_hires(), File_error, Open_file_write(), Warning_message(), Write_bytes(), and Write_word_le().

Referenced by Save_C64().

◆ Save_C64_fli_monolayer()

int Save_C64_fli_monolayer ( T_IO_Context context,
byte  saveWhat,
word  loadAddr 
)

Save a C64 FLI (Flexible Line Interpretation) picture.

This function is able to save a one layer picture, by finding itself the background colors and color RAM value to be used.

The algorithm is :

  • first choose the lowest value for all possible background colors for each line
  • first the lowest value from the possible colors for color RAM
  • encode bitmap and screen RAMs

The algorithm can fail by picking a "wrong" background color for a line, that make the choice for the color RAM value of one of the 40 blocks impossible.

Parameters
contextthe IO context
saveWhatwhat part of the data to save
loadAddrThe load address

References c64state::bitmap, C64_pixels_to_FLI(), File_error, Open_file_write(), T_IO_Context::Pitch, T_IO_Context::Target_address, Warning_message(), Write_bytes(), and Write_word_le().

Referenced by Save_C64().

◆ Encode_C64_multicolor()

static int Encode_C64_multicolor ( T_IO_Context context,
byte bitmap,
byte screen_ram,
byte color_ram,
byte background 
)
static

Encode a picture in the C64 Multicolor bitmap format.

8000 bytes bitmap, 1000 bytes screen RAM and 1000 bytes color RAM :

BITS COLOR INFORMATION COMES FROM 00 Background color #0 (screen color) 01 Upper 4 bits of Screen RAM 10 Lower 4 bits of Screen RAM 11 Color RAM nybble (nybble = 1/2 byte = 4 bits)

References c64state::bitmap, Get_pixel(), GFX2_DEBUG, GFX2_Log(), Warning_message(), and Warning_with_format().

Referenced by Save_C64_multi(), and Save_PRG().

◆ Save_C64_multi()

int Save_C64_multi ( T_IO_Context context,
byte  saveWhat,
word  loadAddr 
)

Save a C64 multicolor picture.

Parameters
contextthe IO context
saveWhatwhat part of the data to save
loadAddrThe load address

References c64state::bitmap, Encode_C64_multicolor(), File_error, NULL, Open_file_write(), Warning_message(), Write_byte(), Write_bytes(), and Write_word_le().

Referenced by Save_C64().

◆ Save_C64_fli()

int Save_C64_fli ( T_IO_Context context,
byte  saveWhat,
word  loadAddr 
)

Save a C64 FLI (Flexible Line Interpretation) picture.

This function need a 3 layer image :

  • layer 0 is background colors
  • layer 1 is color RAM values (4x8 blocks)
  • layer 2 is the actual picture
Parameters
contextthe IO context
saveWhatwhat part of the data to save
loadAddrThe load address

References C64_FLI(), File_error, Open_file_write(), Warning_message(), Write_bytes(), and Write_word_le().

Referenced by Save_C64().

◆ C64_color_ram_pack()

static int C64_color_ram_pack ( FILE *  f,
const byte data,
int  count 
)
static

Pack a stream of nibbles (ignore the high 4 bits) to a file.

This is designed to pack the color RAM data for VIC-II, as the color RAM is only 4 bits.

The output format is a stream of bytes of the following format : CD C = (16 - count), D = DATA (4bits)

Returns
the output stream size, -1 for error

References Write_byte().

Referenced by Save_PRG().

Variable Documentation

◆ c64_format_names

const char* c64_format_names[]
static
Initial value:
= {
"Hires",
"Multicolor",
"Bitmap",
"FLI"
}

C64 file formats names.

Referenced by Load_C64(), and Save_C64_window().