OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C)2011 D. R. Commander. All Rights Reserved. | 2 * Copyright (C)2011 D. R. Commander. All Rights Reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are met: | 5 * modification, are permitted provided that the following conditions are met: |
6 * | 6 * |
7 * - Redistributions of source code must retain the above copyright notice, | 7 * - Redistributions of source code must retain the above copyright notice, |
8 * this list of conditions and the following disclaimer. | 8 * this list of conditions and the following disclaimer. |
9 * - Redistributions in binary form must reproduce the above copyright notice, | 9 * - Redistributions in binary form must reproduce the above copyright notice, |
10 * this list of conditions and the following disclaimer in the documentation | 10 * this list of conditions and the following disclaimer in the documentation |
(...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 if((handle=tjInitTransform())==NULL) _throw(tjGetErrorStr()); | 480 if((handle=tjInitTransform())==NULL) _throw(tjGetErrorStr()); |
481 | 481 |
482 bailif0(cls=(*env)->GetObjectClass(env, obj)); | 482 bailif0(cls=(*env)->GetObjectClass(env, obj)); |
483 bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J")); | 483 bailif0(fid=(*env)->GetFieldID(env, cls, "handle", "J")); |
484 (*env)->SetLongField(env, obj, fid, (jlong)handle); | 484 (*env)->SetLongField(env, obj, fid, (jlong)handle); |
485 | 485 |
486 bailout: | 486 bailout: |
487 return; | 487 return; |
488 } | 488 } |
489 | 489 |
| 490 typedef struct _JNICustomFilterParams |
| 491 { |
| 492 JNIEnv *env; |
| 493 jobject tobj; |
| 494 jobject cfobj; |
| 495 } JNICustomFilterParams; |
| 496 |
| 497 static int JNICustomFilter(short *coeffs, tjregion arrayRegion, |
| 498 tjregion planeRegion, int componentIndex, int transformIndex, |
| 499 tjtransform *transform) |
| 500 { |
| 501 JNICustomFilterParams *params=(JNICustomFilterParams *)transform->data; |
| 502 JNIEnv *env=params->env; |
| 503 jobject tobj=params->tobj, cfobj=params->cfobj; |
| 504 jobject arrayRegionObj, planeRegionObj, bufobj, borobj; |
| 505 jclass cls; jmethodID mid; jfieldID fid; |
| 506 |
| 507 bailif0(bufobj=(*env)->NewDirectByteBuffer(env, coeffs, |
| 508 sizeof(short)*arrayRegion.w*arrayRegion.h)); |
| 509 bailif0(cls=(*env)->FindClass(env, "java/nio/ByteOrder")); |
| 510 bailif0(mid=(*env)->GetStaticMethodID(env, cls, "nativeOrder", |
| 511 "()Ljava/nio/ByteOrder;")); |
| 512 bailif0(borobj=(*env)->CallStaticObjectMethod(env, cls, mid)); |
| 513 bailif0(cls=(*env)->GetObjectClass(env, bufobj)); |
| 514 bailif0(mid=(*env)->GetMethodID(env, cls, "order", |
| 515 "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;")); |
| 516 (*env)->CallObjectMethod(env, bufobj, mid, borobj); |
| 517 bailif0(mid=(*env)->GetMethodID(env, cls, "asShortBuffer", |
| 518 "()Ljava/nio/ShortBuffer;")); |
| 519 bailif0(bufobj=(*env)->CallObjectMethod(env, bufobj, mid)); |
| 520 |
| 521 bailif0(cls=(*env)->FindClass(env, "java/awt/Rectangle")); |
| 522 bailif0(arrayRegionObj=(*env)->AllocObject(env, cls)); |
| 523 bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I")); |
| 524 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.x); |
| 525 bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I")); |
| 526 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.y); |
| 527 bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I")); |
| 528 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.w); |
| 529 bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I")); |
| 530 (*env)->SetIntField(env, arrayRegionObj, fid, arrayRegion.h); |
| 531 |
| 532 bailif0(planeRegionObj=(*env)->AllocObject(env, cls)); |
| 533 bailif0(fid=(*env)->GetFieldID(env, cls, "x", "I")); |
| 534 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.x); |
| 535 bailif0(fid=(*env)->GetFieldID(env, cls, "y", "I")); |
| 536 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.y); |
| 537 bailif0(fid=(*env)->GetFieldID(env, cls, "width", "I")); |
| 538 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.w); |
| 539 bailif0(fid=(*env)->GetFieldID(env, cls, "height", "I")); |
| 540 (*env)->SetIntField(env, planeRegionObj, fid, planeRegion.h); |
| 541 |
| 542 bailif0(cls=(*env)->GetObjectClass(env, cfobj)); |
| 543 bailif0(mid=(*env)->GetMethodID(env, cls, "customFilter", |
| 544 "(Ljava/nio/ShortBuffer;Ljava/awt/Rectangle;Ljava/awt/Rectangle;
IILorg/libjpegturbo/turbojpeg/TJTransform;)V")); |
| 545 (*env)->CallVoidMethod(env, cfobj, mid, bufobj, arrayRegionObj, |
| 546 planeRegionObj, componentIndex, transformIndex, tobj); |
| 547 |
| 548 return 0; |
| 549 |
| 550 bailout: |
| 551 return -1; |
| 552 } |
| 553 |
490 JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transf
orm | 554 JNIEXPORT jintArray JNICALL Java_org_libjpegturbo_turbojpeg_TJTransformer_transf
orm |
491 (JNIEnv *env, jobject obj, jbyteArray jsrcBuf, jint jpegSize, | 555 (JNIEnv *env, jobject obj, jbyteArray jsrcBuf, jint jpegSize, |
492 jobjectArray dstobjs, jobjectArray tobjs, jint flags) | 556 jobjectArray dstobjs, jobjectArray tobjs, jint flags) |
493 { | 557 { |
494 tjhandle handle=0; int i; | 558 tjhandle handle=0; int i; |
495 unsigned char *jpegBuf=NULL, **dstBufs=NULL; jsize n=0; | 559 unsigned char *jpegBuf=NULL, **dstBufs=NULL; jsize n=0; |
496 unsigned long *dstSizes=NULL; tjtransform *t=NULL; | 560 unsigned long *dstSizes=NULL; tjtransform *t=NULL; |
497 jbyteArray *jdstBufs=NULL; | 561 jbyteArray *jdstBufs=NULL; |
498 int jpegWidth=0, jpegHeight=0, jpegSubsamp; | 562 int jpegWidth=0, jpegHeight=0, jpegSubsamp; |
499 jintArray jdstSizes=0; jint *dstSizesi=NULL; | 563 jintArray jdstSizes=0; jint *dstSizesi=NULL; |
| 564 JNICustomFilterParams *params=NULL; |
500 | 565 |
501 gethandle(); | 566 gethandle(); |
502 | 567 |
503 if((*env)->GetArrayLength(env, jsrcBuf)<jpegSize) | 568 if((*env)->GetArrayLength(env, jsrcBuf)<jpegSize) |
504 _throw("Source buffer is not large enough"); | 569 _throw("Source buffer is not large enough"); |
505 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); | 570 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegWidth", "I")); |
506 jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); | 571 jpegWidth=(int)(*env)->GetIntField(env, obj, _fid); |
507 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); | 572 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegHeight", "I")); |
508 jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); | 573 jpegHeight=(int)(*env)->GetIntField(env, obj, _fid); |
509 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); | 574 bailif0(_fid=(*env)->GetFieldID(env, _cls, "jpegSubsamp", "I")); |
510 jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); | 575 jpegSubsamp=(int)(*env)->GetIntField(env, obj, _fid); |
511 | 576 |
512 n=(*env)->GetArrayLength(env, dstobjs); | 577 n=(*env)->GetArrayLength(env, dstobjs); |
513 if(n!=(*env)->GetArrayLength(env, tobjs)) | 578 if(n!=(*env)->GetArrayLength(env, tobjs)) |
514 _throw("Mismatch between size of transforms array and destinatio
n buffers array"); | 579 _throw("Mismatch between size of transforms array and destinatio
n buffers array"); |
515 | 580 |
516 if((dstBufs=(unsigned char **)malloc(sizeof(unsigned char *)*n))==NULL) | 581 if((dstBufs=(unsigned char **)malloc(sizeof(unsigned char *)*n))==NULL) |
517 _throw("Memory allocation failure"); | 582 _throw("Memory allocation failure"); |
518 if((jdstBufs=(jbyteArray *)malloc(sizeof(jbyteArray)*n))==NULL) | 583 if((jdstBufs=(jbyteArray *)malloc(sizeof(jbyteArray)*n))==NULL) |
519 _throw("Memory allocation failure"); | 584 _throw("Memory allocation failure"); |
520 if((dstSizes=(unsigned long *)malloc(sizeof(unsigned long)*n))==NULL) | 585 if((dstSizes=(unsigned long *)malloc(sizeof(unsigned long)*n))==NULL) |
521 _throw("Memory allocation failure"); | 586 _throw("Memory allocation failure"); |
522 if((t=(tjtransform *)malloc(sizeof(tjtransform)*n))==NULL) | 587 if((t=(tjtransform *)malloc(sizeof(tjtransform)*n))==NULL) |
523 _throw("Memory allocation failure"); | 588 _throw("Memory allocation failure"); |
| 589 if((params=(JNICustomFilterParams *)malloc(sizeof(JNICustomFilterParams)
*n)) |
| 590 ==NULL) |
| 591 _throw("Memory allocation failure"); |
524 for(i=0; i<n; i++) | 592 for(i=0; i<n; i++) |
525 { | 593 { |
526 dstBufs[i]=NULL; jdstBufs[i]=NULL; dstSizes[i]=0; | 594 dstBufs[i]=NULL; jdstBufs[i]=NULL; dstSizes[i]=0; |
527 memset(&t[i], 0, sizeof(tjtransform)); | 595 memset(&t[i], 0, sizeof(tjtransform)); |
| 596 memset(¶ms[i], 0, sizeof(JNICustomFilterParams)); |
528 } | 597 } |
529 | 598 |
530 for(i=0; i<n; i++) | 599 for(i=0; i<n; i++) |
531 { | 600 { |
532 » » jobject tobj; | 601 » » jobject tobj, cfobj; |
533 | 602 |
534 bailif0(tobj=(*env)->GetObjectArrayElement(env, tobjs, i)); | 603 bailif0(tobj=(*env)->GetObjectArrayElement(env, tobjs, i)); |
535 bailif0(_cls=(*env)->GetObjectClass(env, tobj)); | 604 bailif0(_cls=(*env)->GetObjectClass(env, tobj)); |
536 bailif0(_fid=(*env)->GetFieldID(env, _cls, "op", "I")); | 605 bailif0(_fid=(*env)->GetFieldID(env, _cls, "op", "I")); |
537 t[i].op=(*env)->GetIntField(env, tobj, _fid); | 606 t[i].op=(*env)->GetIntField(env, tobj, _fid); |
538 bailif0(_fid=(*env)->GetFieldID(env, _cls, "options", "I")); | 607 bailif0(_fid=(*env)->GetFieldID(env, _cls, "options", "I")); |
539 t[i].options=(*env)->GetIntField(env, tobj, _fid); | 608 t[i].options=(*env)->GetIntField(env, tobj, _fid); |
540 bailif0(_fid=(*env)->GetFieldID(env, _cls, "x", "I")); | 609 bailif0(_fid=(*env)->GetFieldID(env, _cls, "x", "I")); |
541 t[i].r.x=(*env)->GetIntField(env, tobj, _fid); | 610 t[i].r.x=(*env)->GetIntField(env, tobj, _fid); |
542 bailif0(_fid=(*env)->GetFieldID(env, _cls, "y", "I")); | 611 bailif0(_fid=(*env)->GetFieldID(env, _cls, "y", "I")); |
543 t[i].r.y=(*env)->GetIntField(env, tobj, _fid); | 612 t[i].r.y=(*env)->GetIntField(env, tobj, _fid); |
544 bailif0(_fid=(*env)->GetFieldID(env, _cls, "width", "I")); | 613 bailif0(_fid=(*env)->GetFieldID(env, _cls, "width", "I")); |
545 t[i].r.w=(*env)->GetIntField(env, tobj, _fid); | 614 t[i].r.w=(*env)->GetIntField(env, tobj, _fid); |
546 bailif0(_fid=(*env)->GetFieldID(env, _cls, "height", "I")); | 615 bailif0(_fid=(*env)->GetFieldID(env, _cls, "height", "I")); |
547 t[i].r.h=(*env)->GetIntField(env, tobj, _fid); | 616 t[i].r.h=(*env)->GetIntField(env, tobj, _fid); |
| 617 |
| 618 bailif0(_fid=(*env)->GetFieldID(env, _cls, "cf", |
| 619 "Lorg/libjpegturbo/turbojpeg/TJCustomFilter;")); |
| 620 cfobj=(*env)->GetObjectField(env, tobj, _fid); |
| 621 if(cfobj) |
| 622 { |
| 623 params[i].env=env; |
| 624 params[i].tobj=tobj; |
| 625 params[i].cfobj=cfobj; |
| 626 t[i].customFilter=JNICustomFilter; |
| 627 t[i].data=(void *)¶ms[i]; |
| 628 } |
548 } | 629 } |
549 | 630 |
550 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0)); | 631 bailif0(jpegBuf=(*env)->GetPrimitiveArrayCritical(env, jsrcBuf, 0)); |
551 for(i=0; i<n; i++) | 632 for(i=0; i<n; i++) |
552 { | 633 { |
553 int w=jpegWidth, h=jpegHeight; | 634 int w=jpegWidth, h=jpegHeight; |
554 if(t[i].r.w!=0) w=t[i].r.w; | 635 if(t[i].r.w!=0) w=t[i].r.w; |
555 if(t[i].r.h!=0) h=t[i].r.h; | 636 if(t[i].r.h!=0) h=t[i].r.h; |
556 bailif0(jdstBufs[i]=(*env)->GetObjectArrayElement(env, dstobjs,
i)); | 637 bailif0(jdstBufs[i]=(*env)->GetObjectArrayElement(env, dstobjs,
i)); |
557 if((*env)->GetArrayLength(env, jdstBufs[i])<tjBufSize(w, h, jpeg
Subsamp)) | 638 if((*env)->GetArrayLength(env, jdstBufs[i])<tjBufSize(w, h, jpeg
Subsamp)) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
592 if(dstSizesi) (*env)->ReleaseIntArrayElements(env, jdstSizes, dstSizesi,
0); | 673 if(dstSizesi) (*env)->ReleaseIntArrayElements(env, jdstSizes, dstSizesi,
0); |
593 if(t) free(t); | 674 if(t) free(t); |
594 return jdstSizes; | 675 return jdstSizes; |
595 } | 676 } |
596 | 677 |
597 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_destroy | 678 JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_destroy |
598 (JNIEnv *env, jobject obj) | 679 (JNIEnv *env, jobject obj) |
599 { | 680 { |
600 Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy(env, obj); | 681 Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy(env, obj); |
601 } | 682 } |
OLD | NEW |