Skip to content

Commit

Permalink
Do not fail on CMYK images
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin committed Jun 15, 2017
1 parent a5952bf commit 4854dde
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<arg path="${libpng.dir}/include"/>
<arg value="-I"/>
<arg path="${libwebp.dir}/include"/>
<arg line="${gcc.extra.args}"/>
<arg path="${src.native.dir}/onewebp.c"/>
<arg path="${src.native.dir}/jniwrapper.c"/>
<arg path="${libjpeg-turbo.dir}/lib64/libturbojpeg.a"/>
Expand Down Expand Up @@ -121,6 +122,7 @@
<arg path="${libjpeg-turbo.dir}/lib64/libturbojpeg.a"/>
<arg path="${libpng.dir}/lib/libpng.a"/>
<arg path="${libwebp.dir}/lib/libwebp.a"/>
<arg line="${gcc.extra.args}"/>
<arg value="-lm"/>
<arg value="-lz"/>
<arg value="-lpthread"/>
Expand Down
23 changes: 22 additions & 1 deletion src/one/webp/native/onewebp.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ static int webp_writer(const uint8_t* data, size_t dataSize, const WebPPicture*
return 1;
}

// This is not a proper conversion, but rather a quick approximation to generate low-quality previews
static void cmyk_to_argb(unsigned char* src, unsigned long srcSize) {
unsigned char* end = src + srcSize;
for (; src < end; src += 4) {
unsigned int k = src[3];
unsigned char r = src[0] * k / 255;
unsigned char g = src[1] * k / 255;
unsigned char b = src[2] * k / 255;
src[0] = b;
src[1] = g;
src[2] = r;
src[3] = 255;
}
}


static int decompress_jpeg(unsigned char* src, unsigned long srcSize, RawImage* rawImage, Params params) {
tjhandle handle = tjInitDecompress();
Expand All @@ -79,12 +94,18 @@ static int decompress_jpeg(unsigned char* src, unsigned long srcSize, RawImage*
rawImage->height = height;
rawImage->argb = (unsigned char*)malloc(width * height * 4);

if (tjDecompress2(handle, src, srcSize, rawImage->argb, width, 0, height, TJPF_BGRX, 0)) {
int pixelFormat = (colorspace == TJCS_CMYK || colorspace == TJCS_YCCK) ? TJPF_CMYK : TJPF_BGRX;
int err = tjDecompress2(handle, src, srcSize, rawImage->argb, width, 0, height, pixelFormat, 0);
if (err) {
free(rawImage->argb);
tjDestroy(handle);
return ERR_DECOMPRESS;
}

if (pixelFormat == TJPF_CMYK) {
cmyk_to_argb(rawImage->argb, width * height * 4);
}

tjDestroy(handle);
return 0;
}
Expand Down

0 comments on commit 4854dde

Please sign in to comment.