jdk/src/share/native/sun/awt/image/jpeg/imageioJPEG.c
changeset 3009 de653b2cab31
parent 2383 c6a2226cc4de
child 4248 73d530928788
child 4201 b3906ffdbcd6
equal deleted inserted replaced
3008:e6248b598ca5 3009:de653b2cab31
  1781      jint maxProgressivePass,
  1781      jint maxProgressivePass,
  1782      jboolean wantUpdates) {
  1782      jboolean wantUpdates) {
  1783 
  1783 
  1784 
  1784 
  1785     struct jpeg_source_mgr *src;
  1785     struct jpeg_source_mgr *src;
  1786     JSAMPROW scanLinePtr;
  1786     JSAMPROW scanLinePtr = NULL;
  1787     jint bands[MAX_BANDS];
  1787     jint bands[MAX_BANDS];
  1788     int i, j;
  1788     int i, j;
  1789     jint *body;
  1789     jint *body;
  1790     int scanlineLimit;
  1790     int scanlineLimit;
  1791     int pixelStride;
  1791     int pixelStride;
  1817         return JNI_FALSE;
  1817         return JNI_FALSE;
  1818     }
  1818     }
  1819 
  1819 
  1820     cinfo = (j_decompress_ptr) data->jpegObj;
  1820     cinfo = (j_decompress_ptr) data->jpegObj;
  1821 
  1821 
  1822     if ((numBands < 1) || (numBands > cinfo->num_components) ||
  1822     if ((numBands < 1) ||
  1823         (sourceXStart < 0) || (sourceXStart >= (jint)cinfo->image_width) ||
  1823         (sourceXStart < 0) || (sourceXStart >= (jint)cinfo->image_width) ||
  1824         (sourceYStart < 0) || (sourceYStart >= (jint)cinfo->image_height) ||
  1824         (sourceYStart < 0) || (sourceYStart >= (jint)cinfo->image_height) ||
  1825         (sourceWidth < 1) || (sourceWidth > (jint)cinfo->image_width) ||
  1825         (sourceWidth < 1) || (sourceWidth > (jint)cinfo->image_width) ||
  1826         (sourceHeight < 1) || (sourceHeight > (jint)cinfo->image_height) ||
  1826         (sourceHeight < 1) || (sourceHeight > (jint)cinfo->image_height) ||
  1827         (stepX < 1) || (stepY < 1) ||
  1827         (stepX < 1) || (stepY < 1) ||
  1875 
  1875 
  1876     if (setPixelBuffer(env, pb, buffer) == NOT_OK) {
  1876     if (setPixelBuffer(env, pb, buffer) == NOT_OK) {
  1877         return data->abortFlag;  // We already threw an out of memory exception
  1877         return data->abortFlag;  // We already threw an out of memory exception
  1878     }
  1878     }
  1879 
  1879 
  1880     // Allocate a 1-scanline buffer
       
  1881     scanLinePtr = (JSAMPROW)malloc(cinfo->image_width*cinfo->num_components);
       
  1882     if (scanLinePtr == NULL) {
       
  1883         RELEASE_ARRAYS(env, data, src->next_input_byte);
       
  1884         JNU_ThrowByName( env,
       
  1885                          "java/lang/OutOfMemoryError",
       
  1886                          "Reading JPEG Stream");
       
  1887         return data->abortFlag;
       
  1888     }
       
  1889 
       
  1890     /* Establish the setjmp return context for sun_jpeg_error_exit to use. */
  1880     /* Establish the setjmp return context for sun_jpeg_error_exit to use. */
  1891     jerr = (sun_jpeg_error_ptr) cinfo->err;
  1881     jerr = (sun_jpeg_error_ptr) cinfo->err;
  1892 
  1882 
  1893     if (setjmp(jerr->setjmp_buffer)) {
  1883     if (setjmp(jerr->setjmp_buffer)) {
  1894         /* If we get here, the JPEG code has signaled an error
  1884         /* If we get here, the JPEG code has signaled an error
  1898             char buffer[JMSG_LENGTH_MAX];
  1888             char buffer[JMSG_LENGTH_MAX];
  1899             (*cinfo->err->format_message) ((struct jpeg_common_struct *) cinfo,
  1889             (*cinfo->err->format_message) ((struct jpeg_common_struct *) cinfo,
  1900                                           buffer);
  1890                                           buffer);
  1901             JNU_ThrowByName(env, "javax/imageio/IIOException", buffer);
  1891             JNU_ThrowByName(env, "javax/imageio/IIOException", buffer);
  1902         }
  1892         }
  1903         free(scanLinePtr);
  1893         if (scanLinePtr != NULL) {
       
  1894             free(scanLinePtr);
       
  1895             scanLinePtr = NULL;
       
  1896         }
  1904         return data->abortFlag;
  1897         return data->abortFlag;
  1905     }
  1898     }
  1906 
  1899 
  1907     if (GET_ARRAYS(env, data, &src->next_input_byte) == NOT_OK) {
  1900     if (GET_ARRAYS(env, data, &src->next_input_byte) == NOT_OK) {
  1908         JNU_ThrowByName(env,
  1901         JNU_ThrowByName(env,
  1935     }
  1928     }
  1936 
  1929 
  1937     data->streamBuf.suspendable = FALSE;
  1930     data->streamBuf.suspendable = FALSE;
  1938 
  1931 
  1939     jpeg_start_decompress(cinfo);
  1932     jpeg_start_decompress(cinfo);
       
  1933 
       
  1934     if (numBands !=  cinfo->output_components) {
       
  1935         JNU_ThrowByName(env, "javax/imageio/IIOException",
       
  1936                         "Invalid argument to native readImage");
       
  1937         return data->abortFlag;
       
  1938     }
       
  1939 
       
  1940 
       
  1941     // Allocate a 1-scanline buffer
       
  1942     scanLinePtr = (JSAMPROW)malloc(cinfo->image_width*cinfo->output_components);
       
  1943     if (scanLinePtr == NULL) {
       
  1944         RELEASE_ARRAYS(env, data, src->next_input_byte);
       
  1945         JNU_ThrowByName( env,
       
  1946                          "java/lang/OutOfMemoryError",
       
  1947                          "Reading JPEG Stream");
       
  1948         return data->abortFlag;
       
  1949     }
  1940 
  1950 
  1941     // loop over progressive passes
  1951     // loop over progressive passes
  1942     done = FALSE;
  1952     done = FALSE;
  1943     while (!done) {
  1953     while (!done) {
  1944         if (progressive) {
  1954         if (progressive) {
  1963             jpeg_read_scanlines(cinfo, &scanLinePtr, 1);
  1973             jpeg_read_scanlines(cinfo, &scanLinePtr, 1);
  1964         }
  1974         }
  1965 
  1975 
  1966         scanlineLimit = sourceYStart+sourceHeight;
  1976         scanlineLimit = sourceYStart+sourceHeight;
  1967         pixelLimit = scanLinePtr
  1977         pixelLimit = scanLinePtr
  1968             +(sourceXStart+sourceWidth)*cinfo->num_components;
  1978             +(sourceXStart+sourceWidth)*cinfo->output_components;
  1969 
  1979 
  1970         pixelStride = stepX*cinfo->num_components;
  1980         pixelStride = stepX*cinfo->output_components;
  1971         targetLine = 0;
  1981         targetLine = 0;
  1972 
  1982 
  1973         while ((data->abortFlag == JNI_FALSE)
  1983         while ((data->abortFlag == JNI_FALSE)
  1974                && ((jint)cinfo->output_scanline < scanlineLimit)) {
  1984                && ((jint)cinfo->output_scanline < scanlineLimit)) {
  1975 
  1985 
  1980 
  1990 
  1981             if (orderedBands && (pixelStride == numBands)) {
  1991             if (orderedBands && (pixelStride == numBands)) {
  1982                 // Optimization: The component bands are ordered sequentially,
  1992                 // Optimization: The component bands are ordered sequentially,
  1983                 // so we can simply use memcpy() to copy the intermediate
  1993                 // so we can simply use memcpy() to copy the intermediate
  1984                 // scanline buffer into the raster.
  1994                 // scanline buffer into the raster.
  1985                 in = scanLinePtr + (sourceXStart * cinfo->num_components);
  1995                 in = scanLinePtr + (sourceXStart * cinfo->output_components);
  1986                 if (pixelLimit > in) {
  1996                 if (pixelLimit > in) {
  1987                     memcpy(out, in, pixelLimit - in);
  1997                     memcpy(out, in, pixelLimit - in);
  1988                 }
  1998                 }
  1989             } else {
  1999             } else {
  1990                 for (in = scanLinePtr+sourceXStart*cinfo->num_components;
  2000                 for (in = scanLinePtr+sourceXStart*cinfo->output_components;
  1991                      in < pixelLimit;
  2001                      in < pixelLimit;
  1992                      in += pixelStride) {
  2002                      in += pixelStride) {
  1993                     for (i = 0; i < numBands; i++) {
  2003                     for (i = 0; i < numBands; i++) {
  1994                         *out++ = *(in+bands[i]);
  2004                         *out++ = *(in+bands[i]);
  1995                     }
  2005                     }