bradgrantham-apple2e/extractglyphs.cpp

74 lines
1.8 KiB
C++
Raw Normal View History

2016-11-18 19:33:50 +00:00
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <unistd.h>
#include <cmath>
#include <FreeImagePlus.h>
using namespace std;
// image_out and image_in have identical dimensions
2016-11-19 04:53:40 +00:00
void extract_glyph(fipImage &image_in, int g, unsigned char glyph[8][7])
2016-11-18 19:33:50 +00:00
{
int glyphs_across = image_in.getWidth() / 7;
int x = (g % glyphs_across) * 7;
int y = (g / glyphs_across) * 8; // XXX why invert in Y?
for(int j = 0; j < 8; j++) {
for(int i = 0; i < 7; i++) {
RGBQUAD result;
image_in.getPixelColor(x + i, image_in.getHeight() - 1 - (y + j), &result);
2016-11-19 04:53:40 +00:00
glyph[j][i] = (result.rgbRed > 127) ? 255 : 0;
2016-11-18 19:33:50 +00:00
}
}
}
int main(int argc, char **argv)
{
if(argc != 2) {
printf("usage: %s input\n", argv[0]);
exit(EXIT_FAILURE);
}
fipImage image_in;
bool success;
if (!(success = image_in.load(argv[1]))) {
cerr << "Failed to load image from " << argv[1] << endl;
exit(EXIT_FAILURE);
}
2016-11-19 04:53:40 +00:00
unsigned char font[96 * 8 * 7];
for(int k = 0; k <= 95; k++) {
unsigned char glyph[8][7];
extract_glyph(image_in, k, glyph);
2016-11-18 19:33:50 +00:00
for(int j = 0; j < 8; j++)
2016-11-19 04:53:40 +00:00
for(int i = 0; i < 7; i++) {
font[i + j * 7 + k * 7 * 8] = glyph[j][i];
}
}
2016-11-18 19:33:50 +00:00
2016-11-19 04:53:40 +00:00
printf("int font_offset = 32;\n");
printf("unsigned char font_bytes[96 * 7 * 8] = {\n");
for(int k = 0; k < 96; k++) {
2016-11-19 04:53:40 +00:00
int c = k + 32;
if(c == '\\')
printf(" // %d : backslash\n", c);
2016-11-18 19:33:50 +00:00
else
printf(" // %d : %c\n", c, c);
2016-11-19 04:53:40 +00:00
for(int j = 0; j < 8; j++) {
printf(" ");
for(int i = 0; i < 7; i++)
printf("0x%02X,", font[i + j * 7 + k * 7 * 8]);
printf("\n");
}
2016-11-18 19:33:50 +00:00
}
printf("};\n");
exit(EXIT_SUCCESS);
}