/** * Encodes a buffered image as a WSQ byte array. * Based on cwsq.c. * * @param imgObject the buffered image to encode/compress * @param r_bitrate the target bit rate * @param ppi the target density * * @return a byte array containing the encoded image */ JNIEXPORT jbyteArray JNICALL Java_org_jmrtd_imageio_WSQImageWriter_encodeWSQ (JNIEnv *env, jobject obj, jobject imgObject, jdouble bitrate, jint ppi, jstring jnistcom_text) { jclass ioexceptionClazz = (*env)->FindClass(env, "java/io/IOException"); jclass imgClazz = (*env)->FindClass(env, "java/awt/image/BufferedImage"); jclass rasterClazz = (*env)->FindClass(env, "java/awt/image/Raster"); jclass writableRasterClazz = (*env)->FindClass(env, "java/awt/image/WritableRaster"); jmethodID imgGetRasterMethodID = (*env)->GetMethodID(env, imgClazz, "getRaster", "()Ljava/awt/image/WritableRaster;"); jmethodID imgGetWidthMethodID = (*env)->GetMethodID(env, imgClazz, "getWidth", "()I"); jmethodID imgGetHeightMethodID = (*env)->GetMethodID(env, imgClazz, "getHeight", "()I"); jmethodID rasterGetDataElementsMethodID = (*env)->GetMethodID(env, rasterClazz, "getDataElements", "(IIIILjava/lang/Object;)Ljava/lang/Object;"); /* Get width, height from buffered img. */ jint width = (*env)->CallIntMethod(env, imgObject, imgGetWidthMethodID); jint height = (*env)->CallIntMethod(env, imgObject, imgGetHeightMethodID); /* Copy pixel data from buffered img to unsigned char array. */ jobject rasterObject = (*env)->CallObjectMethod(env, imgObject, imgGetRasterMethodID); jbyteArray jpixels = (jbyteArray)((*env)->CallObjectMethod(env, rasterObject, rasterGetDataElementsMethodID, 0, 0, width, height, NULL)); jint ilen = (*env)->GetArrayLength(env, jpixels); unsigned char* pixels = (unsigned char*)(*env)->GetByteArrayElements(env, jpixels, JNI_FALSE); char* nistcom_text = (char*)(*env)->GetStringUTFChars(env, jnistcom_text, NULL); unsigned char* odata=NULL; jbyteArray jout = NULL; int olen=0; int encode_ret = wsq_encode_mem(&odata, &olen, (float)bitrate, pixels, width, height, 8, ppi, nistcom_text); (*env)->ReleaseByteArrayElements(env, jpixels, (jbyte*)pixels, JNI_FALSE); (*env)->ReleaseStringUTFChars(env, jnistcom_text, nistcom_text); if (encode_ret){ (*env)->ThrowNew(env, ioexceptionClazz, "(In native C code) function wsq_encode_mem failed"); return NULL; } jout = (*env)->NewByteArray(env, olen); (*env)->SetByteArrayRegion(env, jout, 0, olen, (jbyte*)odata); free(odata); return jout; }
NISTWSQ_API int __cdecl wsq_encode( byte** compressed_data, int* compressed_data_length, float bitrate, byte* image_data, int width, int height, int depth, int pixels_per_inch, char* comment_text) { return wsq_encode_mem( compressed_data, compressed_data_length, bitrate, image_data, width, height, depth, pixels_per_inch, comment_text); }