Initial commit.

This commit is contained in:
Steven Smith 2015-01-22 22:06:24 -08:00
commit 2e75e2c9cc
11 changed files with 8109 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.idea
build

7
CMakeLists.txt Normal file
View file

@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 2.8.4)
project(bannertool)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES source/main.cpp source/lz11.cpp source/lodepng.cpp)
add_executable(bannertool ${SOURCE_FILES})

7
LICENSE.txt Normal file
View file

@ -0,0 +1,7 @@
Copyright (C) 2015 Steveice10
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

6
README.md Normal file
View file

@ -0,0 +1,6 @@
<b><center><h1>bannertool</h></center></b>
==========
A tool for creating 3DS banners. Currently does not support WAV files as audio input.
Usage: bannertool <banner png> <audio bcwav> <output file>

16
source/data.h Normal file

File diff suppressed because one or more lines are too long

6104
source/lodepng.cpp Normal file

File diff suppressed because it is too large Load diff

1702
source/lodepng.h Normal file

File diff suppressed because it is too large Load diff

125
source/lz11.cpp Normal file
View file

@ -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;
}

8
source/lz11.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef __LZ11_H__
#define __LZ11_H__
#include "types.h"
u8* compress_lz11(u8* input, u32 inputSize, u32* size);
#endif

121
source/main.cpp Normal file
View file

@ -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;
}

11
source/types.h Normal file
View file

@ -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