From e15ebbe3d0737956e78c0e7d0d9444dc4168a03d Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Tue, 15 Sep 2020 23:59:53 +0200 Subject: [PATCH] ws: generate only needed version and add option to save conversion to file --- client/src/cmdhfwaveshare.c | 213 ++++++++++++++++++++++-------------- 1 file changed, 129 insertions(+), 84 deletions(-) diff --git a/client/src/cmdhfwaveshare.c b/client/src/cmdhfwaveshare.c index ad260e988..0483af94c 100644 --- a/client/src/cmdhfwaveshare.c +++ b/client/src/cmdhfwaveshare.c @@ -78,24 +78,25 @@ typedef enum { } model_enum_t; static model_t models[] = { - {"2.13 inch e-paper", 16, 122, 250}, // tested - {"2.9 inch e-paper", 16, 296, 128}, - {"4.2 inch e-paper", 100, 400, 300}, // tested - {"7.5 inch e-paper", 120, 800, 480}, - {"2.7 inch e-paper", 121, 276, 176}, - {"2.13 inch e-paper B", 106, 212, 104}, - {"1.54 inch e-paper B", 100, 200, 200}, - {"7.5 inch e-paper HD", 120, 880, 528}, + {"2.13 inch e-paper", 16, 122, 250}, // tested + {"2.9 inch e-paper", 16, 296, 128}, + {"4.2 inch e-paper", 100, 400, 300}, // tested + {"7.5 inch e-paper", 120, 800, 480}, + {"2.7 inch e-paper", 121, 276, 176}, + {"2.13 inch e-paper B (with red)", 106, 212, 104}, + {"1.54 inch e-paper B (with red)", 100, 200, 200}, + {"7.5 inch e-paper HD", 120, 880, 528}, }; static int CmdHelp(const char *Cmd); static int usage_hf_waveshare_loadbmp(void) { PrintAndLogEx(NORMAL, "Load BMP file to Waveshare NFC ePaper."); - PrintAndLogEx(NORMAL, "Usage: hf waveshare loadbmp [h] f m "); + PrintAndLogEx(NORMAL, "Usage: hf waveshare loadbmp [h] f m [s]"); PrintAndLogEx(NORMAL, " Options :"); PrintAndLogEx(NORMAL, " f : " _YELLOW_("filename[.bmp]") " to upload to tag"); PrintAndLogEx(NORMAL, " m : " _YELLOW_("model number") " of your tag"); + PrintAndLogEx(NORMAL, " s : save dithered version in filename-[n].bmp, only for RGB BMP"); for (uint8_t i=0; i< MEND; i++) { PrintAndLogEx(NORMAL, " m %2i : %s", i, models[i].desc); } @@ -349,7 +350,7 @@ static void map8to1(uint8_t *colormap, uint16_t width, uint16_t height, uint8_t } } -static int read_bmp_rgb(const uint8_t *bmp, const size_t bmpsize, uint8_t **black, uint8_t **black_minus_red, uint8_t **red) { +static int read_bmp_rgb(uint8_t *bmp, const size_t bmpsize, uint8_t model_nr, uint8_t **black, uint8_t **black_minus_red, uint8_t **red, char *filename, bool save_conversions) { BMP_HEADER *pbmpheader = (BMP_HEADER *)bmp; // check file is full color if (pbmpheader->bpp != 24) { @@ -397,88 +398,128 @@ static int read_bmp_rgb(const uint8_t *bmp, const size_t bmpsize, uint8_t **blac offset+=width%4; } - // for BW-only screens: - int16_t *chanGrey = calloc(width*height, sizeof(int16_t)); - if (chanGrey == NULL) { - free(chanR); - free(chanG); - free(chanB); - return PM3_EMALLOC; - } - rgb_to_gray(chanR, chanG, chanB, width, height, chanGrey); - dither_chan_inplace(chanGrey, width, height); + if ((model_nr == M1in54B) || (model_nr == M2in13B)) { + // for BW+Red screens: + uint8_t *mapBlackMinusRed = calloc(width*height, sizeof(uint8_t)); + if (mapBlackMinusRed == NULL) { + free(chanR); + free(chanG); + free(chanB); + return PM3_EMALLOC; + } + uint8_t *mapRed = calloc(width*height, sizeof(uint8_t)); + if (mapRed == NULL) { + free(chanR); + free(chanG); + free(chanB); + free(mapBlackMinusRed); + return PM3_EMALLOC; + } + rgb_to_gray_red_inplace(chanR, chanG, chanB, width, height); - uint8_t *mapBlack = calloc(width*height, sizeof(uint8_t)); - if (mapBlack == NULL) { + uint8_t palette[] ={0x00,0x00,0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00}; // black, white, red + dither_rgb_inplace(chanR, chanG, chanB, width, height, palette, sizeof(palette)/3); + + threshold_rgb_black_red(chanR, chanG, chanB, width, height, 128, 128, mapBlackMinusRed, mapRed); + if (save_conversions) { + // fill BMP chans + offset = pbmpheader->offset; + for (uint16_t Y=0; Yoffset; + for (uint16_t Y=0; Y