Code Monkey home page Code Monkey logo

turbojpeg-ios's Introduction

turbojpeg-ios

libturbojpeg libjpeg iOS 静态库

turbojpeg压缩

+(NSData *)turboJpegCopress:(NSData *)data withWidth:(int)width withHeight:(int)height {
    tjhandle handle = tjInitCompress();
    unsigned long size = TJBUFSIZE(width, height);
    unsigned char *dstBuf = malloc(size);
    tjCompress(handle, (unsigned char *)data.bytes, width, width * 4, height, 4, dstBuf, &size, 0, 100, 0);
    NSData *dstData = [[NSData alloc]initWithBytes:dstBuf length:size];
    free(dstBuf);
    return dstData;
}

turbojpeg解压

+(NSData *)turboJpegDecompress:(NSData *)data withWidth:(int)width withHeight:(int)height {
    tjhandle deHandle = tjInitDecompress();
    long size = width * height * 4;
    unsigned char *dstBuf = malloc(size);
    int result = tjDecompress(deHandle, (unsigned char *)data.bytes, data.length, dstBuf, width, width * 4, height, 4, TJFLAG_FORCESSE3);
    if (result == -1) {
        NSLog(@"turbojpeg failed");
    }
    NSData *dstData = [[NSData alloc]initWithBytes:dstBuf length:size];
    free(dstBuf);
    return dstData;
}

其它压缩算法

jpeg压缩属于图片压缩算法,还有对文件而言的通用压缩算法,通用压缩算法也能够对图片进行压缩,例如 zstd snappy lz4

zstd压缩

+(NSData *)zstdCompress:(NSData *)data {
    size_t const cBuffSize = ZSTD_compressBound(data.length);
    void* const cBuff = malloc(cBuffSize);
    size_t const cSize = ZSTD_compress(cBuff, cBuffSize, data.bytes, data.length, 1);
    if (ZSTD_isError(cSize)) {
        printf("error compressing : %s \n", ZSTD_getErrorName(cSize));
        exit(8);
    }
    NSData *comData = [[NSData alloc]initWithBytes:cBuff length:cSize];
    free(cBuff);
    return comData;
}

zstd解压

+(NSData *)zstdDecompress:(NSData *)data {
    long rSize = ZSTD_getDecompressedSize(data.bytes, data.length);
    if (rSize == ZSTD_CONTENTSIZE_ERROR) {
        printf("it was not compressed by zstd.\n");
    } else if (rSize==ZSTD_CONTENTSIZE_UNKNOWN) {
        printf("original size unknown. Use streaming decompression instead.\n");
    }
    void* const rBuff = malloc(rSize);
    size_t const dSize = ZSTD_decompress(rBuff, rSize, data.bytes, data.length);
    if (dSize != rSize) {
        printf("error decoding : %s \n", ZSTD_getErrorName(dSize));
    }
    NSData *content = [[NSData alloc]initWithBytes:rBuff length:dSize];
    free(rBuff);
    return content;
}

snappy压缩

+(NSData *)snappyCompress:(NSData *)data {
    size_t outSize = snappy_max_compressed_length(data.length);
    char* outBuff = malloc(outSize);
    snappy_compress(data.bytes, data.length, outBuff, &outSize);
    NSData *snappyData = [[NSData alloc]initWithBytes:outBuff length:outSize];
    free(outBuff);
    return snappyData;
}

snappy解压

+(NSData *)snappyDecompress:(NSData *)data {
    size_t outSize;
    snappy_status status = snappy_uncompressed_length(data.bytes, data.length, &outSize);
    if (status != 0) {
        printf("snappy status is zero");
    }
    char* outBuff = malloc(outSize);
    snappy_uncompress(data.bytes, data.length, outBuff, &outSize);
    NSData *snappyData = [[NSData alloc]initWithBytes:outBuff length:outSize];
    free(outBuff);
    return snappyData;
}

lz4压缩

+(NSData *)lz4Compress:(NSData *)data {
    int dstSize = LZ4_compressBound((int)data.length);
    char *outBuff = malloc(dstSize);
    //dstSize = LZ4_compress_default(data.bytes, outBuff, (int)data.length, dstSize);
    dstSize = LZ4_compress_fast(data.bytes, outBuff, (int)data.length, dstSize, 1);
    NSData *dstData = [[NSData alloc]initWithBytes:outBuff length:dstSize];
    free(outBuff);
    return dstData;
}

lz4解压

+(NSData *)lz4Decompress:(NSData *)data {
    char *outBuff = malloc(dstSize);
    dstSize = LZ4_decompress_safe(data.bytes, outBuff, (int)conData.length, dstSize);
    //dstSize = LZ4_decompress_fast(data.bytes, outBuff, dstSize);
    NSData *dstData = [[NSData alloc]initWithBytes:outBuff length:dstSize];
    free(outBuff);
    return dstData;
}

turbojpeg-ios's People

Contributors

wccw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.