mirror of
https://github.com/usetrmnl/bb_epaper.git
synced 2026-04-29 13:43:26 -07:00
Enhanced image convert tool to accept all bit depths as input
This commit is contained in:
+101
-40
@@ -124,13 +124,74 @@ void AddHexBytes(FILE *f, void *pData, int iLen, int bLast)
|
||||
fprintf(f, "};\n");
|
||||
}
|
||||
} /* AddHexBytes() */
|
||||
//
|
||||
// The user passed a file with the wrong bit depth (not 1)
|
||||
// convert to 1-bpp by converting each color to 0 or 1 based on the gray level
|
||||
//
|
||||
void ConvertTo1Bpp(uint8_t *pBMP, int w, int h, int iBpp, uint8_t *palette)
|
||||
{
|
||||
int g, x, y, iDelta, iPitch, iDestPitch;
|
||||
uint8_t *s, *d, *pPal, u8, count;
|
||||
|
||||
iPitch = (w * iBpp)/8;
|
||||
iPitch = (iPitch + 3) & 0xfffc;
|
||||
iDestPitch = (w+7)/8;
|
||||
iDestPitch = (iDestPitch + 3) & 0xfffc; // round to nearest "DWORD"
|
||||
iDelta = iBpp/8;
|
||||
for (y=0; y<h; y++) {
|
||||
s = &pBMP[iPitch * y];
|
||||
d = &pBMP[iDestPitch * y]; // overwrite the original data as we change it
|
||||
count = 8; // bits in a byte
|
||||
u8 = 0; // start with all black
|
||||
for (x=0; x<w; x++) { // slower code, but less code :)
|
||||
u8 <<= 1;
|
||||
switch (iBpp) {
|
||||
case 24:
|
||||
case 32:
|
||||
g = (s[0] + s[1]*2 + s[2])/4; // gray value
|
||||
s += iDelta;
|
||||
break;
|
||||
case 16:
|
||||
g = s[1] & 0xf8; // red
|
||||
g += ((s[0] | s[1] << 8) << 2) & 0x1f0; // green x 2
|
||||
g += (s[0] << 3) & 0xf8; // blue
|
||||
g /= 4;
|
||||
s += 2;
|
||||
break;
|
||||
case 8:
|
||||
pPal = &palette[s[0] * 4];
|
||||
g = (pPal[0] + pPal[1]*2 + pPal[2])/4;
|
||||
s++;
|
||||
break;
|
||||
case 4:
|
||||
if (x & 1) {
|
||||
pPal = &palette[(s[0] & 0xf) * 4];
|
||||
g = (pPal[0] + pPal[1]*2 + pPal[2])/4;
|
||||
s++;
|
||||
} else {
|
||||
pPal = &palette[(s[0]>>4) * 4];
|
||||
g = (pPal[0] + pPal[1]*2 + pPal[2])/4;
|
||||
}
|
||||
break;
|
||||
} // switch on bpp
|
||||
if (g >= 128) u8 |= 1; // white
|
||||
count--;
|
||||
if (count == 0) { // byte is full, move on
|
||||
*d++ = u8;
|
||||
u8 = 0;
|
||||
count = 8;
|
||||
}
|
||||
} // for x
|
||||
} // for y
|
||||
} /* ConvertTo1Bpp() */
|
||||
|
||||
int main(int argc, const char * argv[]) {
|
||||
uint8_t *pBMP, *pOut;
|
||||
uint8_t *s, *pBMP, *pOut;
|
||||
int rc, w, h, y, bpp;
|
||||
int iOutSize, iPitch;
|
||||
G5ENCIMAGE g5enc;
|
||||
BB_BITMAP bbbm;
|
||||
uint8_t palette[1024];
|
||||
int bHFile; // flag indicating if the output will be a .H file of hex data
|
||||
|
||||
printf("Group5 image conversion tool\n");
|
||||
@@ -141,48 +202,48 @@ int main(int argc, const char * argv[]) {
|
||||
pOut = (uint8_t *)argv[2] + strlen(argv[2]) - 1;
|
||||
bHFile = (pOut[0] == 'H' || pOut[0] == 'h'); // output an H file?
|
||||
|
||||
pBMP = ReadBMP(argv[1], &w, &h, &bpp, NULL);
|
||||
if (bpp == 1) {
|
||||
uint8_t *s = pBMP;
|
||||
|
||||
printf("bmp size %d x %d\n", w, h);
|
||||
iPitch = (w+7) >> 3;
|
||||
pOut = (uint8_t *)malloc(iPitch * h);
|
||||
rc = g5_encode_init(&g5enc, w, h, pOut, iPitch * h);
|
||||
for (y=0; y<h && rc == G5_SUCCESS; y++) {
|
||||
rc = g5_encode_encodeLine(&g5enc, s);
|
||||
s += iPitch;
|
||||
}
|
||||
if (rc == G5_ENCODE_COMPLETE) {
|
||||
FILE *f;
|
||||
printf("Encode succeeded!\n");
|
||||
iOutSize = g5_encode_getOutSize(&g5enc);
|
||||
printf("Input size: %d bytes, output size: %d bytes\n", iPitch*h, iOutSize);
|
||||
printf("Compression ratio: %2.1f:1\n", (float)(iPitch*h) / (float)iOutSize);
|
||||
bbbm.u16Marker = BB_BITMAP_MARKER;
|
||||
bbbm.width = w;
|
||||
bbbm.height = h;
|
||||
bbbm.size = iOutSize;
|
||||
f = fopen(argv[2], "w+b");
|
||||
if (!f) {
|
||||
printf("Error opening: %s\n", argv[2]);
|
||||
} else {
|
||||
if (bHFile) { // generate HEX file to include in a project
|
||||
StartHexFile(f, iOutSize+sizeof(BB_BITMAP), w, h, argv[2]);
|
||||
AddHexBytes(f, &bbbm, sizeof(BB_BITMAP), 0);
|
||||
AddHexBytes(f, pOut, iOutSize, 1);
|
||||
} else { // generate a binary file
|
||||
fwrite(&bbbm, 1, sizeof(BB_BITMAP), f);
|
||||
fwrite(pOut, 1, iOutSize, f);
|
||||
}
|
||||
fflush(f);
|
||||
fclose(f);
|
||||
}
|
||||
pBMP = ReadBMP(argv[1], &w, &h, &bpp, palette);
|
||||
if (bpp != 1) { // need to convert it to 1-bpp
|
||||
printf("Converting from %d-bpp to 1-bpp\n", bpp);
|
||||
ConvertTo1Bpp(pBMP, w, h, bpp, palette);
|
||||
}
|
||||
s = pBMP;
|
||||
printf("Bitmap size: %d x %d\n", w, h);
|
||||
iPitch = (w+7) >> 3;
|
||||
pOut = (uint8_t *)malloc(iPitch * h);
|
||||
rc = g5_encode_init(&g5enc, w, h, pOut, iPitch * h);
|
||||
for (y=0; y<h && rc == G5_SUCCESS; y++) {
|
||||
rc = g5_encode_encodeLine(&g5enc, s);
|
||||
s += iPitch;
|
||||
}
|
||||
if (rc == G5_ENCODE_COMPLETE) {
|
||||
FILE *f;
|
||||
iOutSize = g5_encode_getOutSize(&g5enc);
|
||||
printf("Input data size: %d bytes, compressed size: %d bytes\n", iPitch*h, iOutSize);
|
||||
printf("Compression ratio: %2.1f:1\n", (float)(iPitch*h) / (float)iOutSize);
|
||||
bbbm.u16Marker = BB_BITMAP_MARKER;
|
||||
bbbm.width = w;
|
||||
bbbm.height = h;
|
||||
bbbm.size = iOutSize;
|
||||
f = fopen(argv[2], "w+b");
|
||||
if (!f) {
|
||||
printf("Error opening: %s\n", argv[2]);
|
||||
} else {
|
||||
printf("Error encoding image: %d\n", rc);
|
||||
if (bHFile) { // generate HEX file to include in a project
|
||||
StartHexFile(f, iOutSize+sizeof(BB_BITMAP), w, h, argv[2]);
|
||||
AddHexBytes(f, &bbbm, sizeof(BB_BITMAP), 0);
|
||||
AddHexBytes(f, pOut, iOutSize, 1);
|
||||
printf(".H file created successfully!\n");
|
||||
} else { // generate a binary file
|
||||
fwrite(&bbbm, 1, sizeof(BB_BITMAP), f);
|
||||
fwrite(pOut, 1, iOutSize, f);
|
||||
printf("Binary file created successfully!\n");
|
||||
}
|
||||
fflush(f);
|
||||
fclose(f);
|
||||
}
|
||||
} else {
|
||||
printf("Only 1-bpp images are supported.\n");
|
||||
printf("Error encoding image: %d\n", rc);
|
||||
}
|
||||
free(pBMP);
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user