dosgame1/converter.cpp

54 lines
1.6 KiB
C++

#include <cstdio>
#include "util/Bmp.h"
#include "util/Gbm.h"
void usage() {
printf("Usage: gbmconv <gbm output file> <source bitmap> <source mask bitmap>\n");
}
int main(int argc, char** argv) {
if (argc < 4) {
usage();
return 1;
}
char *gbmPath = argv[1];
char *bmpPath = argv[2];
char *maskBmpPath = argv[3];
auto bitmap = Bmp::loadFromFile(bmpPath);
if (bitmap.GetWidth() == 0 || bitmap.GetHeight() == 0) {
printf("Unable to load source bitmap\n");
return 1;
}
auto maskBitmap = Bmp::loadFromFile(maskBmpPath);
if (maskBitmap.GetWidth() == 0 || maskBitmap.GetHeight() == 0) {
printf("Unable to load mask bitmap\n");
return 1;
}
if (maskBitmap.GetWidth() != bitmap.GetWidth() || maskBitmap.GetHeight() != bitmap.GetHeight()) {
printf("Mask bitmap must be the same size as the source bitmap\n");
return 1;
}
// Create a combined bitmap where the most significant bit is a transparency bit
auto combined = Bitmap(bitmap.GetWidth(), bitmap.GetHeight());
for (auto y = 0u; y < bitmap.GetHeight(); y++) {
for (auto x = 0u; x < bitmap.GetWidth(); x++) {
auto isTransparent = (maskBitmap.GetPixel(x, y) == 0);
auto pv = (isTransparent ? 0x80 : 0x00) | (bitmap.GetPixel(x, y) & 0x7f);
combined.SetPixel(x, y, pv);
}
}
for (auto i = 0u; i < 256; i++) {
combined.SetPaletteEntry(i, bitmap.GetPaletteEntry(i));
}
Gbm::writeToFile(gbmPath, combined);
printf("Wrote %s\n", gbmPath);
return 0;
}