├── .gitignore ├── examples └── Hello World │ ├── font.png │ ├── README.md │ ├── Makefile │ ├── Header.inc │ ├── Hello.asm │ └── Snes_Init.asm ├── main.h ├── pngfunctions.h ├── palette.h ├── argparser.h ├── tile.h ├── Makefile ├── palette.c ├── README.md ├── pngfunctions.c ├── argparser.c ├── main.c ├── tests.c ├── tile.c └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | png2snes 2 | -------------------------------------------------------------------------------- /examples/Hello World/font.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NewLunarFire/png2snes/HEAD/examples/Hello World/font.png -------------------------------------------------------------------------------- /main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | //void convert_tiles(png_structp png_ptr, png_infop info_ptr, struct arguments args, FILE* output); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /pngfunctions.h: -------------------------------------------------------------------------------- 1 | #ifndef PNG_FUNCTIONS_H 2 | #define PNG_FUNCTIONS_H 3 | 4 | int detect_png(FILE* fp); 5 | int initialize_libpng(FILE* fp, png_structp* png_ptr, png_infop* info_ptr, png_infop* end_info); 6 | int detect_palette(); 7 | uint8_t* read_png(png_structp png_ptr, png_infop info_ptr); 8 | 9 | #endif //PNG_FUNCTIONS_H 10 | -------------------------------------------------------------------------------- /palette.h: -------------------------------------------------------------------------------- 1 | #ifndef PALETTE_H 2 | #define PALETTE_H 3 | #include 4 | 5 | uint16_t* convert_palette(png_structp png_ptr, png_infop info_ptr, int* size, int pad_to_size); 6 | void output_palette_binary(char* basename, uint16_t* data, int words); 7 | void output_palette_wla(char* basename, uint16_t* data, int words); 8 | 9 | #endif //PALETTE_H 10 | -------------------------------------------------------------------------------- /examples/Hello World/README.md: -------------------------------------------------------------------------------- 1 | # Hello World 2 | 3 | This example converts a bitmap font to SNES tiles to print an "Hello World". 4 | 5 | It shows how palette and tiles can be extracted from a PNG image and used in a SNES Rom for graphics. 6 | 7 | Use `make` to compile and run `Hello.smc` with your favorite SNES emulator. It has been tested in ZSNES and Higan. 8 | -------------------------------------------------------------------------------- /argparser.h: -------------------------------------------------------------------------------- 1 | #ifndef ARG_PARSER_H 2 | #define ARG_PARSER_H 3 | 4 | /* Supplied arguments */ 5 | struct arguments 6 | { 7 | int verbose; 8 | int binary; 9 | char *input_file; 10 | char *output_file; 11 | int bitplanes; 12 | int tilesize; 13 | }; 14 | 15 | /* Argument parser */ 16 | struct arguments parse_arguments(int argc, char **argv); 17 | 18 | #endif //ARG_PARSER_H 19 | -------------------------------------------------------------------------------- /tile.h: -------------------------------------------------------------------------------- 1 | #ifndef TILE_H 2 | #define TILE_H 3 | 4 | uint8_t* convert_tiles(png_structp png_ptr, png_infop info_ptr, unsigned int bitplane_count, unsigned int tilesize, unsigned int* data_size); 5 | void output_tiles_binary(char* basename, uint8_t* data, int bytes); 6 | void output_tiles_wla(char* basename, uint8_t* data, int bytes); 7 | 8 | #define SUBTILE_SIZE 16 9 | #define TILE_SIZE 64 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-std=c99 -Wall -pedantic -g -D_GNU_SOURCE `libpng-config --cflags` 3 | LDFLAGS=`libpng-config --ldflags` -lm 4 | HEADERS=argparser.h palette.h pngfunctions.h tile.h 5 | SRC=argparser.c palette.c pngfunctions.c tile.c 6 | TARGET=png2snes 7 | 8 | all: main.c main.h $(SRC) $(HEADERS) 9 | $(CC) $(CFLAGS) $(SRC) main.c $(LDFLAGS) -o $(TARGET) 10 | 11 | test: tests.c $(SRC) $(HEADERS) 12 | $(CC) $(CFLAGS) $(SRC) tests.c $(LDFLAGS) -o $@ 13 | 14 | clean: 15 | rm -f *.asm *.cgr *.vra 16 | -------------------------------------------------------------------------------- /examples/Hello World/Makefile: -------------------------------------------------------------------------------- 1 | AS=wla-65816 2 | ASFLAGS=-v -o 3 | 4 | LD=wlalink 5 | LDFLAGS=-v -r 6 | 7 | PNG2SNES=../../png2snes 8 | PNG2SNESFLAGS=--bitplanes 2 --tilesize 8 --binary 9 | TARGET=Hello 10 | 11 | PNG=font.png 12 | SRC=$(TARGET).asm 13 | INC=Header.inc $(PNG:.png=.cgr) Snes_Init.asm 14 | 15 | OBJ=$(TARGET).obj 16 | LINK=$(TARGET).link 17 | SMC=$(TARGET).smc 18 | 19 | .PHONY: rmlink 20 | 21 | all: $(SMC) 22 | 23 | $(SMC): $(INC) $(LINK) $(OBJ) 24 | $(LD) $(LDFLAGS) $(LINK) $(SMC) 25 | 26 | $(LINK): 27 | echo '[objects]' > $(LINK) 28 | 29 | $(OBJ): $(SRC) $(INC) 30 | $(AS) $(ASFLAGS) $@ $< 31 | echo $@ >> $(LINK) 32 | 33 | font.cgr: font.png 34 | $(PNG2SNES) $(PNG2SNESFLAGS) $< --output=font 35 | 36 | clean: 37 | rm $(OBJ) $(LINK) $(SMC) $(PNG:.png=.cgr) $(PNG:.png=.vra) 38 | 39 | rmlink: 40 | rm $(LINK) 41 | -------------------------------------------------------------------------------- /examples/Hello World/Header.inc: -------------------------------------------------------------------------------- 1 | ;==LoRom== ; We'll get to HiRom some other time. 2 | 3 | .MEMORYMAP ; Begin describing the system architecture. 4 | SLOTSIZE $8000 ; The slot is $8000 bytes in size. More details on slots later. 5 | DEFAULTSLOT 0 6 | SLOT 0 $8000 ; Defines Slot 0's starting address. 7 | .ENDME ; End MemoryMap definition 8 | 9 | .ROMBANKSIZE $8000 ; Every ROM bank is 32 KBytes in size 10 | .ROMBANKS 1 ; 256 kbits - Tell WLA we want to use only 1 ROM Bank 11 | 12 | .SNESHEADER 13 | ID "SNES" ; 1-4 letter string, just leave it as "SNES" 14 | 15 | NAME "Hello World PNG Demo " ; Program Title - can't be over 21 bytes, 16 | ; "123456789012345678901" ; use spaces for unused bytes of the name. 17 | 18 | SLOWROM 19 | LOROM 20 | 21 | CARTRIDGETYPE $00 ; $00 = ROM only, see WLA documentation for others 22 | ROMSIZE $08 ; $08 = 2 Mbits, see WLA doc for more.. 23 | SRAMSIZE $00 ; No SRAM see WLA doc for more.. 24 | COUNTRY $01 ; $01 = U.S. $00 = Japan $02 = Australia, Europe, Oceania and Asia $03 = Sweden $04 = Finland $05 = Denmark $06 = France $07 = Holland $08 = Spain $09 = Germany, Austria and Switzerland $0A = Italy $0B = Hong Kong and China $0C = Indonesia $0D = Korea 25 | LICENSEECODE $00 ; Just use $00 26 | VERSION $00 ; $00 = 1.00, $01 = 1.01, etc. 27 | .ENDSNES 28 | 29 | .SNESNATIVEVECTOR ; Define Native Mode interrupt vector table 30 | COP EmptyHandler 31 | BRK EmptyHandler 32 | ABORT EmptyHandler 33 | NMI VBlank 34 | IRQ EmptyHandler 35 | .ENDNATIVEVECTOR 36 | 37 | .SNESEMUVECTOR ; Define Emulation Mode interrupt vector table 38 | COP EmptyHandler 39 | ABORT EmptyHandler 40 | NMI EmptyHandler 41 | RESET Start ; where execution starts 42 | IRQBRK EmptyHandler 43 | .ENDEMUVECTOR 44 | 45 | .BANK 0 SLOT 0 ; Defines the ROM bank and the slot it is inserted in memory. 46 | .ORG 0 ; .ORG 0 is really $8000, because the slot starts at $8000 47 | .SECTION "EmptyVectors" SEMIFREE 48 | 49 | EmptyHandler: 50 | rti 51 | 52 | .ENDS 53 | 54 | .EMPTYFILL $00 ; fill unused areas with $00, opcode for BRK. 55 | ; BRK will crash the snes if executed. 56 | -------------------------------------------------------------------------------- /examples/Hello World/Hello.asm: -------------------------------------------------------------------------------- 1 | .include "Header.inc" 2 | .include "Snes_Init.asm" 3 | 4 | VBlank: ; Needed to satisfy interrupt definition in "header.inc" 5 | rti 6 | 7 | Start: 8 | ; Initialize the SNES. 9 | Snes_Init 10 | 11 | sep #$20 ; Set the A register to 8-bit. 12 | rep #$10 ; Set the X and Y registers to 16-bit 13 | 14 | jsr DMAPalette ; Load Palette in CGRAM 15 | jsr DMATiles ; Load Tiles in VRAM 16 | 17 | stz $2105 ; Set Video Mode 0, 8x8 tiles, 4 color BG1/BG2/BG3/BG4 18 | lda #$04 ; Set BG1's Tile Map offset to $0400 (Word address) 19 | sta $2107 ; And the Tile Map size to 32x32 20 | stz $210B ; Set BG1's Character VRAM offset to $0000 (word address) 21 | 22 | lda #$01 ; Enable BG1 23 | sta $212C 24 | 25 | stz $210E 26 | stz $210E 27 | 28 | CLEARTILEMAP: 29 | lda #$80 30 | sta $2115 ; Set Word Write mode to VRAM (increment after $2119) 31 | ldx #$0400 32 | stx $2116 33 | lda #$09 34 | sta $4300 ; Set DMA Mode (word, no increment) 35 | lda #$18 36 | sta $4301 ; Write to VRAM ($2118) 37 | ldx #ZERO ; Load Zero Address 38 | stx $4302 ; Write DMA Source Address 39 | stz $4304 ; Take it from bank 0 40 | ldx #1024 41 | stx $4305 ; 1024 Bytes to transfer 42 | 43 | lda #$01 44 | sta $420B ; Initiate VRAM DMA Transfer 45 | 46 | ; Now, load up some data into our tile map 47 | ldx #$0400 + (15*32) + 10 48 | stx $2116 49 | ldx #0 50 | 51 | GETNEXTCHAR: 52 | lda TEXT.W,X ; Load next character 53 | beq TURNON ; If it's a 0, quit 54 | tay 55 | sty $2118 ; Store result in next OAM table 56 | inx ; Go to next character 57 | jmp GETNEXTCHAR 58 | 59 | TURNON: 60 | lda #15 61 | sta $2100 ; Set brightness to 15 (100%) 62 | 63 | ; Loop forever. 64 | Forever: 65 | wai 66 | jmp Forever 67 | 68 | DMAPalette: 69 | stz $2121 ; Set CGRAM Address to 0 70 | stz $4300 ; Set DMA Mode (byte, increment) 71 | lda #$22 72 | sta $4301 ; Write to CGRAM ($2122) 73 | ldx #PALETTE 74 | stx $4302 ; Write Source Address 75 | stz $4304 ; Take from Bank 0 76 | ldx #4 77 | stx $4305 ; 4 Bytes to transfer (2 colors) 78 | 79 | lda #$01 80 | sta $420B ; Initiate CGRAM DMA Transfer 81 | 82 | rts 83 | 84 | DMATiles: 85 | lda #$80 86 | sta $2115 ; Set Word Write mode to VRAM (increment after $2119) 87 | stz $2116 88 | stz $2117 ; Set VRAM Address to 0 89 | lda #$01 90 | sta $4300 ; Set DMA Mode (word, increment) 91 | lda #$18 92 | sta $4301 ; Write to VRAM ($2118) 93 | ldx #TILES ; Load Tile Address 94 | stx $4302 ; Write DMA Source Address 95 | stz $4304 ; Take it from bank 0 96 | ldx #2048 97 | stx $4305 ; 2048 Bytes to transfer 98 | 99 | lda #$01 100 | sta $420B ; Initiate VRAM DMA Transfer 101 | 102 | rts 103 | 104 | 105 | TEXT: 106 | .db "Hello World!" 107 | 108 | ZERO: 109 | .db 0 110 | 111 | PALETTE: 112 | .incbin "font.cgr" 113 | 114 | TILES: 115 | .incbin "font.vra" 116 | -------------------------------------------------------------------------------- /palette.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "palette.h" 7 | 8 | #define CONVERT_TO_BGR15(red, green, blue) (((blue & 0xF8) << 7) | ((green & 0xF8) << 2) | (red >> 3)) 9 | 10 | uint16_t* convert_palette(png_structp png_ptr, png_infop info_ptr, int* size, int pad_to_size) 11 | { 12 | uint16_t* palette; 13 | png_color* png_palette; 14 | int src_size, final_size; 15 | 16 | //Fetch Palette from PNG file 17 | png_get_PLTE(png_ptr, info_ptr, &png_palette, &src_size); 18 | 19 | if (pad_to_size > 0) { 20 | if (pad_to_size < src_size) { 21 | fprintf(stderr, "More colors (%d) than expected (%d)\n", src_size, pad_to_size); 22 | return NULL; 23 | } 24 | final_size = pad_to_size; 25 | } else { 26 | final_size = src_size; 27 | } 28 | 29 | //Allocate memory for palette. Potentially has more entires than 30 | //the source PNG when pad_to_size is used. Unused entries will be zeroed 31 | //by calloc. 32 | palette = calloc(final_size,sizeof(uint16_t)); 33 | 34 | for(size_t i = 0; i < src_size; i++) 35 | palette[i] = CONVERT_TO_BGR15(png_palette[i].red, png_palette[i].green, png_palette[i].blue); 36 | 37 | for(size_t i = 1; i < src_size; i++) 38 | { 39 | for(size_t j = 1; j < i; j++) 40 | { 41 | if(palette[i] == palette[j]) 42 | fprintf(stderr, "Found duplicate colors %lu and %lu\n", i, j); 43 | } 44 | } 45 | 46 | *size = final_size; 47 | 48 | return palette; 49 | } 50 | 51 | void output_palette_binary(char* basename, uint16_t* data, int words) 52 | { 53 | FILE* fp; 54 | char* filename; 55 | 56 | if(asprintf(&filename, "%s.cgr", basename) == -1) 57 | { 58 | perror("output_palette_binary"); 59 | return; 60 | } 61 | 62 | fp = fopen(filename, "wb"); 63 | 64 | if(!fp) 65 | { 66 | perror(filename); 67 | free(filename); 68 | return; 69 | } 70 | 71 | for(size_t i = 0; i < words; i++) 72 | { 73 | putc(data[i] & 0xFF, fp); 74 | putc(data[i] >> 8, fp); 75 | } 76 | 77 | free(filename); 78 | fclose(fp); 79 | } 80 | 81 | void output_palette_wla(char* basename, uint16_t* data, int words) 82 | { 83 | FILE* fp; 84 | char* filename; 85 | 86 | if(strcmp(basename, "-") == 0) 87 | fp = stdout; 88 | else 89 | { 90 | if(asprintf(&filename, "%s_cgram.asm", basename) == -1) 91 | { 92 | perror("output_palette_wla"); 93 | return; 94 | } 95 | 96 | fp = fopen(filename, "w"); 97 | 98 | if(!fp) 99 | { 100 | perror(filename); 101 | free(filename); 102 | return; 103 | } 104 | 105 | free(filename); 106 | } 107 | 108 | for(size_t i = 0; i < words; i++) 109 | { 110 | if((i & 0x07) == 0) 111 | fprintf(fp, "\n\t.dw "); 112 | 113 | fprintf(fp, "$%04X", data[i]); 114 | 115 | if(((i & 0x07) < 7) && (i < (words-1))) 116 | fprintf(fp, ", "); 117 | } 118 | 119 | fprintf(fp, "\n"); 120 | 121 | if(fp != stdout && fp != NULL) 122 | fclose(fp); 123 | } 124 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PNG to SNES Graphics converter 2 | 3 | This project is to be able to generate CGRAM and VRAM data for Super Nintendo 4 | from Color-Indexed (e.g. that uses a palette) PNG files. 5 | 6 | ## Usage 7 | `png2snes [OPTIONS] FILE` 8 | 9 | That's about it. The program will return different errors along the way if 10 | there are problems with the supplied PNG file. 11 | 12 | Options: 13 | 14 | * --tilesize: Specify if tiles should be 8x8, 16x16 or 32x32. 15 | * --bitplanes: Number of bits per pixel in the output. Defaults to the least bitplanes required from the PNG bit depth. 16 | * --output=BASENAME: Basename of the output file (instead of stdout). In text mode, generates BASENAME_cgram.asm for the palette and BASENAME_vram.asm for the tiles. 17 | * --binary: Outputs file to binary format. Generates BASENAME.cgr for the palette and BASENAME.vra for the tiles. 18 | * --verbose: Verbose mode (default), print diagnostic information in stderr 19 | * --quiet: No diagnostic output to stderr 20 | 21 | ## Compiling 22 | `make` and you're in business. The project depends only on libpng. 23 | 24 | ## Example 25 | An Hello World example is provided in the example/Hello World folder. You need WLA-DX 9.6 installed. Again, `make` and you're in business. 26 | 27 | ## Why use PNG for making SNES graphics? 28 | There are multiple reasons for using PNG to create your SNES tiles and sprites before converting them to CGRAM and VRAM data: 29 | 30 | * The format is lossless. Formats like JPEG are not well suited for game graphics because the color information changes slightly evertime you save introducing artifacts. But you should know this already. 31 | * It supports 24-bit colors. Both in indexed and non-indexed modes. The SNES only has 15-bit color, so a narrowing conversion has to be done, but 24-bit is a standard. 32 | * It supports transparency. The SNES uses color 0 as the transparency color, so I intend to map transparent pixels to this color in the future. This means that you don't have to find a weird color and map that to color 0 in your palette for the pixels that will be transparent. 33 | * It is widely used and implemented. All desktop managers and browsers support PNG out-of-the-box, and most if not all graphic editors can save to PNG. So it doesn't matter if you draw your images in Gimp, Paint.NET or GraphicsGale, you can save it as PNG. Also, you can already find spritesheets in PNG on the net. 34 | * It supports using a palette. This means that you can control which colors you use and limit yourself to a narrow range of colors if you use 4-colors or 16-colors mode on the SNES, and also use colors that won't overlap after the 24-bit to 15-bit narrowing conversion. 35 | * It is compressed. You can save your spritesheets in a compressed lossless format, and the program is able to inflate your format and generate your sprites at build time. 36 | * It has a polyvalent format. The format requires certain mandatory chunks to be present, but you can add ancillary chunks and the program will just ignore them. So you can add additional information in those chunks without worrying about png2snes not understanding them. 37 | 38 | ## Creating Color-Indexed PNGs 39 | In GIMP, select Image -> Mode -> Indexed... 40 | 41 | In Photoshop, Select Image -> Mode -> Index Color 42 | -------------------------------------------------------------------------------- /pngfunctions.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "pngfunctions.h" 6 | 7 | #define HEADER_BYTES 8 8 | 9 | void expand_to_byte(png_structp png_ptr, png_row_infop row_info, png_bytep data) 10 | { 11 | unsigned int pixels_per_byte = 8 / row_info->bit_depth; 12 | unsigned int mask = 0xFF >> (8 - row_info->bit_depth); 13 | unsigned int offset = row_info->width - 1; 14 | 15 | for(size_t i = row_info->rowbytes - 1; i < row_info->rowbytes; i--, offset-=pixels_per_byte) 16 | { 17 | for(size_t j = 0, k = 0; j < 8; j+=row_info->bit_depth, k++) 18 | data[offset-k] = (data[i] >> j) & mask; 19 | } 20 | } 21 | 22 | void read_transform_fn(png_structp png_ptr, png_row_infop row_info, png_bytep data) 23 | { 24 | if(row_info->bit_depth < 8) 25 | expand_to_byte(png_ptr, row_info, data); 26 | } 27 | 28 | int detect_png(FILE* fp) 29 | { 30 | unsigned char header[HEADER_BYTES]; 31 | int is_png; 32 | 33 | //Read bytes at the beginning and verify if this is a PNG file 34 | fread(header, 1, HEADER_BYTES, fp); 35 | is_png = !png_sig_cmp(header, 0, HEADER_BYTES); 36 | 37 | if (!is_png) 38 | fprintf(stderr, "File is not a PNG"); 39 | 40 | return is_png; 41 | } 42 | 43 | int initialize_libpng(FILE* fp, png_structp* png_ptr, png_infop* info_ptr, png_infop* end_info) 44 | { 45 | //Create the PNG Read Struct 46 | *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 47 | if (!png_ptr) 48 | { 49 | fprintf(stderr, "Error creating libpng read struct\n"); 50 | return 0; 51 | } 52 | 53 | //Get the first info struct 54 | *info_ptr = png_create_info_struct(*png_ptr); 55 | 56 | if (!info_ptr) 57 | { 58 | png_destroy_read_struct(png_ptr, NULL, NULL); 59 | fprintf(stderr, "Error creating first info struct\n"); 60 | return 0; 61 | } 62 | 63 | //Get the end info struct 64 | *end_info = png_create_info_struct(*png_ptr); 65 | if (!end_info) 66 | { 67 | png_destroy_read_struct(png_ptr, info_ptr, NULL); 68 | fprintf(stderr, "Error creating second info struct\n"); 69 | return 0; 70 | } 71 | 72 | //Initialize IO and tell libpng we used bytes to check the header 73 | png_init_io(*png_ptr, fp); 74 | png_set_sig_bytes(*png_ptr, HEADER_BYTES); 75 | 76 | //Read file information 77 | png_read_info(*png_ptr, *info_ptr); 78 | 79 | //Set User transform functions 80 | png_set_read_user_transform_fn(*png_ptr, read_transform_fn); 81 | png_set_user_transform_info(*png_ptr, png_get_user_transform_ptr(*png_ptr), 8, png_get_channels(*png_ptr, *info_ptr)); 82 | png_read_update_info(*png_ptr, *info_ptr); 83 | 84 | return 1; 85 | } 86 | 87 | int detect_palette(png_structp png_ptr, png_infop info_ptr) 88 | { 89 | //Verify that this is a paletted PNG image 90 | if(png_get_color_type(png_ptr, info_ptr) != PNG_COLOR_TYPE_PALETTE) 91 | { 92 | fprintf(stderr, "File does not contain a palette\n"); 93 | return 0; 94 | } 95 | 96 | //Verify the validity of the PLTE chunk 97 | if(!png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE)) 98 | { 99 | fprintf(stderr, "PLTE chunk is invalid\n"); 100 | return 0; 101 | } 102 | 103 | return 1; 104 | } 105 | -------------------------------------------------------------------------------- /argparser.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "argparser.h" 5 | 6 | #define BINARY 1 7 | #define BASENAME 2 8 | 9 | /* Version and bugs address */ 10 | const char *argp_program_version = "png2snes beta"; 11 | const char *argp_program_bug_address = ""; 12 | 13 | /* Program documentation. */ 14 | static char doc[] = "png2snes -- Create SNES Graphics from PNG files"; 15 | 16 | /* A description of the arguments we accept. */ 17 | static char args_doc[] = "INPUT_FILE"; 18 | 19 | /* The options we understand. */ 20 | static struct argp_option options[] = { 21 | {"verbose", 'v', 0, 0, "Produce more verbose output" }, 22 | {"quiet", 'q', 0, 0, "Don't produce any output" }, 23 | {"output", 'o', "BASENAME", 0, "Output to file instead of standard output" }, 24 | {"bitplanes", 'b', "PLANES", 0, "Number of bitplanes per tile (2,4 or 8)"}, 25 | {"tilesize", 't', "SIZE", 0, "Size of tiles (8x8 or 16x16)"}, 26 | {"binary", BINARY, 0, 0, "Output to binary format"}, 27 | { 0 } 28 | }; 29 | 30 | int parse_number(char* number) 31 | { 32 | char* endptr; 33 | int val = strtol(number, &endptr, 0); 34 | 35 | /* Check for various possible errors */ 36 | if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) 37 | return 0; 38 | 39 | if (endptr == number) 40 | return 0; 41 | 42 | /* Return valid conversion result */ 43 | return (int)val; 44 | } 45 | 46 | int parse_tilesize(char* arg) 47 | { 48 | int ts = parse_number(arg); 49 | 50 | /* Check for invalid values */ 51 | if(ts != 8 && ts != 16) 52 | return 0; 53 | 54 | return ts; 55 | } 56 | 57 | int parse_bitplanes(char* arg) 58 | { 59 | int bp = parse_number(arg); 60 | 61 | /* Check for invalid values */ 62 | if(bp != 2 && bp != 4 && bp != 8) 63 | return 0; 64 | 65 | return bp; 66 | } 67 | 68 | /* Parse a single option. */ 69 | error_t parse_opt (int key, char *arg, struct argp_state *state) 70 | { 71 | /* Get the input argument from argp_parse, which we 72 | know is a pointer to our arguments structure. */ 73 | struct arguments *arguments = state->input; 74 | 75 | switch (key) 76 | { 77 | case 'q': case 's': 78 | arguments->verbose = 0; 79 | break; 80 | case 'v': 81 | arguments->verbose = 1; 82 | break; 83 | case BINARY: 84 | arguments->binary = 1; 85 | break; 86 | case 'o': 87 | arguments->output_file = arg; 88 | break; 89 | case 'b': 90 | arguments->bitplanes = parse_bitplanes(arg); 91 | if(!arguments->bitplanes) 92 | { 93 | fprintf(stderr, "Invalid value for bitplanes: %s (Possible values are 2, 4 and 8)\n", arg); 94 | argp_usage(state); 95 | } 96 | break; 97 | case 't': 98 | arguments->tilesize = parse_tilesize(arg); 99 | if(!arguments->tilesize) 100 | { 101 | fprintf(stderr, "Invalid value for bitplanes: %s (Possible values are 8 and 16)\n", arg); 102 | argp_usage(state); 103 | } 104 | break; 105 | case ARGP_KEY_ARG: 106 | if (state->arg_num >= 1) 107 | /* Too many arguments. */ 108 | argp_usage (state); 109 | 110 | arguments->input_file = arg; 111 | 112 | break; 113 | 114 | case ARGP_KEY_END: 115 | if (state->arg_num < 1) 116 | /* Not enough arguments. */ 117 | argp_usage (state); 118 | break; 119 | 120 | default: 121 | return ARGP_ERR_UNKNOWN; 122 | } 123 | return 0; 124 | } 125 | 126 | /* Our argp parser. */ 127 | static struct argp argp = { options, parse_opt, args_doc, doc }; 128 | 129 | struct arguments parse_arguments(int argc, char **argv) 130 | { 131 | struct arguments arguments; 132 | 133 | /* Default values. */ 134 | arguments.verbose = 1; 135 | arguments.binary = 0; 136 | arguments.output_file = "-"; 137 | arguments.bitplanes = 0; 138 | arguments.tilesize = 0; 139 | 140 | /* Parse our arguments; every option seen by parse_opt will 141 | be reflected in arguments. */ 142 | argp_parse (&argp, argc, argv, 0, 0, &arguments); 143 | return arguments; 144 | } 145 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "argparser.h" 8 | #include "palette.h" 9 | #include "pngfunctions.h" 10 | #include "tile.h" 11 | 12 | #define DEFAULT_TILE_SIZE 8 13 | 14 | int generate_cgram(png_structp png_ptr, png_infop info_ptr, struct arguments args); 15 | void generate_vram(png_structp png_ptr, png_infop info_ptr, struct arguments args); 16 | uint8_t* convert_to_tiles(uint8_t* data, unsigned int width, unsigned int height, unsigned int bitplane_count, unsigned int tilesize, unsigned int* data_size); 17 | 18 | int main(int argc, char *argv[]) 19 | { 20 | int exit_code = -2; 21 | 22 | //Lib PNG Structures 23 | png_structp png_ptr; 24 | png_infop info_ptr, end_info; 25 | 26 | FILE* input = NULL; //File containing png image 27 | 28 | //Parse command-line arguments 29 | struct arguments args = parse_arguments(argc, argv); 30 | 31 | //Open the file 32 | input = fopen(args.input_file, "rb"); 33 | if (!input) 34 | { 35 | fprintf(stderr, "Error opening file %s\n", args.input_file); 36 | return -1; 37 | } 38 | 39 | //If file is not a PNG, quit 40 | if (!detect_png(input)) 41 | goto close_file; 42 | 43 | //If initializing libpng failed, quit 44 | if(!initialize_libpng(input, &png_ptr, &info_ptr, &end_info)) 45 | goto close_file; 46 | 47 | //If the PNG file does not contain a palette, exit 48 | if(!detect_palette(png_ptr, info_ptr)) 49 | goto clean_png_struct; 50 | 51 | if (generate_cgram(png_ptr, info_ptr, args) != 0) 52 | goto clean_png_struct; 53 | 54 | generate_vram(png_ptr, info_ptr, args); 55 | 56 | exit_code++; 57 | clean_png_struct: 58 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); 59 | exit_code++; 60 | close_file: 61 | fclose(input); 62 | 63 | return exit_code; 64 | } 65 | 66 | int generate_cgram(png_structp png_ptr, png_infop info_ptr, struct arguments args) 67 | { 68 | //Convert palette to the format used by the SNES 69 | int palette_size; 70 | uint16_t* palette = convert_palette(png_ptr, info_ptr, &palette_size, powl(2, args.bitplanes)); 71 | 72 | if (!palette) 73 | return -1; 74 | 75 | if(args.verbose) 76 | { 77 | fprintf(stderr, "Palette contains %d colors\n", palette_size); 78 | fprintf(stderr, "CGRAM section is %d bytes long\n", palette_size * 2); 79 | } 80 | 81 | if(args.binary) 82 | output_palette_binary(args.output_file, palette, palette_size); 83 | else 84 | output_palette_wla(args.output_file, palette, palette_size); 85 | 86 | free(palette); 87 | 88 | return 0; 89 | } 90 | 91 | void generate_vram(png_structp png_ptr, png_infop info_ptr, struct arguments args) 92 | { 93 | unsigned int bitplane_count, tilesize, data_size; 94 | 95 | //Get image size and bit depth 96 | unsigned int height = png_get_image_height(png_ptr, info_ptr); 97 | unsigned int width = png_get_image_width(png_ptr, info_ptr); 98 | unsigned int bit_depth = png_get_bit_depth(png_ptr, info_ptr); 99 | //unsigned int rowbytes = png_get_rowbytes(png_ptr, info_ptr); 100 | 101 | //Print tilesize 102 | if(args.tilesize == 0) 103 | { 104 | tilesize = DEFAULT_TILE_SIZE; 105 | if(args.verbose) 106 | fprintf(stderr, "Assuming %ux%u background tiles\n", tilesize, tilesize); 107 | } 108 | else 109 | { 110 | tilesize = args.tilesize; 111 | if(args.verbose) 112 | fprintf(stderr, "Using %ux%u background tiles\n", tilesize, tilesize); 113 | } 114 | 115 | //Print bitplanes used 116 | if(args.bitplanes != 0) 117 | { 118 | bitplane_count = args.bitplanes; 119 | if(args.verbose) 120 | fprintf(stderr, "Using %u bitplanes\n", bitplane_count); 121 | } 122 | else 123 | { 124 | //Get bit depth from png file 125 | bitplane_count = bit_depth; 126 | if(bitplane_count < 2) 127 | bitplane_count = 2; 128 | 129 | if(args.verbose) 130 | fprintf(stderr, "Assuming %u bitplanes\n", bitplane_count); 131 | } 132 | 133 | //Print information on the image 134 | if(args.verbose) 135 | { 136 | fprintf(stderr, "Image size is %ux%u, bit depth is %u\n", width / tilesize, height / tilesize, bit_depth); 137 | fprintf(stderr, "Tile count: %u (%ux%u)\n", (width / tilesize) * (height / tilesize), width / tilesize, height / tilesize); 138 | fprintf(stderr, "Outputting on %u bitplanes\n", bitplane_count); 139 | } 140 | 141 | uint8_t* data = convert_tiles(png_ptr, info_ptr, bitplane_count, tilesize, &data_size); 142 | 143 | if(args.verbose) 144 | fprintf(stderr, "VRAM section is %u bytes long\n", data_size); 145 | 146 | if(args.binary) 147 | output_tiles_binary(args.output_file, data, data_size); 148 | else 149 | output_tiles_wla(args.output_file, data, data_size); 150 | 151 | free(data); 152 | } 153 | -------------------------------------------------------------------------------- /examples/Hello World/Snes_Init.asm: -------------------------------------------------------------------------------- 1 | .MACRO Snes_Init 2 | sei ; Disabled interrupts 3 | clc ; clear carry to switch to native mode 4 | xce ; Xchange carry & emulation bit. native mode 5 | rep #$18 ; Binary mode (decimal mode off), X/Y 16 bit 6 | ldx #$1FFF ; set stack to $1FFF 7 | txs 8 | 9 | jsr Init 10 | .ENDM 11 | 12 | .bank 0 13 | .section "Snes_Init" SEMIFREE 14 | Init: 15 | sep #$30 ; X,Y,A are 8 bit numbers 16 | lda #$8F ; screen off, full brightness 17 | sta $2100 ; brightness + screen enable register 18 | stz $2101 ; Sprite register (size + address in VRAM) 19 | stz $2102 ; Sprite registers (address of sprite memory [OAM]) 20 | stz $2103 ; "" "" 21 | stz $2105 ; Mode 0, = Graphic mode register 22 | stz $2106 ; noplanes, no mosaic, = Mosaic register 23 | stz $2107 ; Plane 0 map VRAM location 24 | stz $2108 ; Plane 1 map VRAM location 25 | stz $2109 ; Plane 2 map VRAM location 26 | stz $210A ; Plane 3 map VRAM location 27 | stz $210B ; Plane 0+1 Tile data location 28 | stz $210C ; Plane 2+3 Tile data location 29 | stz $210D ; Plane 0 scroll x (first 8 bits) 30 | stz $210D ; Plane 0 scroll x (last 3 bits) #$0 - #$07ff 31 | lda #$FF ; The top pixel drawn on the screen isn't the top one in the tilemap, it's the one above that. 32 | sta $210E ; Plane 0 scroll y (first 8 bits) 33 | sta $2110 ; Plane 1 scroll y (first 8 bits) 34 | sta $2112 ; Plane 2 scroll y (first 8 bits) 35 | sta $2114 ; Plane 3 scroll y (first 8 bits) 36 | lda #$07 ; Since this could get quite annoying, it's better to edit the scrolling registers to fix this. 37 | sta $210E ; Plane 0 scroll y (last 3 bits) #$0 - #$07ff 38 | sta $2110 ; Plane 1 scroll y (last 3 bits) #$0 - #$07ff 39 | sta $2112 ; Plane 2 scroll y (last 3 bits) #$0 - #$07ff 40 | sta $2114 ; Plane 3 scroll y (last 3 bits) #$0 - #$07ff 41 | stz $210F ; Plane 1 scroll x (first 8 bits) 42 | stz $210F ; Plane 1 scroll x (last 3 bits) #$0 - #$07ff 43 | stz $2111 ; Plane 2 scroll x (first 8 bits) 44 | stz $2111 ; Plane 2 scroll x (last 3 bits) #$0 - #$07ff 45 | stz $2113 ; Plane 3 scroll x (first 8 bits) 46 | stz $2113 ; Plane 3 scroll x (last 3 bits) #$0 - #$07ff 47 | lda #$80 ; increase VRAM address after writing to $2119 48 | sta $2115 ; VRAM address increment register 49 | stz $2116 ; VRAM address low 50 | stz $2117 ; VRAM address high 51 | stz $211A ; Initial Mode 7 setting register 52 | stz $211B ; Mode 7 matrix parameter A register (low) 53 | lda #$01 54 | sta $211B ; Mode 7 matrix parameter A register (high) 55 | stz $211C ; Mode 7 matrix parameter B register (low) 56 | stz $211C ; Mode 7 matrix parameter B register (high) 57 | stz $211D ; Mode 7 matrix parameter C register (low) 58 | stz $211D ; Mode 7 matrix parameter C register (high) 59 | stz $211E ; Mode 7 matrix parameter D register (low) 60 | sta $211E ; Mode 7 matrix parameter D register (high) 61 | stz $211F ; Mode 7 center position X register (low) 62 | stz $211F ; Mode 7 center position X register (high) 63 | stz $2120 ; Mode 7 center position Y register (low) 64 | stz $2120 ; Mode 7 center position Y register (high) 65 | stz $2121 ; Color number register ($0-ff) 66 | stz $2123 ; BG1 & BG2 Window mask setting register 67 | stz $2124 ; BG3 & BG4 Window mask setting register 68 | stz $2125 ; OBJ & Color Window mask setting register 69 | stz $2126 ; Window 1 left position register 70 | stz $2127 ; Window 2 left position register 71 | stz $2128 ; Window 3 left position register 72 | stz $2129 ; Window 4 left position register 73 | stz $212A ; BG1, BG2, BG3, BG4 Window Logic register 74 | stz $212B ; OBJ, Color Window Logic Register (or,and,xor,xnor) 75 | sta $212C ; Main Screen designation (planes, sprites enable) 76 | stz $212D ; Sub Screen designation 77 | stz $212E ; Window mask for Main Screen 78 | stz $212F ; Window mask for Sub Screen 79 | lda #$30 80 | sta $2130 ; Color addition & screen addition init setting 81 | stz $2131 ; Add/Sub sub designation for screen, sprite, color 82 | lda #$E0 83 | sta $2132 ; color data for addition/subtraction 84 | stz $2133 ; Screen setting (interlace x,y/enable SFX data) 85 | stz $4200 ; Enable V-blank, interrupt, Joypad register 86 | lda #$FF 87 | sta $4201 ; Programmable I/O port 88 | stz $4202 ; Multiplicand A 89 | stz $4203 ; Multiplier B 90 | stz $4204 ; Multiplier C 91 | stz $4205 ; Multiplicand C 92 | stz $4206 ; Divisor B 93 | stz $4207 ; Horizontal Count Timer 94 | stz $4208 ; Horizontal Count Timer MSB (most significant bit) 95 | stz $4209 ; Vertical Count Timer 96 | stz $420A ; Vertical Count Timer MSB 97 | stz $420B ; General DMA enable (bits 0-7) 98 | stz $420C ; Horizontal DMA (HDMA) enable (bits 0-7) 99 | stz $420D ; Access cycle designation (slow/fast rom) 100 | cli ; Enable interrupts 101 | rts 102 | .ends 103 | -------------------------------------------------------------------------------- /tests.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | //#include "argparser.h" 8 | //#include "main.h" 9 | //#include "palette.h" 10 | #include "pngfunctions.h" 11 | #include "tile.h" 12 | 13 | uint8_t* get_tile_from_png(uint8_t* destination, png_structp png_ptr, png_bytepp row_pointers, int x, int y); 14 | void convert_to_bitplanes(uint8_t* destination, const uint8_t* source, int bitplane_count); 15 | 16 | //Tests 17 | int testGetTileFromPNG(); 18 | int testConvert0sto2Bitplanes(); 19 | int testConvert3sto2Bitplanes(); 20 | int testConvertTo2Bitplanes(); 21 | 22 | 23 | struct unit_test_t { 24 | char* name; 25 | int (*testFunction)(); 26 | }; 27 | 28 | struct unit_test_t unitTests[] = { 29 | {"Get Tile from PNG", testGetTileFromPNG}, 30 | {"Convert 0s to 2 bitplanes", testConvert0sto2Bitplanes}, 31 | {"Convert 3s to 2 bitplanes", testConvert3sto2Bitplanes}, 32 | {"Convert Array to 2 bitplanes", testConvertTo2Bitplanes}, 33 | {NULL, NULL} 34 | }; 35 | 36 | int main(int argc, char* argv[]) { 37 | unsigned int i = 0; 38 | unsigned int testsPassed = 0; 39 | unsigned int testsFailed = 0; 40 | 41 | printf("PNG2SNES test suite\n\n"); 42 | for(i = 0; unitTests[i].name != NULL; i++) { 43 | printf("=====================================\n"); 44 | printf("Test %d: %s\n", i+1, unitTests[i].name); 45 | printf("=====================================\n"); 46 | int result = unitTests[i].testFunction(); 47 | 48 | printf("=====================================\n"); 49 | if(result != 0) { 50 | printf("Failed, returned %d exit status\n", result); 51 | testsFailed++; 52 | } else { 53 | printf("Success\n"); 54 | testsPassed++; 55 | } 56 | printf("=====================================\n"); 57 | } 58 | 59 | printf("Stats: %d run, %d passed, %d failed\n", i, testsPassed, testsFailed); 60 | return testsFailed > 1; 61 | } 62 | 63 | png_bytepp get_row_pointers(png_structp png_ptr, unsigned int width, unsigned int height) { 64 | size_t i; 65 | png_bytepp row_pointers = malloc(sizeof(png_bytep) * height); 66 | 67 | for(i = 0; i < height; i++) 68 | row_pointers[i] = malloc(sizeof(png_byte) * width); 69 | 70 | png_read_image(png_ptr, row_pointers); 71 | return row_pointers; 72 | } 73 | 74 | void free_row_pointers(png_bytepp row_pointers, unsigned int height) { 75 | size_t i; 76 | for(i = 0; i < height; i++) 77 | free(row_pointers[i]); 78 | 79 | free(row_pointers); 80 | } 81 | 82 | int testGetTileFromPNG() { 83 | int exit_code = 0; 84 | uint8_t tile[TILE_SIZE]; 85 | size_t col, row, k; 86 | 87 | //Lib PNG Structures 88 | png_structp png_ptr; 89 | png_infop info_ptr, end_info; 90 | png_bytepp row_pointers; 91 | 92 | //Open the file 93 | FILE* input = fopen("ChessPattern.png", "rb"); //File containing png image 94 | 95 | if (!input) 96 | { 97 | //Could not open file 98 | printf("Error opening file ChessPattern.png\n"); 99 | return -1; 100 | } 101 | 102 | //If file is not a PNG, quit 103 | if (!detect_png(input)) { 104 | fclose(input); 105 | return -1; 106 | } 107 | 108 | //If initializing libpng failed, quit 109 | if(!initialize_libpng(input, &png_ptr, &info_ptr, &end_info)) { 110 | fclose(input); 111 | return -1; 112 | } 113 | 114 | row_pointers = get_row_pointers(png_ptr, 16, 16); 115 | 116 | for(row = 0; row < 2; row++) { 117 | for(col = 0; col < 2; col++) { 118 | uint8_t v = (row * 2) + col; 119 | get_tile_from_png(tile, png_ptr, row_pointers, col, row); 120 | 121 | for(k = 0; k < TILE_SIZE; k++) { 122 | if(tile[k] != v) { 123 | printf("Expected %u got %u\n", v, tile[k]); 124 | row = col = 2; 125 | k = TILE_SIZE; 126 | exit_code = 1; 127 | } 128 | } 129 | } 130 | } 131 | 132 | free_row_pointers(row_pointers, 16); 133 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); 134 | fclose(input); 135 | return exit_code; 136 | } 137 | 138 | int testConvert0sto2Bitplanes() { 139 | uint8_t data[64]; 140 | uint8_t bitplanes[16]; 141 | size_t i; 142 | int exit_code = 0; 143 | 144 | memset(data, 0, 64); 145 | 146 | convert_to_bitplanes(bitplanes, data, 2); 147 | 148 | for(i = 0; i < 16; i++) { 149 | if(bitplanes[i] != 0) { 150 | printf("Expected all zeroes, but got a %02X at %lu\n", bitplanes[i], i); 151 | exit_code = 1; 152 | } 153 | } 154 | 155 | return exit_code; 156 | } 157 | 158 | int testConvert3sto2Bitplanes() { 159 | uint8_t data[64]; 160 | uint8_t bitplanes[16]; 161 | size_t i; 162 | int exit_code = 0; 163 | 164 | memset(data, 3, 64); 165 | 166 | convert_to_bitplanes(bitplanes, data, 2); 167 | 168 | for(i = 0; i < 16; i++) { 169 | if(bitplanes[i] != 0xFF) { 170 | printf("Expected all FFs, but got a %02X at %lu\n", bitplanes[i], i); 171 | exit_code = 1; 172 | } 173 | } 174 | 175 | return exit_code; 176 | } 177 | 178 | int testConvertTo2Bitplanes() { 179 | uint8_t actualResult[16]; 180 | size_t i; 181 | 182 | const uint8_t inputSequence[] = {0, 0, 0, 0, 0, 0, 0, 0, 183 | 0, 0, 0, 0, 0, 0, 0, 0, 184 | 1, 1, 1, 1, 1, 1, 1, 1, 185 | 1, 1, 1, 1, 1, 1, 1, 1, 186 | 2, 2, 2, 2, 2, 2, 2, 2, 187 | 2, 2, 2, 2, 2, 2, 2, 2, 188 | 3, 3, 3, 3, 3, 3, 3, 3, 189 | 3, 3, 3, 3, 3, 3, 3, 3}; 190 | 191 | const uint8_t expectedResult[] = {0x00, 0x00, 0x00, 0x00, 192 | 0xFF, 0x00, 0xFF, 0x00, 193 | 0x00, 0xFF, 0x00, 0xFF, 194 | 0xFF, 0xFF, 0xFF, 0xFF}; 195 | 196 | 197 | convert_to_bitplanes(actualResult, inputSequence, 2); 198 | if(memcmp(actualResult, expectedResult, 16) != 0) { 199 | printf("Expected and actual results do not match\n"); 200 | 201 | printf("Expected:"); 202 | for(i = 0; i < 15; i++) 203 | printf("%02X, ", expectedResult[i]); 204 | printf("%02X\n", expectedResult[15]); 205 | 206 | printf("Actual:"); 207 | for(i = 0; i < 15; i++) 208 | printf("%02X, ", actualResult[i]); 209 | printf("%02X\n", actualResult[15]); 210 | 211 | return 1; 212 | } 213 | 214 | return 0; 215 | } 216 | -------------------------------------------------------------------------------- /tile.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "tile.h" 8 | 9 | void output_tiles_binary(char* basename, uint8_t* data, int bytes) 10 | { 11 | 12 | FILE* fp; 13 | char* filename; 14 | 15 | if(asprintf(&filename, "%s.vra", basename) == -1) 16 | { 17 | perror("output_tiles_binary"); 18 | return; 19 | } 20 | 21 | fp = fopen(filename, "wb"); 22 | 23 | if(!fp) 24 | { 25 | perror(filename); 26 | free(filename); 27 | return; 28 | } 29 | 30 | for(size_t i = 0; i < bytes; i++) 31 | putc(data[i], fp); 32 | 33 | free(filename); 34 | fclose(fp); 35 | } 36 | 37 | void output_tiles_wla(char* basename, uint8_t* data, int bytes) 38 | { 39 | FILE* fp; 40 | char* filename; 41 | 42 | if(strcmp(basename, "-") == 0) 43 | fp = stdout; 44 | else 45 | { 46 | if(asprintf(&filename, "%s_vram.asm", basename) == -1) 47 | { 48 | perror("output_tiles_wla"); 49 | return; 50 | } 51 | 52 | fp = fopen(filename, "w"); 53 | 54 | if(!fp) 55 | { 56 | perror(filename); 57 | free(filename); 58 | return; 59 | } 60 | 61 | free(filename); 62 | } 63 | 64 | for(size_t i = 0; i < bytes; i++) 65 | { 66 | if((i & 0x0F) == 0) 67 | fprintf(fp, "\n\t.db "); 68 | 69 | fprintf(fp, "$%02X", data[i]); 70 | 71 | if(((i & 0x0F) < 15) && (i < (bytes-1))) 72 | fprintf(fp, ", "); 73 | } 74 | 75 | fprintf(fp, "\n"); 76 | 77 | if(fp != stdout && fp != NULL) 78 | fclose(fp); 79 | } 80 | 81 | void print_tile(uint8_t* tile) { 82 | size_t i, j; 83 | for(i = 0; i < 8; i++) { 84 | for(j = 0; j < 8; j++) { 85 | printf("%d", tile[(i*8) + j]); 86 | } 87 | 88 | printf("\n"); 89 | } 90 | 91 | printf("\n"); 92 | } 93 | 94 | void print_bitplanes(uint8_t* bitplanes) { 95 | size_t i = 0; 96 | for(i = 0; i < (SUBTILE_SIZE - 1); i++) { 97 | printf("%02X, ", bitplanes[i]); 98 | } 99 | 100 | printf("%02X\n", bitplanes[SUBTILE_SIZE - 1]); 101 | } 102 | 103 | uint8_t* get_tile_from_png(uint8_t* destination, png_structp png_ptr, png_bytepp row_pointers, int x, int y) 104 | { 105 | unsigned int row = y * 8; 106 | 107 | for(size_t k = 0; k < 8; k++, row++) 108 | memcpy(destination + (k*8), row_pointers[row] + (x*8), 8); 109 | 110 | return destination; 111 | } 112 | 113 | void convert_to_bitplanes(uint8_t* destination, const uint8_t* source, int bitplane_count) 114 | { 115 | uint8_t color; 116 | 117 | memset(destination, 0, bitplane_count * 8); 118 | 119 | //For each row 120 | for(size_t row = 0, offset = 0; row < 8; row++, offset+=2) 121 | { 122 | //For each column 123 | for(size_t bit = 7; bit < 8; bit--) 124 | { 125 | //Get color number 126 | color = source[(row*8) + 7 - bit]; 127 | 128 | switch(bitplane_count) 129 | { 130 | case 8: 131 | destination[offset+(SUBTILE_SIZE*3)+1] |= ((color & 0x80) >> 7) << bit; 132 | destination[offset+(SUBTILE_SIZE*3)] |= ((color & 0x40) >> 6) << bit; 133 | destination[offset+(SUBTILE_SIZE*2)+1] |= ((color & 0x20) >> 5) << bit; 134 | destination[offset+(SUBTILE_SIZE*2)] |= ((color & 0x10) >> 4) << bit; 135 | case 4: 136 | destination[offset+SUBTILE_SIZE+1] |= ((color & 0x08) >> 3) << bit; 137 | destination[offset+SUBTILE_SIZE] |= ((color & 0x04) >> 2) << bit; 138 | case 2: 139 | destination[offset+1] |= ((color & 0x02) >> 1) << bit; 140 | destination[offset] |= (color & 0x01) << bit; 141 | } 142 | } 143 | } 144 | } 145 | 146 | uint8_t* convert_to_tiles_16_16(png_structp png_ptr, png_infop info_ptr, unsigned int bitplane_count, unsigned int* data_size) 147 | { 148 | //Keep tracks of outputed bytes 149 | //Get image size and bit depth 150 | unsigned int height = png_get_image_height(png_ptr, info_ptr); 151 | unsigned int width = png_get_image_width(png_ptr, info_ptr); 152 | //unsigned int bit_depth = png_get_bit_depth(png_ptr, info_ptr); 153 | unsigned int rowbytes = png_get_rowbytes(png_ptr, info_ptr); 154 | 155 | unsigned int horizontal_tiles = width / 16; 156 | unsigned int vertical_tiles = height / 16; 157 | unsigned int tile_count = horizontal_tiles * vertical_tiles; 158 | unsigned int bytes_per_tile = 8 * bitplane_count; 159 | 160 | unsigned int tile_number; 161 | uint8_t tile[TILE_SIZE]; 162 | uint8_t* data; 163 | 164 | png_bytepp row_pointers = malloc(sizeof(png_bytep) * height); 165 | for(size_t i = 0; i < height; i++) 166 | row_pointers[i] = malloc(sizeof(png_byte) * rowbytes); 167 | 168 | png_read_image(png_ptr, row_pointers); 169 | 170 | //Optimize this calculation. It shouldn't be that complex 171 | *data_size = ((((tile_count/8) + 1) * 8) + tile_count) * 16 * bitplane_count; 172 | 173 | //Allocate required space 174 | data = malloc(*data_size); 175 | 176 | //For each tile row 177 | for(size_t i = 0, k = 0; i < vertical_tiles; i++) 178 | { 179 | //For each tile 180 | for(size_t j = 0; j < horizontal_tiles; j++, k++) 181 | { 182 | //Tile 183 | tile_number = ((k & 0x07) << 1) | ((k & ~(0x07)) << 2); 184 | get_tile_from_png(tile, png_ptr, row_pointers, 2*j, 2*i); 185 | convert_to_bitplanes(data + (tile_number * bytes_per_tile), tile, bitplane_count); 186 | 187 | //print_tile(tile); 188 | //print_bitplanes(data + (tile_number * bytes_per_tile)); 189 | printf("Tile Number = %u\nOffset = %u\n", tile_number, tile_number * bytes_per_tile); 190 | 191 | //Tile + 1 192 | tile_number += 1; 193 | get_tile_from_png(tile, png_ptr, row_pointers, (2*j) + 1, 2*i); 194 | convert_to_bitplanes(data + (tile_number * bytes_per_tile), tile, bitplane_count); 195 | 196 | //print_tile(tile); 197 | //print_bitplanes(data + (tile_number * bytes_per_tile)); 198 | printf("Tile Number = %u\nOffset = %u\n", tile_number, tile_number * bytes_per_tile); 199 | 200 | //Tile + 16 201 | tile_number += 15; 202 | get_tile_from_png(tile, png_ptr, row_pointers, 2*j, (2*i) + 1); 203 | convert_to_bitplanes(data + (tile_number * bytes_per_tile), tile, bitplane_count); 204 | 205 | //print_tile(tile); 206 | //print_bitplanes(data + (tile_number * bytes_per_tile)); 207 | printf("Tile Number = %u\nOffset = %u\n", tile_number, tile_number * bytes_per_tile); 208 | 209 | //Tile + 17 210 | tile_number += 1; 211 | get_tile_from_png(tile, png_ptr, row_pointers, (2*j) + 1, (2*i) + 1); 212 | convert_to_bitplanes(data + (tile_number * bytes_per_tile), tile, bitplane_count); 213 | 214 | //print_tile(tile); 215 | //print_bitplanes(data + (tile_number * bytes_per_tile)); 216 | printf("Tile Number = %u\nOffset = %u\n", tile_number, tile_number * bytes_per_tile); 217 | } 218 | } 219 | 220 | return data; 221 | } 222 | 223 | uint8_t* convert_to_tiles_8_8(png_structp png_ptr, png_infop info_ptr, unsigned int bitplane_count, unsigned int* data_size) 224 | { 225 | //Keep tracks of outputed bytes 226 | unsigned int height = png_get_image_height(png_ptr, info_ptr); 227 | unsigned int width = png_get_image_width(png_ptr, info_ptr); 228 | unsigned int rowbytes = png_get_rowbytes(png_ptr, info_ptr); 229 | unsigned int horizontal_tiles = width / 8; 230 | unsigned int vertical_tiles = height / 8; 231 | unsigned int bytes_per_tile = 8 * bitplane_count; 232 | 233 | //unsigned int position; 234 | uint8_t *tile, *data; 235 | 236 | png_bytepp row_pointers = malloc(sizeof(png_bytep) * height); 237 | for(size_t i = 0; i < height; i++) 238 | row_pointers[i] = malloc(sizeof(png_byte) * rowbytes); 239 | 240 | png_read_image(png_ptr, row_pointers); 241 | 242 | *data_size = horizontal_tiles * vertical_tiles * 8 * bitplane_count; 243 | data = malloc(*data_size); 244 | tile = malloc(TILE_SIZE); 245 | 246 | //For each tile row 247 | for(size_t i = 0, k = 0; i < vertical_tiles; i++) 248 | { 249 | //For each tile 250 | for(size_t j = 0; j < horizontal_tiles; j++, k++) 251 | { 252 | get_tile_from_png(tile, png_ptr, row_pointers, j, i); 253 | convert_to_bitplanes(data + (k * bytes_per_tile), tile, bitplane_count); 254 | } 255 | } 256 | 257 | for (size_t i = 0; i < height; i++) 258 | { 259 | free(row_pointers[i]); 260 | } 261 | 262 | free(row_pointers); 263 | free(tile); 264 | return data; 265 | } 266 | 267 | uint8_t* convert_tiles(png_structp png_ptr, png_infop info_ptr, unsigned int bitplane_count, unsigned int tilesize, unsigned int* data_size) 268 | { 269 | /* 270 | 271 | //Keep tracks of outputed bytes 272 | //Get image size and bit depth 273 | unsigned int height = png_get_image_height(png_ptr, info_ptr); 274 | unsigned int width = png_get_image_width(png_ptr, info_ptr); 275 | unsigned int bit_depth = png_get_bit_depth(png_ptr, info_ptr); 276 | unsigned int rowbytes = png_get_rowbytes(png_ptr, info_ptr); 277 | 278 | unsigned int horizontal_tiles = width / tilesize; 279 | unsigned int vertical_tiles = height / tilesize; 280 | unsigned int tile_count = horizontal_tiles * vertical_tiles; 281 | unsigned int bytes_per_tile = 8 * bitplane_count; 282 | 283 | 284 | png_bytepp row_pointers = malloc(sizeof(png_bytep) * height); 285 | for(size_t i = 0; i < height; i++) 286 | row_pointers[i] = malloc(sizeof(png_byte) * rowbytes); 287 | 288 | png_read_image(png_ptr, row_pointers); 289 | 290 | //For each tile row 291 | for(size_t i = 0, k = 0; i < vertical_tiles; i++) 292 | { 293 | //For each tile 294 | for(size_t j = 0; j < horizontal_tiles; j++, k++) 295 | { 296 | //Convert single tile 297 | } 298 | } 299 | */ 300 | 301 | if(tilesize == 8) 302 | { 303 | return convert_to_tiles_8_8(png_ptr, info_ptr, bitplane_count, data_size); 304 | } 305 | else// if(tilesize == 16) 306 | { 307 | return convert_to_tiles_16_16(png_ptr, info_ptr, bitplane_count, data_size); 308 | } 309 | } 310 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------