Giter VIP home page Giter VIP logo

Comments (3)

alecazam avatar alecazam commented on June 29, 2024

This isn't always a generic decompress where the decompressed size is totally unknown. This is decoding known rows of known size, so the memory is allocated ahead of time. So the decompress should hand down the buffer to fill in.

I also couldn't get miniz to work either with this api.

from lodepng.

alecazam avatar alecazam commented on June 29, 2024

This does everything I'd expect for custom_zib, but is commented out by LODEPNG_NO_COMPILE_ZLIB. So I guess I'll leave in zlib by not setting that, and still supply the custom_zlib impl.

/*expected_size is expected output size, to avoid intermediate allocations. Set to 0 if not known. */
static unsigned zlib_decompress(unsigned char** out, size_t* outsize, size_t expected_size,
                                const unsigned char* in, size_t insize, const LodePNGDecompressSettings* settings) {
  unsigned error;
  if(settings->custom_zlib) {
    ucvector v = ucvector_init(*out, *outsize);
    if(expected_size) {
        /*reserve the memory to avoid intermediate reallocations*/
        ucvector_resize(&v, expected_size);
        v.size = expected_size;
    }
      
    // this only happens on iccp block
    if (*outsize == 0 && expected_size == 0) {
        expected_size = 16*1024;
        ucvector_resize(&v, expected_size);
    }
      
    error = settings->custom_zlib(&v.data, &v.size, in, insize, settings);
    if(error) {
      /*the custom zlib is allowed to have its own error codes, however, we translate it to code 110*/
      error = 110;
      /*if there's a max output size, and the custom zlib returned error, then indicate that error instead*/
      if(settings->max_output_size && *outsize > settings->max_output_size) error = 109;
    }
      
    *out = v.data;
    *outsize = v.size;
  } 

from lodepng.

alecazam avatar alecazam commented on June 29, 2024

Failures on iccp blocks (which passes 16K for size, even though ony 4506 bytes are decompressed). So then my decompressor returns an error. Not sure why this doesn't fail with the default zlib_decompress.

I did get libCompression working by +2 on ptr, and -2 on size. But then iccp block broke all this. So just went back to default decompress in lodepng.

https://github.com/alecazam/kram/blob/main/libkram/kram/Kram.cpp

// wrap miniz decompress, since it ignores crc checksum and is faster than default png
unsigned LodepngDecompressUsingMiniz(
    unsigned char** dstData, size_t* dstDataSize,
    const unsigned char* srcData, size_t srcDataSize,
    const LodePNGDecompressSettings* settings)
{
    // mz_ulong doesn't line up with size_t on Windows, but does on macOS
    KASSERT(*dstDataSize != 0);
    
#if USE_LIBCOMPRESSION
    // this returns 121 dstSize instead of 16448 on 126 srcSize.  
    // Open src dir to see this.  Have to advance by 2 to fix this.
    if (srcDataSize <= 2) {
        return MZ_DATA_ERROR;
    }
    
    char scratchBuffer[compression_decode_scratch_buffer_size(COMPRESSION_ZLIB)];
    size_t bytesDecoded = compression_decode_buffer(
         (uint8_t*)*dstData, *dstDataSize,
         (const uint8_t*)srcData + 2, srcDataSize - 2,
        scratchBuffer, // scratch-buffer that could speed up to pass
         COMPRESSION_ZLIB);
    
    int result = MZ_OK;
    if (bytesDecoded != *dstDataSize) {
        result = MZ_DATA_ERROR;
        *dstDataSize = 0;
    }
#else
    // This works.
    mz_ulong bytesDecoded = *dstDataSize;
    int result = mz_uncompress(*dstData, &bytesDecoded,
                               srcData, srcDataSize);
    
    if (result != MZ_OK || bytesDecoded != *dstDataSize) {
        *dstDataSize = 0;
    }
    else {
        *dstDataSize = bytesDecoded;
    }
#endif

    return result;
}

from lodepng.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.