bitbank2 / JPEGDEC

An optimized JPEG decoder for Arduino
Apache License 2.0
365 stars 47 forks source link

Could not yet implement decodeDither using setPixelType(FOUR_BIT_DITHERED) #29

Closed martinberlin closed 2 years ago

martinberlin commented 2 years ago

My time measuring show that as you claim JPEGDEC is twice as fast as tjpgd Trying this with a parallel epaper that is 960*540 pixels and 4 bit per pixel (16 grays)

JPEGDEC 227592 bytes read from http://img.cale.es/jpg/fasani/5e636b0f39aac decode: 478 ms - 960x540 image

Same image with tjpgd decode: 1028 ms . image decompression

However it's being hard to understand why jpeg.decodeDither fails always with: Failed with error: 2 (decode error)

Following #26 I increased the buffer in JPEGDEC.h

//960 * 16
#define JPEG_FILE_BUF_SIZE 15360

But still everything breaks just by setting this line after openRAM (Using RAM directly since the image is downloaded instead of being a file)

if (jpeg.openRAM(source_buf, img_buf_pos, JPEGDraw)) {
    // Enabling this says always decode error 2 even if I made the JPEG_FILE_BUF_SIZE big enough
    jpeg.setPixelType(FOUR_BIT_DITHERED); // Commenting it works

    if (jpeg.decodeDither(ditherSpace, 0))
      {
        time_decomp = (esp_timer_get_time() - decode_start)/1000 - time_render;
        ESP_LOGI("decode", "%d ms - %dx%d image", time_decomp, jpeg.getWidth(), jpeg.getHeight());
      } else {
        ESP_LOGE("jpeg.decode", "Failed with error: %d", jpeg.getLastError());
      }

@bitbank2 do you see any obvious mistake here? If I can get this working I would like to collaborate with JPEGDEC making a Pull request with a proper decodeDither CPP since there is at the moment no valid example. Thanks for your guidance and this awesome library!

bitbank2 commented 2 years ago

How big is ditherspace? The file buffer should not be changed and can be left at 2k, but the dither buffer must be at least 960 x 16 bytes = 15360 bytes. It looks like you are confusing the two buffers; they are not one and the same. If you point them to the same place you'll get a decoding error because the input file data will be overwritten. You must have the ditherspace buffer in your code and pass the pointer to it into the JPEGDEC library.