Skip to content

Commit

Permalink
thcrap_tasofro: add png->bmp conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
brliron committed Nov 4, 2017
1 parent af239e3 commit af8e3f8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
76 changes: 75 additions & 1 deletion thcrap_tasofro/src/nsml.cpp
Expand Up @@ -12,10 +12,16 @@
#include "thcrap_tasofro.h"

int patch_cv2(void *file_inout, size_t size_out, size_t size_in, const char *fn, json_t*);
int patch_bmp(void *file_inout, size_t size_out, size_t size_in, const char *fn, json_t*);

int nsml_init()
{
patchhook_register("*.cv2", patch_cv2);
if (game_id == TH_MEGAMARI) {
patchhook_register("*.bmp", patch_bmp);
}
if (game_id == TH_NSML) {
patchhook_register("*.cv2", patch_cv2);
}
return 0;
}

Expand Down Expand Up @@ -65,6 +71,74 @@ int patch_cv2(void *file_inout, size_t size_out, size_t size_in, const char *fn,
return 1;
}

int patch_bmp(void *file_inout, size_t size_out, size_t size_in, const char *fn, json_t*)
{
VLA(char, fn_png, strlen(fn) + 1);
strcpy(fn_png, fn);
strcpy(PathFindExtensionA(fn_png), ".png");

uint32_t width, height, rowbytes;
uint8_t bpp;
BYTE** row_pointers = png_image_read(fn_png, &width, &height, &bpp);
VLA_FREE(fn_png);

if (!row_pointers) {
return 0;
}

rowbytes = width * bpp / 8;
if (rowbytes % 4 != 0) {
rowbytes += 4 - (rowbytes % 4);
}
if (size_out < sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + rowbytes * height) {
log_print("Destination buffer too small!\n");
free(row_pointers);
return -1;
}

BITMAPFILEHEADER *bpFile = (BITMAPFILEHEADER*)file_inout;
BITMAPINFOHEADER *bpInfo = (BITMAPINFOHEADER*)(bpFile + 1);
BYTE *bpData = (BYTE*)(bpInfo + 1);
bpFile->bfType = 0x4D42;
bpFile->bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + rowbytes * height;
bpFile->bfReserved1 = 0;
bpFile->bfReserved2 = 0;
bpFile->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

bpInfo->biSize = sizeof(BITMAPINFOHEADER);
bpInfo->biWidth = width;
bpInfo->biHeight = height;
bpInfo->biPlanes = 1;
bpInfo->biBitCount = bpp;
bpInfo->biCompression = (bpp == 32 ? BI_BITFIELDS : BI_RGB);
bpInfo->biSizeImage = rowbytes * height;
bpInfo->biXPelsPerMeter = 65535;
bpInfo->biYPelsPerMeter = 65535;
bpInfo->biClrUsed = 0;
bpInfo->biClrImportant = 0;

for (unsigned int h = 0; h < height; h++) {
unsigned int pos = 0;
for (unsigned int w = 0; w < width; w++) {
bpData[h * rowbytes + pos + 0] = row_pointers[height - h - 1][w * bpp / 8 + 2];
bpData[h * rowbytes + pos + 1] = row_pointers[height - h - 1][w * bpp / 8 + 1];
bpData[h * rowbytes + pos + 2] = row_pointers[height - h - 1][w * bpp / 8 + 0];
if (bpp == 32) {
bpData[h * rowbytes + pos + 3] = row_pointers[height - h - 1][w * bpp / 8 + 3];
}
pos += bpp / 8;
}
// Padding
while (pos % 4) {
bpData[h * rowbytes + pos] = 0;
pos++;
}
}

free(row_pointers);
return 1;
}

int BP_nsml_file_header(x86_reg_t *regs, json_t *bp_info)
{
// Parameters
Expand Down
5 changes: 4 additions & 1 deletion thcrap_tasofro/src/thcrap_tasofro.h
Expand Up @@ -14,10 +14,13 @@
typedef enum {
TH_NONE,

// • Doesn't share any code with the other formats
// • nsml engine
TH_MEGAMARI,

// • Use a slightly different XORing
TH_NSML,

// • th135 engine
// • filename hash: uses hash = ch ^ 0x1000193 * hash
// • spells: using data/csv/Item*.csv
// • spells: data/csv/story/*/*.csv has columns for popularity
Expand Down

0 comments on commit af8e3f8

Please sign in to comment.