Skip to content

Commit

Permalink
XPK: For XPKs with multiple entries, Decode all individual frames
Browse files Browse the repository at this point in the history
We prepend the output file's name with the number of the frame.

Fixes #46
  • Loading branch information
dressupgeekout committed Jul 20, 2020
1 parent e8c2277 commit 178953b
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions src/xpk/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,35 +138,56 @@ main(int argc, char *argv[])
XPKEntry_Pretty(XPK_EntryAtIndex(xpk, i));
}

/* We can only decode the 0th entry for now. */
XPKEntry *entry = XPK_EntryAtIndex(xpk, 0);
for (int i = 0; i < xpk->n_entries; i++) {
XPKEntry *entry = XPK_EntryAtIndex(xpk, i);

rgba = XPKEntry_Decode(entry, &status);
rgba = XPKEntry_Decode(entry, &status);

if (status != LUNAPURPURA_OK) {
LPWarn(LP_SUBSYSTEM_XPK, "couldn't decode: %s", LPStatusString(status));
goto fail;
}
if (status != LUNAPURPURA_OK) {
LPWarn(LP_SUBSYSTEM_XPK, "couldn't decode: %s", LPStatusString(status));
goto fail;
}

if (!(entry->width && entry->height)) {
LPWarn(LP_SUBSYSTEM_XPK, "skipping entry %d with zero width and/or height", i);
continue;
}

char real_out_path[512];

if (out_path) {
if (xpk->n_entries > 1) {
/*
* The number of XPK entries is a uint16, which means the largest
* possible frame number is 65535, which is 5 decimal digits long.
*/
snprintf(real_out_path, sizeof(real_out_path), "%05d-%s", i, out_path);
} else {
snprintf(real_out_path, sizeof(real_out_path), "%s", out_path);
}
LPWarn(LP_SUBSYSTEM_XPK, "decoding entry #%d to %s", i, real_out_path);
}

#ifdef LUNAPURPURA_PNG_SUPPORT
if (out_path && !want_png)
if (out_path && !want_png)
#else
if (out_path)
if (out_path)
#endif
{
FILE *out_f = fopen(out_path, "wb");
fwrite(rgba, 4, entry->width*entry->height, out_f);
fclose(out_f);
}
{
FILE *out_f = fopen(real_out_path, "wb");
fwrite(rgba, 4, entry->width*entry->height, out_f);
fclose(out_f);
}

#ifdef LUNAPURPURA_PNG_SUPPORT
if (out_path && want_png) {
if ((status = XPKDecoder_RGBAToPNG(rgba, entry, out_path)) != LUNAPURPURA_OK) {
LPWarn(LP_SUBSYSTEM_XPK, "couldn't write PNG!: %s", LPStatusString(status));
goto fail;
if (out_path && want_png) {
if ((status = XPKDecoder_RGBAToPNG(rgba, entry, real_out_path)) != LUNAPURPURA_OK) {
LPWarn(LP_SUBSYSTEM_XPK, "couldn't write PNG!: %s", LPStatusString(status));
goto fail;
}
}
}
#endif
}
}

XPKDecoder_FreeRGBA(rgba);
Expand Down

0 comments on commit 178953b

Please sign in to comment.