Merge pull request #3 from gravgun/master

Replace lodepng by stb_image; add Vorbis banner sound import
This commit is contained in:
Steven Smith 2016-01-24 09:08:21 -08:00
commit 10094fc4da
11 changed files with 12154 additions and 7832 deletions

View file

@ -3,5 +3,5 @@ project(bannertool)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES source/main.cpp source/cmd.cpp source/pc/wav.cpp source/3ds/cbmd.cpp source/3ds/cwav.cpp source/3ds/lz11.cpp source/3ds/util.cpp source/pc/lodepng.cpp)
set(SOURCE_FILES source/main.cpp source/cmd.cpp source/pc/wav.cpp source/3ds/cbmd.cpp source/3ds/cwav.cpp source/3ds/lz11.cpp source/3ds/util.cpp source/pc/stb_image.c source/pc/stb_vorbis.c)
add_executable(bannertool ${SOURCE_FILES})

View file

@ -1,6 +1,6 @@
#include "util.h"
#include "../pc/lodepng.h"
#include "../pc/stb_image.h"
#include <stdlib.h>
@ -39,9 +39,12 @@ u16 pack_color(u8 r, u8 g, u8 b, u8 a, PixelFormat format) {
u8* load_image(const char* image, u32 width, u32 height) {
unsigned char *img;
unsigned int imgWidth, imgHeight;
if(lodepng_decode32_file(&img, &imgWidth, &imgHeight, image)) {
printf("ERROR: Could not load png file.\n");
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;
}
@ -58,9 +61,19 @@ u8* load_image(const char* image, u32 width, u32 height) {
return NULL;
}
if(imgDepth != STBI_rgb_alpha) {
printf("ERROR: Decoded image does't match expected format (%d, wanted %d).\n",
imgDepth, STBI_rgb_alpha);
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;
@ -89,5 +102,7 @@ u16* image_to_tiles(const char* image, u32 width, u32 height, PixelFormat format
return NULL;
}
return image_data_to_tiles(img, width, height, format, size);
u16* tiles = image_data_to_tiles(img, width, height, format, size);
free_image(img);
return tiles;
}

View file

@ -2,6 +2,7 @@
#include "3ds/3ds.h"
#include "pc/wav.h"
#include "pc/stb_vorbis.h"
#include "types.h"
#include <errno.h>
@ -24,26 +25,59 @@ u8* convert_to_cgfx(const std::string image, u32 width, u32 height, u32* size) {
memcpy(ret + BANNER_CGFX_HEADER_LENGTH, converted, convertedSize);
*size = BANNER_CGFX_HEADER_LENGTH + convertedSize;
free(converted);
return ret;
}
u8* convert_to_cwav(const std::string file, u32* size) {
WAV* wav = wav_read(file.c_str());
if(wav == NULL) {
return NULL;
u8* ret = NULL;
// Determine what file type we have
FILE* fd = fopen(file.c_str(), "rb");
char magic[4];
fread(magic, sizeof(magic), 1, fd);
rewind(fd); // equivalent to SEEK_SET to pos 0
if (magic[0] == 'R' && magic[1] == 'I' && magic[2] == 'F' && magic[3] == 'F') {
WAV* wav = wav_read(fd);
if(wav != NULL) {
CWAV cwav;
cwav.channels = wav->format.numChannels;
cwav.sampleRate = wav->format.sampleRate;
cwav.bitsPerSample = wav->format.bitsPerSample;
cwav.dataSize = wav->data.chunkSize;
cwav.data = wav->data.data;
ret = cwav_build(cwav, size);
wav_free(wav);
}
} else if (magic[0] == 'O' && magic[1] == 'g' && magic[2] == 'g' && magic[3] == 'S') {
int error;
stb_vorbis* vorb = stb_vorbis_open_file(fd, false, &error, NULL);
if(vorb != NULL) {
stb_vorbis_info info = stb_vorbis_get_info(vorb);
CWAV cwav;
cwav.channels = info.channels;
cwav.sampleRate = info.sample_rate;
cwav.bitsPerSample = 16; // stb_vorbis always outputs 16 bit samples
int sampleCount = stb_vorbis_stream_length_in_samples(vorb) * info.channels;
cwav.dataSize = sampleCount * 2;
cwav.data = (u8*) calloc(sampleCount, 2);
stb_vorbis_get_samples_short_interleaved(vorb, info.channels, (short*) cwav.data, sampleCount);
ret = cwav_build(cwav, size);
free(cwav.data);
stb_vorbis_close(vorb);
} else {
printf("ERROR: Vorbis open failed, error %d.\n", error);
}
} else {
printf("ERROR: Audio file header '%c%c%c%c' unrecognized.\n", magic[0], magic[1], magic[2], magic[3]);
}
CWAV cwav;
cwav.channels = wav->format.numChannels;
cwav.sampleRate = wav->format.sampleRate;
cwav.bitsPerSample = wav->format.bitsPerSample;
cwav.dataSize = wav->data.chunkSize;
cwav.data = wav->data.data;
u8* ret = cwav_build(cwav, size);
wav_free(wav);
fclose(fd);
return ret;
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

3
source/pc/stb_image.c Normal file
View file

@ -0,0 +1,3 @@
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_PNG
#include "stb_image.h"

6614
source/pc/stb_image.h Normal file

File diff suppressed because it is too large Load diff

5462
source/pc/stb_vorbis.c Normal file

File diff suppressed because it is too large Load diff

2
source/pc/stb_vorbis.h Normal file
View file

@ -0,0 +1,2 @@
#define STB_VORBIS_HEADER_ONLY
#include "stb_vorbis.c"

View file

@ -1,6 +1,5 @@
#include "wav.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
@ -18,8 +17,7 @@ bool wav_find_chunk(FILE* fd, const char* magic) {
return true;
}
WAV* wav_read(const char* file) {
FILE* fd = fopen(file, "r");
WAV* wav_read(FILE* fd) {
if(!fd) {
printf("ERROR: Could not open WAV file: %s\n", strerror(errno));
return NULL;
@ -52,8 +50,6 @@ WAV* wav_read(const char* file) {
data.data = (u8*) malloc(data.chunkSize);
fread(data.data, 1, data.chunkSize, fd);
fclose(fd);
WAV* wav = (WAV*) malloc(sizeof(WAV));
wav->riff = riff;
wav->format = format;

View file

@ -1,6 +1,8 @@
#ifndef __WAV_H__
#define __WAV_H__
#include <stdio.h>
#include "../types.h"
typedef struct {
@ -32,7 +34,7 @@ typedef struct {
Data data;
} WAV;
WAV* wav_read(const char* file);
WAV* wav_read(FILE* fd);
void wav_free(WAV* wav);
#endif