JNIEXPORT void JNICALL TJCompressor_encodeYUV_12 (JNIEnv *env, jobject obj, jarray src, jint srcElementSize, jint width, jint pitch, jint height, jint pf, jbyteArray dst, jint subsamp, jint flags) { tjhandle handle=0; jsize arraySize=0; unsigned char *srcBuf=NULL, *dstBuf=NULL; gethandle(); if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 || pitch<0) _throwarg("Invalid argument in encodeYUV()"); if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) _throwarg("Mismatch between Java and C API"); arraySize=(pitch==0)? width*tjPixelSize[pf]*height:pitch*height; if((*env)->GetArrayLength(env, src)*srcElementSize<arraySize) _throwarg("Source buffer is not large enough"); if((*env)->GetArrayLength(env, dst) <(jsize)tjBufSizeYUV(width, height, subsamp)) _throwarg("Destination buffer is not large enough"); bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); if(tjEncodeYUV2(handle, srcBuf, width, pitch, height, pf, dstBuf, subsamp, flags)==-1) _throwtj(); bailout: if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); return; }
JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_encodeYUV___3IIIII_3BII (JNIEnv *env, jobject obj, jintArray src, jint width, jint pitch, jint height, jint pf, jbyteArray dst, jint subsamp, jint flags) { tjhandle handle=0; jsize arraySize=0; unsigned char *srcBuf=NULL, *dstBuf=NULL; gethandle(); if(pf<0 || pf>=org_libjpegturbo_turbojpeg_TJ_NUMPF || width<1 || height<1 || pitch<0) _throw("Invalid argument in compress()"); if(org_libjpegturbo_turbojpeg_TJ_NUMPF!=TJ_NUMPF) _throw("Mismatch between Java and C API"); if(tjPixelSize[pf]!=sizeof(jint)) _throw("Pixel format must be 32-bit when encoding from an integer buffer."); arraySize=(pitch==0)? width*height:pitch*height; if((*env)->GetArrayLength(env, src)<arraySize) _throw("Source buffer is not large enough"); if((*env)->GetArrayLength(env, dst) <(jsize)tjBufSizeYUV(width, height, subsamp)) _throw("Destination buffer is not large enough"); bailif0(srcBuf=(*env)->GetPrimitiveArrayCritical(env, src, 0)); bailif0(dstBuf=(*env)->GetPrimitiveArrayCritical(env, dst, 0)); if(tjEncodeYUV2(handle, srcBuf, width, pitch*sizeof(jint), height, pf, dstBuf, subsamp, flags)==-1) { (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); dstBuf=srcBuf=NULL; _throw(tjGetErrorStr()); } bailout: if(dstBuf) (*env)->ReleasePrimitiveArrayCritical(env, dst, dstBuf, 0); if(srcBuf) (*env)->ReleasePrimitiveArrayCritical(env, src, srcBuf, 0); return; }
void dotestyuv(unsigned char *srcbuf, int w, int h, int subsamp, char *filename) { char tempstr[1024], tempstr2[80]; FILE *file=NULL; tjhandle handle=NULL; unsigned char *dstbuf=NULL; double start, elapsed; int i, retval=0, ps=tjPixelSize[pf]; int yuvsize=0; yuvsize=tjBufSizeYUV(w, h, subsamp); if((dstbuf=(unsigned char *)malloc(yuvsize)) == NULL) _throwunix("allocating image buffer"); if(!quiet) printf(">>>>> %s (%s) <--> YUV %s <<<<<\n", pixFormatStr[pf], (flags&TJFLAG_BOTTOMUP)? "Bottom-up":"Top-down", subNameLong[subsamp]); if(quiet==1) printf("%s\t%s\t%s\tN/A\t", pixFormatStr[pf], (flags&TJFLAG_BOTTOMUP)? "BU":"TD", subNameLong[subsamp]); if((handle=tjInitCompress())==NULL) _throwtj("executing tjInitCompress()"); /* Execute once to preload cache */ if(tjEncodeYUV2(handle, srcbuf, w, 0, h, pf, dstbuf, subsamp, flags)==-1) _throwtj("executing tjEncodeYUV2()"); /* Benchmark */ for(i=0, start=gettime(); (elapsed=gettime()-start)<benchtime; i++) { if(tjEncodeYUV2(handle, srcbuf, w, 0, h, pf, dstbuf, subsamp, flags)==-1) _throwtj("executing tjEncodeYUV2()"); } if(tjDestroy(handle)==-1) _throwtj("executing tjDestroy()"); handle=NULL; if(quiet==1) printf("%-4d %-4d\t", w, h); if(quiet) { printf("%s%c%s%c", sigfig((double)(w*h)/1000000.*(double)i/elapsed, 4, tempstr, 1024), quiet==2? '\n':'\t', sigfig((double)(w*h*ps)/(double)yuvsize, 4, tempstr2, 80), quiet==2? '\n':'\t'); } else { printf("\n%s size: %d x %d\n", "Image", w, h); printf("C--> Frame rate: %f fps\n", (double)i/elapsed); printf(" Output image size: %d bytes\n", yuvsize); printf(" Compression ratio: %f:1\n", (double)(w*h*ps)/(double)yuvsize); printf(" Source throughput: %f Megapixels/sec\n", (double)(w*h)/1000000.*(double)i/elapsed); printf(" Output bit stream: %f Megabits/sec\n", (double)yuvsize*8./1000000.*(double)i/elapsed); } snprintf(tempstr, 1024, "%s_%s.yuv", filename, subName[subsamp]); if((file=fopen(tempstr, "wb"))==NULL) _throwunix("opening reference image"); if(fwrite(dstbuf, yuvsize, 1, file)!=1) _throwunix("writing reference image"); fclose(file); file=NULL; if(!quiet) printf("Reference image written to %s\n", tempstr); bailout: if(file) {fclose(file); file=NULL;} if(dstbuf) {free(dstbuf); dstbuf=NULL;} if(handle) {tjDestroy(handle); handle=NULL;} return; }