mirror of
https://github.com/DarkStore-3DS/bannertool.git
synced 2026-12-06 03:07:17 +00:00
Initial commit.
This commit is contained in:
commit
2e75e2c9cc
11 files changed
+8109
No files matched your search
@@ -0,0 +1,16 @@
|
||||
#ifndef __DATA_H__
|
||||
#define __DATA_H__
|
||||
|
||||
#include "types.h"
|
||||
|
||||
u8 TILE_ORDER[64] = { 0, 1, 8, 9, 2, 3, 10, 11, 16, 17, 24, 25, 18, 19, 26, 27,
|
||||
4, 5, 12, 13, 6, 7, 14, 15, 20, 21, 28, 29, 22, 23, 30, 31,
|
||||
32, 33, 40, 41, 34, 35, 42, 43, 48, 49, 56, 57, 50, 51, 58, 59,
|
||||
36, 37, 44, 45, 38, 39, 46, 47, 52, 53, 60, 61, 54, 55, 62, 63 };
|
||||
|
||||
u8 CGFX_HEADER[0x1580] = { 0x43, 0x47, 0x46, 0x58, 0xFF, 0xFE, 0x14, 0x00, 0x00, 0x00, 0x00, 0x05, 0x80, 0x15, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x44, 0x41, 0x54, 0x41, 0xE4, 0x14, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x49, 0x43, 0x54, 0x2C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0xB0, 0x0F, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x44, 0x49, 0x43, 0x54, 0x2C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x8B, 0x0F, 0x00, 0x00, 0x24, 0x0F, 0x00, 0x00, 0x92, 0x00, 0x00, 0x40, 0x43, 0x4D, 0x44, 0x4C, 0x00, 0x00, 0x00, 0x07, 0x70, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x0D, 0x00, 0x00, 0x68, 0x06, 0x00, 0x00, 0x74, 0x0B, 0x00, 0x00, 0x44, 0x49, 0x43, 0x54, 0x4C, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7B, 0x0E, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x96, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x7D, 0x0E, 0x00, 0x00, 0xC4, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x81, 0x0E, 0x00, 0x00, 0x74, 0x01, 0x00, 0x00, 0x44, 0x49, 0x43, 0x54, 0x2C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x67, 0x0E, 0x00, 0x00, 0x6C, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x1F, 0x0E, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x44, 0x49, 0x43, 0x54, 0x2C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x1D, 0x0E, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x11, 0x0E, 0x00, 0x00, 0x0D, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xED, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0xAD, 0x0D, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x49, 0x43, 0x54, 0x3C, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0xA0, 0x0D, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x96, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x9A, 0x0D, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x84Line truncated
|
||||
u32 CGFX_HEADER_LENGTH = 0x1580;
|
||||
u8 BANNER_CBMD_HEADER[0x84] = { 0x43, 0x42, 0x4D, 0x44, 0x00, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
u32 BANNER_CBMD_HEADER_LENGTH = 0x84;
|
||||
|
||||
#endif
|
||||
+6104
File diff suppressed because it is too large.
Load diff
+1702
File diff suppressed because it is too large.
Load diff
+125
@@ -0,0 +1,125 @@
|
||||
#include "lz11.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <sstream>
|
||||
#include <string.h>
|
||||
|
||||
#define MIN(a,b) (((a)<(b))?(a):(b))
|
||||
|
||||
// Ported from: https://github.com/svn2github/3DS-Explorer/blob/master/3DSExplorer/DSDecmp/Formats/Nitro/LZ11.cs
|
||||
|
||||
int get_occurence_length(u8* newPtr, int newLength, u8* oldPtr, int oldLength, int* disp) {
|
||||
if(disp != NULL) {
|
||||
*disp = 0;
|
||||
}
|
||||
|
||||
if(newLength == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int maxLength = 0;
|
||||
for(int i = 0; i < oldLength - 1; i++) {
|
||||
u8* currentOldStart = oldPtr + i;
|
||||
int currentLength = 0;
|
||||
for(int j = 0; j < newLength; j++) {
|
||||
if(*(currentOldStart + j) != *(newPtr + j)) {
|
||||
break;
|
||||
}
|
||||
|
||||
currentLength++;
|
||||
}
|
||||
|
||||
if(currentLength > maxLength) {
|
||||
maxLength = currentLength;
|
||||
if(disp != NULL) {
|
||||
*disp = oldLength - i;
|
||||
}
|
||||
|
||||
if(maxLength == newLength) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return maxLength;
|
||||
}
|
||||
|
||||
u8* compress_lz11(u8* input, u32 inputSize, u32* size) {
|
||||
if (inputSize > 0xFFFFFF) {
|
||||
printf("ERROR: LZ11 input is too large.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
std::stringstream ss;
|
||||
|
||||
u8 header[4] = { 0x11, (u8) (inputSize & 0xFF), (u8) ((inputSize >> 8) & 0xFF), (u8) ((inputSize >> 16) & 0xFF) };
|
||||
ss.write((char*) header, 4);
|
||||
|
||||
int compressedLength = 4;
|
||||
u8 outbuffer[8 * 4 + 1];
|
||||
outbuffer[0] = 0;
|
||||
int bufferlength = 1;
|
||||
int bufferedBlocks = 0;
|
||||
int readBytes = 0;
|
||||
while(readBytes < inputSize) {
|
||||
if(bufferedBlocks == 8) {
|
||||
ss.write((char*) outbuffer, bufferlength);
|
||||
compressedLength += bufferlength;
|
||||
outbuffer[0] = 0;
|
||||
bufferlength = 1;
|
||||
bufferedBlocks = 0;
|
||||
}
|
||||
|
||||
int disp = 0;
|
||||
int oldLength = MIN(readBytes, 0x1000);
|
||||
int length = get_occurence_length(input + readBytes, MIN(inputSize - readBytes, 0x10110), input + readBytes - oldLength, oldLength, &disp);
|
||||
if(length < 3) {
|
||||
outbuffer[bufferlength++] = *(input + (readBytes++));
|
||||
} else {
|
||||
readBytes += length;
|
||||
outbuffer[0] |= (u8)(1 << (7 - bufferedBlocks));
|
||||
if(length > 0x110) {
|
||||
outbuffer[bufferlength] = 0x10;
|
||||
outbuffer[bufferlength] |= (u8)(((length - 0x111) >> 12) & 0x0F);
|
||||
bufferlength++;
|
||||
outbuffer[bufferlength] = (u8)(((length - 0x111) >> 4) & 0xFF);
|
||||
bufferlength++;
|
||||
outbuffer[bufferlength] = (u8)(((length - 0x111) << 4) & 0xF0);
|
||||
} else if(length > 0x10) {
|
||||
outbuffer[bufferlength] = 0x00;
|
||||
outbuffer[bufferlength] |= (u8)(((length - 0x111) >> 4) & 0x0F);
|
||||
bufferlength++;
|
||||
outbuffer[bufferlength] = (u8)(((length - 0x111) << 4) & 0xF0);
|
||||
} else {
|
||||
outbuffer[bufferlength] = (u8)(((length - 1) << 4) & 0xF0);
|
||||
}
|
||||
|
||||
outbuffer[bufferlength] |= (u8)(((disp - 1) >> 8) & 0x0F);
|
||||
bufferlength++;
|
||||
outbuffer[bufferlength] = (u8)((disp - 1) & 0xFF);
|
||||
bufferlength++;
|
||||
}
|
||||
|
||||
bufferedBlocks++;
|
||||
}
|
||||
|
||||
if(bufferedBlocks > 0) {
|
||||
ss.write((char*) outbuffer, bufferlength);
|
||||
compressedLength += bufferlength;
|
||||
}
|
||||
|
||||
int padLength = 4 - (compressedLength % 4);
|
||||
if(padLength > 0) {
|
||||
u8 pad[padLength];
|
||||
memset(pad, 0, (size_t) padLength);
|
||||
ss.write((char*) pad, padLength);
|
||||
}
|
||||
|
||||
u8* buf = (u8*) malloc((size_t) compressedLength);
|
||||
ss.read((char*) buf, compressedLength);
|
||||
|
||||
*size = (u32) compressedLength;
|
||||
return buf;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef __LZ11_H__
|
||||
#define __LZ11_H__
|
||||
|
||||
#include "types.h"
|
||||
|
||||
u8* compress_lz11(u8* input, u32 inputSize, u32* size);
|
||||
|
||||
#endif
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "data.h"
|
||||
#include "lz11.h"
|
||||
#include "types.h"
|
||||
#include "lodepng.h"
|
||||
|
||||
u8* convert_to_cgfx(const char* file, u32 width, u32 height, u32* size) {
|
||||
unsigned char* img;
|
||||
unsigned int imgWidth, imgHeight;
|
||||
if(lodepng_decode32_file(&img, &imgWidth, &imgHeight, file)) {
|
||||
printf("ERROR: Could not load png file.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(imgWidth != width || imgHeight != height) {
|
||||
printf("ERROR: Image must be exactly %d x %d in size.\n", width, height);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
u8 converted[width * height * 2];
|
||||
u32 n = 0;
|
||||
for(int y = 0; y < height; y += 8) {
|
||||
for(int x = 0; x < width; x += 8) {
|
||||
for(int k = 0; k < 8 * 8; k++) {
|
||||
u32 xx = (u32) (TILE_ORDER[k] & 0x7);
|
||||
u32 yy = (u32) (TILE_ORDER[k] >> 3);
|
||||
|
||||
u8* pixel = img + (((y + yy) * width + (x + xx)) * 4);
|
||||
converted[n++] = ((pixel[2] >> 4) << 4) | (pixel[3] >> 4);
|
||||
converted[n++] = ((pixel[0] >> 4) << 4) | (pixel[1] >> 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
u8* ret = (u8*) malloc(CGFX_HEADER_LENGTH + (width * height * 2));
|
||||
memcpy(ret, CGFX_HEADER, CGFX_HEADER_LENGTH);
|
||||
memcpy(ret + CGFX_HEADER_LENGTH, converted, width * height * 2);
|
||||
|
||||
*size = CGFX_HEADER_LENGTH + (width * height * 2);
|
||||
return ret;
|
||||
}
|
||||
|
||||
u8* make_banner(const char* file, u32* size) {
|
||||
u32 originalSize = 0;
|
||||
u8* cgfx = convert_to_cgfx(file, 256, 128, &originalSize);
|
||||
if(!cgfx) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
u32 compressedSize = 0;
|
||||
u8* compressed = compress_lz11(cgfx, originalSize, &compressedSize);
|
||||
free(cgfx);
|
||||
if(!compressed) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
u32 pad = 16 - ((BANNER_CBMD_HEADER_LENGTH + 4 + compressedSize) % 16);
|
||||
u32 totalLength = BANNER_CBMD_HEADER_LENGTH + 4 + compressedSize + pad;
|
||||
|
||||
u8* ret = (u8*) malloc(totalLength);
|
||||
memcpy(ret, BANNER_CBMD_HEADER, BANNER_CBMD_HEADER_LENGTH);
|
||||
memcpy(ret + BANNER_CBMD_HEADER_LENGTH, &totalLength, 4);
|
||||
memcpy(ret + BANNER_CBMD_HEADER_LENGTH + 4, compressed, compressedSize);
|
||||
memset(ret + BANNER_CBMD_HEADER_LENGTH + 4 + compressedSize, 0, pad);
|
||||
|
||||
free(compressed);
|
||||
*size = (u32) totalLength;
|
||||
return ret;
|
||||
}
|
||||
|
||||
u8* make_audio(const char* file, u32* size) {
|
||||
// TODO: convert from a WAV file.
|
||||
FILE* fd = fopen(file, "rb");
|
||||
if(!fd) {
|
||||
printf("ERROR: Could not load audio file.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fseek(fd, 0, SEEK_END);
|
||||
size_t length = (size_t) ftell(fd);
|
||||
fseek(fd, 0, SEEK_SET);
|
||||
|
||||
u8* data = (u8*) malloc(length);
|
||||
fread(data, 1, length, fd);
|
||||
fclose(fd);
|
||||
|
||||
*size = (u32) length;
|
||||
return data;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if(argc != 4) {
|
||||
printf("Usage: %s <banner png> <audio bcwav> <output file>", argv[0]);
|
||||
}
|
||||
|
||||
u32 bannerSize = 0;
|
||||
u8* banner = make_banner(argv[1], &bannerSize);
|
||||
if(!banner) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
u32 audioSize = 0;
|
||||
u8* audio = make_audio(argv[2], &audioSize);
|
||||
if(!audio) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
FILE* fd = fopen(argv[3], "wb");
|
||||
if(!fd) {
|
||||
printf("ERROR: Could not write output file.\n");
|
||||
return 3;
|
||||
}
|
||||
|
||||
fwrite(banner, 1, bannerSize, fd);
|
||||
fwrite(audio, 1, audioSize, fd);
|
||||
fclose(fd);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#ifndef __TYPES_H__
|
||||
#define __TYPES_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef uint8_t u8;
|
||||
typedef uint16_t u16;
|
||||
typedef uint32_t u32;
|
||||
typedef uint64_t u64;
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user