< prev index next >

src/java.desktop/share/native/libjavajpeg/jpegdecoder.c

Print this page


   1 /*
   2  * Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 466 
 467 GLOBAL(void)
 468 sun_jpeg_term_source(j_decompress_ptr cinfo)
 469 {
 470 }
 471 
 472 JNIEXPORT void JNICALL
 473 Java_sun_awt_image_JPEGImageDecoder_initIDs(JNIEnv *env, jclass cls,
 474                                             jclass InputStreamClass)
 475 {
 476     CHECK_NULL(sendHeaderInfoID = (*env)->GetMethodID(env, cls, "sendHeaderInfo",
 477                                            "(IIZZZ)Z"));
 478     CHECK_NULL(sendPixelsByteID = (*env)->GetMethodID(env, cls, "sendPixels", "([BI)Z"));
 479     CHECK_NULL(sendPixelsIntID = (*env)->GetMethodID(env, cls, "sendPixels", "([II)Z"));
 480     CHECK_NULL(InputStream_readID = (*env)->GetMethodID(env, InputStreamClass,
 481                                              "read", "([BII)I"));
 482     CHECK_NULL(InputStream_availableID = (*env)->GetMethodID(env, InputStreamClass,
 483                                                   "available", "()I"));
 484 }
 485 
 486 
 487 /*
 488  * The Windows Itanium Aug 2002 SDK generates bad code
 489  * for this routine.  Disable optimization for now.
 490  */
 491 #ifdef _M_IA64
 492 #pragma optimize ("", off)
 493 #endif
 494 
 495 JNIEXPORT void JNICALL
 496 Java_sun_awt_image_JPEGImageDecoder_readImage(JNIEnv *env,
 497                                               jobject this,
 498                                               jobject hInputStream,
 499                                               jbyteArray hInputBuffer)
 500 {
 501   /* This struct contains the JPEG decompression parameters and pointers to
 502    * working space (which is allocated as needed by the JPEG library).
 503    */
 504   struct jpeg_decompress_struct cinfo;
 505   /* We use our private extension JPEG error handler.
 506    * Note that this struct must live as long as the main JPEG parameter
 507    * struct, to avoid dangling-pointer problems.
 508    */
 509   struct sun_jpeg_error_mgr jerr;
 510   struct sun_jpeg_source_mgr jsrc;
 511 
 512   int ret;
 513   unsigned char *bp;
 514   int *ip, pixel;


 728   /* This is an important step since it will release a good deal of memory. */
 729   jpeg_destroy_decompress(&cinfo);
 730 
 731   /* After finish_decompress, we can close the input file.
 732    * Here we postpone it until after no more JPEG errors are possible,
 733    * so as to simplify the setjmp error logic above.  (Actually, I don't
 734    * think that jpeg_destroy can do an error exit, but why assume anything...)
 735    */
 736   /* Not needed for Java - the Java code will close the file */
 737   /* fclose(infile); */
 738 
 739   /* At this point you may want to check to see whether any corrupt-data
 740    * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
 741    */
 742 
 743   /* And we're done! */
 744 
 745   RELEASE_ARRAYS(env, &jsrc);
 746   return;
 747 }
 748 #ifdef _M_IA64
 749 #pragma optimize ("", on)
 750 #endif
 751 
 752 
 753 /*
 754  * SOME FINE POINTS:
 755  *
 756  * In the above code, we ignored the return value of jpeg_read_scanlines,
 757  * which is the number of scanlines actually read.  We could get away with
 758  * this because we asked for only one line at a time and we weren't using
 759  * a suspending data source.  See libjpeg.doc for more info.
 760  *
 761  * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
 762  * we should have done it beforehand to ensure that the space would be
 763  * counted against the JPEG max_memory setting.  In some systems the above
 764  * code would risk an out-of-memory error.  However, in general we don't
 765  * know the output image dimensions before jpeg_start_decompress(), unless we
 766  * call jpeg_calc_output_dimensions().  See libjpeg.doc for more about this.
 767  *
 768  * Scanlines are returned in the same order as they appear in the JPEG file,
 769  * which is standardly top-to-bottom.  If you must emit data bottom-to-top,
 770  * you can use one of the virtual arrays provided by the JPEG memory manager
 771  * to invert the data.  See wrbmp.c for an example.
   1 /*
   2  * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 466 
 467 GLOBAL(void)
 468 sun_jpeg_term_source(j_decompress_ptr cinfo)
 469 {
 470 }
 471 
 472 JNIEXPORT void JNICALL
 473 Java_sun_awt_image_JPEGImageDecoder_initIDs(JNIEnv *env, jclass cls,
 474                                             jclass InputStreamClass)
 475 {
 476     CHECK_NULL(sendHeaderInfoID = (*env)->GetMethodID(env, cls, "sendHeaderInfo",
 477                                            "(IIZZZ)Z"));
 478     CHECK_NULL(sendPixelsByteID = (*env)->GetMethodID(env, cls, "sendPixels", "([BI)Z"));
 479     CHECK_NULL(sendPixelsIntID = (*env)->GetMethodID(env, cls, "sendPixels", "([II)Z"));
 480     CHECK_NULL(InputStream_readID = (*env)->GetMethodID(env, InputStreamClass,
 481                                              "read", "([BII)I"));
 482     CHECK_NULL(InputStream_availableID = (*env)->GetMethodID(env, InputStreamClass,
 483                                                   "available", "()I"));
 484 }
 485 









 486 JNIEXPORT void JNICALL
 487 Java_sun_awt_image_JPEGImageDecoder_readImage(JNIEnv *env,
 488                                               jobject this,
 489                                               jobject hInputStream,
 490                                               jbyteArray hInputBuffer)
 491 {
 492   /* This struct contains the JPEG decompression parameters and pointers to
 493    * working space (which is allocated as needed by the JPEG library).
 494    */
 495   struct jpeg_decompress_struct cinfo;
 496   /* We use our private extension JPEG error handler.
 497    * Note that this struct must live as long as the main JPEG parameter
 498    * struct, to avoid dangling-pointer problems.
 499    */
 500   struct sun_jpeg_error_mgr jerr;
 501   struct sun_jpeg_source_mgr jsrc;
 502 
 503   int ret;
 504   unsigned char *bp;
 505   int *ip, pixel;


 719   /* This is an important step since it will release a good deal of memory. */
 720   jpeg_destroy_decompress(&cinfo);
 721 
 722   /* After finish_decompress, we can close the input file.
 723    * Here we postpone it until after no more JPEG errors are possible,
 724    * so as to simplify the setjmp error logic above.  (Actually, I don't
 725    * think that jpeg_destroy can do an error exit, but why assume anything...)
 726    */
 727   /* Not needed for Java - the Java code will close the file */
 728   /* fclose(infile); */
 729 
 730   /* At this point you may want to check to see whether any corrupt-data
 731    * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
 732    */
 733 
 734   /* And we're done! */
 735 
 736   RELEASE_ARRAYS(env, &jsrc);
 737   return;
 738 }




 739 
 740 /*
 741  * SOME FINE POINTS:
 742  *
 743  * In the above code, we ignored the return value of jpeg_read_scanlines,
 744  * which is the number of scanlines actually read.  We could get away with
 745  * this because we asked for only one line at a time and we weren't using
 746  * a suspending data source.  See libjpeg.doc for more info.
 747  *
 748  * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
 749  * we should have done it beforehand to ensure that the space would be
 750  * counted against the JPEG max_memory setting.  In some systems the above
 751  * code would risk an out-of-memory error.  However, in general we don't
 752  * know the output image dimensions before jpeg_start_decompress(), unless we
 753  * call jpeg_calc_output_dimensions().  See libjpeg.doc for more about this.
 754  *
 755  * Scanlines are returned in the same order as they appear in the JPEG file,
 756  * which is standardly top-to-bottom.  If you must emit data bottom-to-top,
 757  * you can use one of the virtual arrays provided by the JPEG memory manager
 758  * to invert the data.  See wrbmp.c for an example.
< prev index next >