Major cleanup.

This commit is contained in:
Steveice10
2017-02-17 23:47:03 -08:00
parent 697b1308d8
commit fbac545897
14 changed files with 843 additions and 629 deletions

View File

@@ -1,12 +0,0 @@
#ifndef I_3DS_H
#define I_3DS_H
#include "cbmd.h"
#include "cwav.h"
#include "smdh.h"
#include "data.h"
#include "lz11.h"
#include "util.h"
#endif

View File

@@ -6,76 +6,78 @@
#include <string.h>
typedef struct {
char magic[4] = {'C', 'B', 'M', 'D'};
u32 zero = 0;
u32 cgfxOffsets[14] = {0};
u8 padding[0x44] = {0};
u32 cwavOffset = 0;
char magic[4];
u32 zero;
u32 cgfxOffsets[CBMD_NUM_CGFXS];
u8 padding[0x44];
u32 cwavOffset;
} CBMDHeader;
u8* cbmd_build_data(CBMD cbmd, u32* size, bool bnr) {
u32 headerSize = sizeof(CBMDHeader);
static void* cbmd_build_data(u32* size, CBMD cbmd, bool bnr) {
CBMDHeader header;
memset(&header, 0, sizeof(header));
u8* compressedCGFXs[14] = {NULL};
memcpy(header.magic, "CBMD", sizeof(header.magic));
u32 outputSize = sizeof(CBMDHeader);
void* compressedCGFXs[14] = {NULL};
u32 compressedCGFXSizes[14] = {0};
u32 offset = headerSize;
for(int i = 0; i < 14; i++) {
for(u32 i = 0; i < CBMD_NUM_CGFXS; i++) {
if(cbmd.cgfxs[i] != NULL) {
compressedCGFXs[i] = lz11_compress(cbmd.cgfxs[i], cbmd.cgfxSizes[i], &compressedCGFXSizes[i]);
header.cgfxOffsets[i] = offset;
offset += compressedCGFXSizes[i];
header.cgfxOffsets[i] = outputSize;
compressedCGFXs[i] = lz11_compress(&compressedCGFXSizes[i], cbmd.cgfxs[i], cbmd.cgfxSizes[i]);
outputSize += compressedCGFXSizes[i];
}
}
u32 pad = 0;
if(bnr) {
pad = 0x10 - (offset % 0x10);
offset += pad;
outputSize = (outputSize + 0xF) & ~0xF;
}
if(cbmd.cwav != NULL) {
header.cwavOffset = offset;
offset += cbmd.cwavSize;
header.cwavOffset = outputSize;
outputSize += cbmd.cwavSize;
}
u8* output = (u8*) malloc(offset);
u32 pos = 0;
void* output = calloc(outputSize, sizeof(u8));
if(output == NULL) {
for(u32 i = 0; i < CBMD_NUM_CGFXS; i++) {
if(cbmd.cgfxs[i] != NULL) {
free(compressedCGFXs[i]);
}
}
memcpy(output + pos, &header, headerSize);
pos += headerSize;
printf("ERROR: Could not allocate memory for CBMD data.\n");
return NULL;
}
for(int i = 0; i < 14; i++) {
memcpy(output, &header, sizeof(header));
for(u32 i = 0; i < CBMD_NUM_CGFXS; i++) {
if(compressedCGFXs[i] != NULL) {
memcpy(output + pos, compressedCGFXs[i], compressedCGFXSizes[i]);
memcpy(&((u8*) output)[header.cgfxOffsets[i]], compressedCGFXs[i], compressedCGFXSizes[i]);
free(compressedCGFXs[i]);
compressedCGFXs[i] = NULL;
pos += compressedCGFXSizes[i];
}
}
if(bnr) {
memset(output + pos, 0, pad);
pos += pad;
}
if(cbmd.cwav != NULL) {
memcpy(output + pos, cbmd.cwav, cbmd.cwavSize);
pos += cbmd.cwavSize;
memcpy(&((u8*) output)[header.cwavOffset], cbmd.cwav, cbmd.cwavSize);
}
if(size != NULL) {
*size = offset;
*size = outputSize;
}
return output;
}
u8* cbmd_build(CBMD cbmd, u32* size) {
return cbmd_build_data(cbmd, size, false);
void* cbmd_build(u32* size, CBMD cbmd) {
return cbmd_build_data(size, cbmd, false);
}
u8* bnr_build(CBMD cbmd, u32* size) {
return cbmd_build_data(cbmd, size, true);
void* bnr_build(u32* size, CBMD cbmd) {
return cbmd_build_data(size, cbmd, true);
}

View File

@@ -3,6 +3,8 @@
#include "../types.h"
#define CBMD_NUM_CGFXS 14
typedef enum {
CGFX_COMMON,
CGFX_EUR_ENGLISH,
@@ -21,13 +23,13 @@ typedef enum {
} CBMDCGFX;
typedef struct {
u8* cgfxs[14] = {NULL};
u32 cgfxSizes[14] = {0};
u8* cwav = NULL;
u32 cwavSize = 0;
void* cgfxs[CBMD_NUM_CGFXS];
u32 cgfxSizes[CBMD_NUM_CGFXS];
void* cwav;
u32 cwavSize;
} CBMD;
u8* cbmd_build(CBMD cbmd, u32* size);
u8* bnr_build(CBMD cbmd, u32* size);
void* cbmd_build(u32* size, CBMD cbmd);
void* bnr_build(u32* size, CBMD cbmd);
#endif

View File

@@ -3,118 +3,180 @@
#include <stdlib.h>
#include <string.h>
typedef enum {
PCM8,
PCM16,
TODO
} CWAVType;
enum {
CWAV_REF_DSP_ADPCM_INFO = 0x0300,
CWAV_REF_IMA_ADPCM_INFO = 0x0301,
CWAV_REF_SAMPLE_DATA = 0x1F00,
CWAV_REF_INFO_BLOCK = 0x7000,
CWAV_REF_DATA_BLOCK = 0x7001,
CWAV_REF_CHANNEL_INFO = 0x7100
};
typedef struct {
char magic[4] = {'C', 'W', 'A', 'V'};
u16 endianess = 0xFEFF;
u16 structLength = 0x40;
u32 unknown0 = 0;
u16 typeId;
u16 padding;
u32 offset;
} CWAVReference;
typedef struct {
CWAVReference ref;
u32 size;
} CWAVSizedReference;
typedef struct {
u32 count;
CWAVReference contents[0]; // Relative to beginning of CWAVReferenceTable.
} CWAVReferenceTable;
#define CWAV_MAGIC "CWAV"
enum {
CWAV_ENDIANNESS_LITTLE = 0xFEFF,
CWAV_ENDIANNESS_BIG = 0xFFFE
};
#define CWAV_VERSION 0x02010000
typedef struct {
char magic[4];
u16 endianness;
u16 headerSize;
u32 version;
u32 fileSize;
u32 numChunks = 2;
u32 infoChunkFlags = 0x7000;
u32 infoChunkOffset;
u32 infoChunkLength;
u32 dataChunkFlags = 0x7000;
u32 dataChunkOffset;
u32 dataChunkLength;
u8 reserved[0x14] = {0};
u16 numBlocks;
u16 reserved;
CWAVSizedReference infoBlock; // Relative to start of file.
CWAVSizedReference dataBlock; // Relative to start of file.
} CWAVHeader;
#define CWAV_BLOCK_MAGIC_INFO "INFO"
#define CWAV_BLOCK_MAGIC_DATA "DATA"
typedef struct {
char magic[4] = {'I', 'N', 'F', 'O'};
u32 length;
u32 type;
char magic[4];
u32 size;
} CWAVBlockHeader;
enum {
CWAV_ENCODING_PCM8 = 0,
CWAV_ENCODING_PCM16 = 1,
CWAV_ENCODING_DSP_ADPCM = 2,
CWAV_ENCODING_IMA_ADPCM = 3
};
typedef struct {
CWAVBlockHeader header;
u8 encoding;
bool loop;
u16 padding;
u32 sampleRate;
u32 unknown1 = 0;
u32 totalSamples;
u32 unknown2 = 0;
u32 totalChannels;
} CWAVInfoHeader;
u32 loopStartFrame;
u32 loopEndFrame;
u32 reserved;
CWAVReferenceTable channelInfos;
} CWAVInfoBlockHeader;
typedef struct {
u32 flags = 0x7100;
u32 offset;
} CWAVChannelDataPointer;
CWAVReference samples; // Relative to CWAVDataBlock.data
CWAVReference adpcmInfo; // Relative to beginning of CWAVChannelInfo.
u32 reserved;
} CWAVChannelInfo;
typedef struct {
u32 flags = 0x1F00;
u32 offset;
u32 unknown3 = 0;
u32 unknown4 = 0;
u32 padding = 0;
} CWAVChannelData;
u16 coefficients[16];
} CWAVDSPADPCMParam;
typedef struct {
char magic[4] = {'D', 'A', 'T', 'A'};
u32 length;
} CWAVDataHeader;
u8 predictorScale;
u8 reserved;
u16 previousSample;
u16 secondPreviousSample;
} CWAVDSPADPCMContext;
u8* cwav_build(CWAV cwav, u32* size) {
CWAVHeader header;
u32 offset = sizeof(CWAVHeader);
typedef struct {
CWAVDSPADPCMParam param;
CWAVDSPADPCMContext context;
CWAVDSPADPCMContext loopContext;
u16 padding;
} CWAVDSPADPCMInfo;
header.infoChunkOffset = offset;
header.infoChunkLength = (u32) sizeof(CWAVInfoHeader) + (u32) ((sizeof(CWAVChannelDataPointer) + sizeof(CWAVChannelData)) * cwav.channels);
offset += header.infoChunkLength;
typedef struct {
u16 data;
u8 tableIndex;
u8 padding;
} CWAVIMAADPCMContext;
header.dataChunkOffset = offset;
header.dataChunkLength = (u32) sizeof(CWAVDataHeader) + cwav.dataSize;
offset += header.dataChunkLength;
typedef struct {
CWAVIMAADPCMContext context;
CWAVIMAADPCMContext loopContext;
} CWAVIMAADPCMInfo;
header.fileSize = offset;
typedef struct {
CWAVBlockHeader header;
u8 data[0];
} CWAVDataBlock;
u8* output = (u8*) malloc(offset);
u32 pos = 0;
void* cwav_build(u32* size, CWAV cwav) {
u32 headerSize = (sizeof(CWAVHeader) + 0x1F) & ~0x1F;
u32 infoSize = ((sizeof(CWAVInfoBlockHeader) + (cwav.channels * (sizeof(CWAVReference) + sizeof(CWAVChannelInfo)))) + 0x1F) & ~0x1F;
u32 dataSize = ((sizeof(CWAVDataBlock) + 0x1F) & ~0x1F) + cwav.dataSize;
memcpy(output + pos, &header, sizeof(CWAVHeader));
pos += sizeof(CWAVHeader);
u32 outputSize = headerSize + infoSize + dataSize;
u32 bytesPerSample = (u32) cwav.bitsPerSample / 8;
CWAVInfoHeader infoHeader;
infoHeader.length = header.infoChunkLength;
infoHeader.type = cwav.bitsPerSample == 16 ? PCM16 : PCM8;
infoHeader.sampleRate = cwav.sampleRate;
infoHeader.totalSamples = cwav.dataSize / bytesPerSample / cwav.channels;
infoHeader.totalChannels = cwav.channels;
memcpy(output + pos, &infoHeader, sizeof(CWAVInfoHeader));
pos += sizeof(CWAVInfoHeader);
for(int i = 0; i < cwav.channels; i++) {
CWAVChannelDataPointer pointer;
pointer.offset = 0x4 + (cwav.channels * (u32) sizeof(CWAVChannelDataPointer)) + (i * (u32) sizeof(CWAVChannelData));
memcpy(output + pos, &pointer, sizeof(CWAVChannelDataPointer));
pos += sizeof(CWAVChannelDataPointer);
void* output = calloc(outputSize, sizeof(u8));
if(output == NULL) {
printf("ERROR: Could not allocate memory for CWAV data.\n");
return NULL;
}
u32 bytesPerChannel = cwav.dataSize / cwav.channels;
for(int i = 0; i < cwav.channels; i++) {
CWAVChannelData data;
data.offset = i * bytesPerChannel;
memcpy(output + pos, &data, sizeof(CWAVChannelData));
pos += sizeof(CWAVChannelData);
CWAVHeader* header = (CWAVHeader*) &((u8*) output)[0];
memcpy(header->magic, CWAV_MAGIC, sizeof(header->magic));
header->endianness = CWAV_ENDIANNESS_LITTLE;
header->headerSize = (u16) headerSize;
header->version = CWAV_VERSION;
header->fileSize = outputSize;
header->numBlocks = 2;
header->infoBlock.ref.typeId = CWAV_REF_INFO_BLOCK;
header->infoBlock.ref.offset = headerSize;
header->infoBlock.size = infoSize;
header->dataBlock.ref.typeId = CWAV_REF_DATA_BLOCK;
header->dataBlock.ref.offset = headerSize + infoSize;
header->dataBlock.size = dataSize;
CWAVInfoBlockHeader* infoBlockHeader = (CWAVInfoBlockHeader*) &((u8*) output)[headerSize];
memcpy(infoBlockHeader->header.magic, CWAV_BLOCK_MAGIC_INFO, sizeof(infoBlockHeader->header.magic));
infoBlockHeader->header.size = infoSize;
infoBlockHeader->encoding = cwav.bitsPerSample == 16 ? CWAV_ENCODING_PCM16 : CWAV_ENCODING_PCM8;
infoBlockHeader->loop = cwav.loop;
infoBlockHeader->sampleRate = cwav.sampleRate;
infoBlockHeader->loopStartFrame = cwav.loopStartFrame;
infoBlockHeader->loopEndFrame = cwav.loopEndFrame != 0 ? cwav.loopEndFrame : cwav.dataSize / cwav.channels / (cwav.bitsPerSample / 8);
infoBlockHeader->channelInfos.count = cwav.channels;
for(u32 c = 0; c < cwav.channels; c++) {
infoBlockHeader->channelInfos.contents[c].typeId = CWAV_REF_CHANNEL_INFO;
infoBlockHeader->channelInfos.contents[c].offset = sizeof(CWAVReferenceTable) + (cwav.channels * sizeof(CWAVReference)) + (c * sizeof(CWAVChannelInfo));
CWAVChannelInfo* info = (CWAVChannelInfo*) &((u8*) output)[headerSize + sizeof(CWAVInfoBlockHeader) + (cwav.channels * sizeof(CWAVReference)) + (c * sizeof(CWAVChannelInfo))];
info->samples.typeId = CWAV_REF_SAMPLE_DATA;
info->samples.offset = (((sizeof(CWAVDataBlock) + 0x1F) & ~0x1F) - sizeof(CWAVDataBlock)) + (c * (cwav.dataSize / cwav.channels));
info->adpcmInfo.typeId = 0;
info->adpcmInfo.offset = 0xFFFFFFFF;
}
CWAVDataHeader dataHeader;
dataHeader.length = header.dataChunkLength;
memcpy(output + pos, &dataHeader, sizeof(CWAVDataHeader));
pos += sizeof(CWAVDataHeader);
CWAVDataBlock* dataBlock = (CWAVDataBlock*) &((u8*) output)[headerSize + infoSize];
memcpy(dataBlock->header.magic, CWAV_BLOCK_MAGIC_DATA, sizeof(dataBlock->header.magic));
dataBlock->header.size = dataSize;
for(int i = 0; i < cwav.dataSize; i += cwav.channels * bytesPerSample) {
for(int c = 0; c < cwav.channels; c++) {
memcpy(output + pos + (bytesPerChannel * c) + (i / cwav.channels), cwav.data + i + (c * bytesPerSample), bytesPerSample);
for(u32 i = 0; i < cwav.dataSize; i += cwav.channels * (cwav.bitsPerSample / 8)) {
for(u32 c = 0; c < cwav.channels; c++) {
CWAVChannelInfo* info = (CWAVChannelInfo*) &((u8*) output)[headerSize + sizeof(CWAVInfoBlockHeader) + (cwav.channels * sizeof(CWAVReference)) + (c * sizeof(CWAVChannelInfo))];
memcpy(&dataBlock->data[info->samples.offset + (i / cwav.channels)], &((u8*) cwav.data)[i + (c * (cwav.bitsPerSample / 8))], cwav.bitsPerSample / 8);
}
}
pos += cwav.dataSize;
if(size != NULL) {
*size = pos;
*size = outputSize;
}
return output;

View File

@@ -7,10 +7,15 @@ typedef struct {
u32 channels;
u32 sampleRate;
u32 bitsPerSample;
bool loop;
u32 loopStartFrame;
u32 loopEndFrame;
u32 dataSize;
u8* data;
void* data;
} CWAV;
u8* cwav_build(CWAV wav, u32* size);
void* cwav_build(u32* size, CWAV wav);
#endif

View File

@@ -1,6 +1,5 @@
#include "lz11.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@@ -46,8 +45,8 @@ int lz11_get_occurence_length(u8* newPtr, int newLength, u8* oldPtr, int oldLeng
return maxLength;
}
u8* lz11_compress(u8* input, u32 inputSize, u32* size) {
if (inputSize > 0xFFFFFF) {
void* lz11_compress(u32* size, void* input, u32 inputSize) {
if(inputSize > 0xFFFFFF) {
printf("ERROR: LZ11 input is too large.\n");
return NULL;
}
@@ -74,9 +73,9 @@ u8* lz11_compress(u8* input, u32 inputSize, u32* size) {
int disp = 0;
int oldLength = MIN(readBytes, 0x1000);
int length = lz11_get_occurence_length(input + readBytes, MIN(inputSize - readBytes, 0x10110), input + readBytes - oldLength, oldLength, &disp);
int length = lz11_get_occurence_length((u8*) input + readBytes, MIN(inputSize - readBytes, 0x10110), (u8*) input + readBytes - oldLength, oldLength, &disp);
if(length < 3) {
outbuffer[bufferlength++] = *(input + (readBytes++));
outbuffer[bufferlength++] = *((u8*) input + (readBytes++));
} else {
readBytes += length;
outbuffer[0] |= (u8)(1 << (7 - bufferedBlocks));
@@ -119,9 +118,12 @@ u8* lz11_compress(u8* input, u32 inputSize, u32* size) {
compressedLength += padLength;
}
u8* buf = (u8*) malloc((size_t) compressedLength);
void* buf = malloc((size_t) compressedLength);
ss.read((char*) buf, compressedLength);
*size = (u32) compressedLength;
if(size != NULL) {
*size = (u32) compressedLength;
}
return buf;
}

View File

@@ -3,6 +3,6 @@
#include "../types.h"
u8* lz11_compress(u8* input, u32 inputSize, u32* size);
void* lz11_compress(u32* size, void* input, u32 inputSize);
#endif

View File

@@ -3,87 +3,94 @@
#include "../types.h"
typedef enum {
JAPANESE,
ENGLISH,
FRENCH,
GERMAN,
ITALIAN,
SPANISH,
SIMPLIFIED_CHINESE,
KOREAN,
DUTCH,
PORTUGESE,
RUSSIAN,
TRADITIONAL_CHINESE
} SMDHTitleLanguage;
#define SMDH_NUM_LANGUAGE_SLOTS 16
#define SMDH_NUM_VALID_LANGUAGE_SLOTS 12
// TODO: Provide values to set ratings to.
typedef enum {
CERO = 0,
ESRB = 1,
USK = 3,
PEGI_GEN = 4,
PEGI_PTR = 6,
PEGI_BBFC = 7,
COB = 8,
GRB = 9,
CGSRR = 10
} SMDHGameRating;
#define SMDH_NUM_RATING_SLOTS 16
#define SMDH_SMALL_ICON_SIZE 24
#define SMDH_LARGE_ICON_SIZE 48
typedef enum {
JAPAN = 0x01,
NORTH_AMERICA = 0x02,
EUROPE = 0x04,
AUSTRALIA = 0x08,
CHINA = 0x10,
KOREA = 0x20,
TAIWAN = 0x40,
SMDH_LANGUAGE_JAPANESE,
SMDH_LANGUAGE_ENGLISH,
SMDH_LANGUAGE_FRENCH,
SMDH_LANGUAGE_GERMAN,
SMDH_LANGUAGE_ITALIAN,
SMDH_LANGUAGE_SPANISH,
SMDH_LANGUAGE_SIMPLIFIED_CHINESE,
SMDH_LANGUAGE_KOREAN,
SMDH_LANGUAGE_DUTCH,
SMDH_LANGUAGE_PORTUGUESE,
SMDH_LANGUAGE_RUSSIAN,
SMDH_LANGUAGE_TRADITIONAL_CHINESE
} SMDHLanguage;
typedef enum {
SMDH_RATING_CERO = 0,
SMDH_RATING_ESRB = 1,
SMDH_RATING_USK = 3,
SMDH_RATING_PEGI_GEN = 4,
SMDH_RATING_PEGI_PTR = 6,
SMDH_RATING_PEGI_BBFC = 7,
SMDH_RATING_COB = 8,
SMDH_RATING_GRB = 9,
SMDH_RATING_CGSRR = 10
} SMDHRating;
typedef enum {
SMDH_REGION_JAPAN = 0x01,
SMDH_REGION_NORTH_AMERICA = 0x02,
SMDH_REGION_EUROPE = 0x04,
SMDH_REGION_AUSTRALIA = 0x08,
SMDH_REGION_CHINA = 0x10,
SMDH_REGION_KOREA = 0x20,
SMDH_REGION_TAIWAN = 0x40,
// Not a bitmask, but a value.
REGION_FREE = 0x7FFFFFFF
} SMDHRegionFlag;
SMDH_REGION_FREE = 0x7FFFFFFF
} SMDHRegion;
typedef enum {
VISIBLE = 0x0001,
AUTO_BOOT = 0x0002,
ALLOW_3D = 0x0004,
REQUIRE_EULA = 0x0008,
AUTO_SAVE_ON_EXIT = 0x0010,
USE_EXTENDED_BANNER = 0x0020,
RATING_REQUIED = 0x0040,
USE_SAVE_DATA = 0x0080,
RECORD_USAGE = 0x0100,
DISABLE_SAVE_BACKUPS = 0x0400,
NEW_3DS = 0x1000
SMDH_FLAG_VISIBLE = 0x0001,
SMDH_FLAG_AUTO_BOOT = 0x0002,
SMDH_FLAG_ALLOW_3D = 0x0004,
SMDH_FLAG_REQUIRE_EULA = 0x0008,
SMDH_FLAG_AUTO_SAVE_ON_EXIT = 0x0010,
SMDH_FLAG_USE_EXTENDED_BANNER = 0x0020,
SMDH_FLAG_RATING_REQUIED = 0x0040,
SMDH_FLAG_USE_SAVE_DATA = 0x0080,
SMDH_FLAG_RECORD_USAGE = 0x0100,
SMDH_FLAG_DISABLE_SAVE_BACKUPS = 0x0400,
SMDH_FLAG_NEW_3DS = 0x1000
} SMDHFlag;
typedef struct {
u16 shortTitle[0x40] = {0};
u16 longTitle[0x80] = {0};
u16 publisher[0x40] = {0};
u16 shortTitle[0x40];
u16 longTitle[0x80];
u16 publisher[0x40];
} SMDHTitle;
typedef struct {
u8 gameRatings[0x10] = {0};
u32 regionLock = REGION_FREE;
u8 matchMakerId[0xC] = {0};
u32 flags = 0;
u16 eulaVersion = 0;
u16 reserved1 = 0;
u32 optimalBannerFrame = 0;
u32 streetpassId = 0;
u8 gameRatings[SMDH_NUM_RATING_SLOTS];
u32 regionLock;
u8 matchMakerId[0xC];
u32 flags;
u16 eulaVersion;
u16 reserved1;
u32 optimalBannerFrame;
u32 streetpassId;
} SMDHSettings;
typedef struct {
char magic[4] = {'S', 'M', 'D', 'H'};
u16 version = 0;
u16 reserved0 = 0;
SMDHTitle titles[0x10];
char magic[4];
u16 version;
u16 reserved0;
SMDHTitle titles[SMDH_NUM_LANGUAGE_SLOTS];
SMDHSettings settings;
u64 reserved2 = 0;
u8 smallIcon[0x480] = {0};
u8 largeIcon[0x1200] = {0};
u64 reserved1;
u16 smallIcon[SMDH_SMALL_ICON_SIZE * SMDH_SMALL_ICON_SIZE];
u16 largeIcon[SMDH_LARGE_ICON_SIZE * SMDH_LARGE_ICON_SIZE];
} SMDH;
#endif

View File

@@ -1,110 +0,0 @@
#include "util.h"
#include "../pc/stb_image.h"
#include <stdlib.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 };
void utf8_to_utf16(u16* dst, const char* src, size_t max_len) {
size_t n = 0;
while(src[n]) {
dst[n] = (u16) src[n];
if(n++ >= max_len) {
return;
}
}
}
u16 pack_color(u8 r, u8 g, u8 b, u8 a, PixelFormat format) {
if(format == RGB565) {
float alpha = a / 255.0f;
r = (u8) (r * alpha) >> 3;
g = (u8) (g * alpha) >> 2;
b = (u8) (b * alpha) >> 3;
return (r << 11) | (g << 5) | b;
} else if(format == RGBA4444) {
r >>= 4;
g >>= 4;
b >>= 4;
a >>= 4;
return r << 12 | g << 8 | b << 4 | a;
}
return 0;
}
u8* load_image(const char* image, u32 width, u32 height) {
unsigned char *img;
int imgWidth, imgHeight, imgDepth;
img = stbi_load(image, &imgWidth, &imgHeight, &imgDepth, STBI_rgb_alpha);
if(img == NULL) {
printf("ERROR: Could not load image file: %s.\n", stbi_failure_reason());
return NULL;
}
if(width == 0) {
width = imgWidth;
}
if(height == 0) {
height = imgHeight;
}
if(imgWidth != width || imgHeight != height) {
printf("ERROR: Image must be exactly %d x %d in size.\n", width, height);
stbi_image_free(img);
return NULL;
}
if(imgDepth != STBI_rgb_alpha) {
printf("ERROR: Decoded image does't match expected format (%d, wanted %d).\n",
imgDepth, STBI_rgb_alpha);
stbi_image_free(img);
return NULL;
}
return img;
}
void free_image(u8* img) {
stbi_image_free(img);
}
u16* image_data_to_tiles(u8* img, u32 width, u32 height, PixelFormat format, u32* size) {
u16* converted = (u16*) malloc(width * height * sizeof(u16));
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++] = pack_color(pixel[0], pixel[1], pixel[2], pixel[3], format);
}
}
}
if(size != NULL) {
*size = width * height * (u32) sizeof(u16);
}
return converted;
}
u16* image_to_tiles(const char* image, u32 width, u32 height, PixelFormat format, u32* size) {
u8* img = load_image(image, width, height);
if(img == NULL) {
return NULL;
}
u16* tiles = image_data_to_tiles(img, width, height, format, size);
free_image(img);
return tiles;
}

View File

@@ -1,17 +0,0 @@
#ifndef UTIL_H
#define UTIL_H
#include "../types.h"
typedef enum {
RGB565,
RGBA4444
} PixelFormat;
void utf8_to_utf16(u16* dst, const char* src, size_t max_len);
u16 pack_color(u8 r, u8 g, u8 b, u8 a, PixelFormat format);
u8* load_image(const char* image, u32 width, u32 height);
u16* image_data_to_tiles(u8* img, u32 width, u32 height, PixelFormat format, u32* size);
u16* image_to_tiles(const char* image, u32 width, u32 height, PixelFormat format, u32* size);
#endif