diff -Nru openjdk.orig/jdk/src/share/native/sun/awt/image/jpeg/imageioJPEG.c openjdk/jdk/src/share/native/sun/awt/image/jpeg/imageioJPEG.c
--- openjdk.orig/jdk/src/share/native/sun/awt/image/jpeg/imageioJPEG.c	2013-08-30 18:50:38.238272213 +0100
+++ openjdk/jdk/src/share/native/sun/awt/image/jpeg/imageioJPEG.c	2013-08-30 18:57:43.240892673 +0100
@@ -447,9 +447,6 @@
     pixelBuffer pixelBuf;     // Buffer for pixels
 
     jboolean abortFlag;       // Passed down from Java abort method
-
-    UINT8 scale[MAX_BANDS][NUM_INPUT_VALUES];
-    int bandSizes[MAX_BANDS]; // For scaling to-from non-8-bit images
 } imageIOData, *imageIODataPtr;
 
 /*
@@ -487,12 +484,6 @@
 
     data->abortFlag = JNI_FALSE;
 
-    for (i = 0; i < MAX_BANDS; i ++) {
-        data->bandSizes[i] = 0;
-        for (j = 0; j < NUM_INPUT_VALUES; j++) {
-            data->scale[i][j] = 0;
-        }
-    }
     return data;
 }
 
@@ -1978,34 +1969,6 @@
 
     (*env)->ReleaseIntArrayElements(env, srcBands, body, JNI_ABORT);
 
-    bandSize = (*env)->GetIntArrayElements(env, bandSizes, NULL);
-
-    for (i = 0; i < numBands; i++) {
-        if (bandSize[i] != JPEG_BAND_SIZE) {
-            mustScale = TRUE;
-            break;
-        }
-    }
-
-    if (mustScale) {
-        // Build any scale tables that aren't already OK
-        for (i = 0; i < numBands; i++) {
-            if (data->bandSizes[i] != bandSize[i]) {
-                data->bandSizes[i] = bandSize[i];
-                maxBandValue = (1 << bandSize[i]) - 1;
-                halfMaxBandValue = maxBandValue >> 1;
-                for (j = 0; j <= maxBandValue; j++) {
-                    data->scale[i][j] =
-                        (UINT8)((j*MAX_JPEG_BAND_VALUE
-                                 + halfMaxBandValue)/maxBandValue);
-                }
-            }
-        }
-    }
-
-    (*env)->ReleaseIntArrayElements(env, bandSizes,
-                                    bandSize, JNI_ABORT);
-
 #ifdef DEBUG_IIO_JPEG
     printf("---- in reader.read ----\n");
     printf("numBands is %d\n", numBands);
@@ -2028,15 +1991,6 @@
         return data->abortFlag;  // We already threw an out of memory exception
     }
 
-    // Allocate a 1-scanline buffer
-    if (cinfo->num_components <= 0 ||
-        cinfo->image_width > (UINT_MAX / (unsigned int)cinfo->num_components))
-    {
-        RELEASE_ARRAYS(env, data, src->next_input_byte);
-        JNU_ThrowByName(env, "javax/imageio/IIOException",
-                        "Invalid number of color components");
-        return data->abortFlag;
-    }
     /* Establish the setjmp return context for sun_jpeg_error_exit to use. */
     jerr = (sun_jpeg_error_ptr) cinfo->err;
 
@@ -2154,15 +2108,8 @@
 
             // Now mangle it into our buffer
             out = data->pixelBuf.buf.bp;
-            if (mustScale) {
-                for (in = scanLinePtr+sourceXStart*cinfo->num_components;
-                     in < pixelLimit;
-                     in += pixelStride) {
-                    for (i = 0; i < numBands; i++) {
-                        *out++ = data->scale[i][*(in+bands[i])];
-                    }
-                }
-            } else if (orderedBands && (pixelStride == numBands)) {
+
+            if (orderedBands && (pixelStride == numBands)) {
                 // Optimization: The component bands are ordered sequentially,
                 // so we can simply use memcpy() to copy the intermediate
                 // scanline buffer into the raster.
@@ -2177,7 +2124,7 @@
             } else {
                 numBytes = numBands;
                 for (in = scanLinePtr+sourceXStart*cinfo->output_components;
-                     in < pixelLimit  &&
+                     in < pixelLimit &&
                        numBytes <= data->pixelBuf.byteBufferLength;
                      in += pixelStride) {
                     for (i = 0; i < numBands; i++) {
@@ -2914,9 +2861,10 @@
     jint *scanData;
     jint *bandSize;
     int maxBandValue, halfMaxBandValue;
-    boolean mustScale = FALSE;
     imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr);
     j_compress_ptr cinfo;
+    UINT8** scale = NULL;
+
 
     /* verify the inputs */
 
@@ -2963,24 +2911,32 @@
 
     for (i = 0; i < numBands; i++) {
         if (bandSize[i] != JPEG_BAND_SIZE) {
-            mustScale = TRUE;
-            break;
-        }
-    }
+            if (scale == NULL) {
+                scale = (UINT8**) calloc(numBands, sizeof(UINT8*));
 
-    if (mustScale) {
-        // Build any scale tables that aren't already OK
-        for (i = 0; i < numBands; i++) {
-            if (data->bandSizes[i] != bandSize[i]) {
-                data->bandSizes[i] = bandSize[i];
-                maxBandValue = (1 << bandSize[i]) - 1;
-                halfMaxBandValue = maxBandValue >> 1;
-                for (j = 0; j <= maxBandValue; j++) {
-                    data->scale[i][j] =
-                        (UINT8)((j*MAX_JPEG_BAND_VALUE
-                                 + halfMaxBandValue)/maxBandValue);
+                if (scale == NULL) {
+                    JNU_ThrowByName( env, "java/lang/OutOfMemoryError",
+                                     "Writing JPEG Stream");
+                    return JNI_FALSE;
                 }
             }
+
+            maxBandValue = (1 << bandSize[i]) - 1;
+
+            scale[i] = (UINT8*) malloc((maxBandValue + 1) * sizeof(UINT8));
+
+            if (scale[i] == NULL) {
+                JNU_ThrowByName( env, "java/lang/OutOfMemoryError",
+                                 "Writing JPEG Stream");
+                return JNI_FALSE;
+            }
+
+            halfMaxBandValue = maxBandValue >> 1;
+
+            for (j = 0; j <= maxBandValue; j++) {
+                scale[i][j] = (UINT8)
+                    ((j*MAX_JPEG_BAND_VALUE + halfMaxBandValue)/maxBandValue);
+            }
         }
     }
 
@@ -3189,28 +3145,25 @@
         out = scanLinePtr;
         pixelLimit = in + ((pixelBufferSize > data->pixelBuf.byteBufferLength) ?
                            data->pixelBuf.byteBufferLength : pixelBufferSize);
-        if (mustScale) {
-          for (; (in < pixelLimit) && (out < scanLineLimit); in += pixelStride) {
-                for (i = 0; i < numBands; i++) {
-                    *out++ = data->scale[i][*(in+i)];
+        for (; (in < pixelLimit) && (out < scanLineLimit); in += pixelStride) {
+            for (i = 0; i < numBands; i++) {
+                if (scale !=NULL && scale[i] != NULL) {
+                    *out++ = scale[i][*(in+i)];
 #ifdef DEBUG_IIO_JPEG
                     if (in == data->pixelBuf.buf.bp){ // Just the first pixel
                         printf("in %d -> out %d, ", *(in+i), *(out-i-1));
                     }
 #endif
-                }
+
 #ifdef DEBUG_IIO_JPEG
                     if (in == data->pixelBuf.buf.bp){ // Just the first pixel
                         printf("\n");
                     }
 #endif
-            }
-        } else {
-          for (; (in < pixelLimit) && (out < scanLineLimit); in += pixelStride) {
-                for (i = 0; i < numBands; i++) {
+                } else {
                     *out++ = *(in+i);
                 }
-          }
+            }
         }
         // write it out
         jpegwritescanlines(cinfo, (JSAMPARRAY)&scanLinePtr, 1);
@@ -3226,6 +3179,16 @@
     } else {
         jpegabort((j_common_ptr)cinfo);
     }
+
+    if (scale != NULL) {
+        for (i = 0; i < numBands; i++) {
+            if (scale[i] != NULL) {
+                free(scale[i]);
+            }
+        }
+        free(scale);
+    }
+
     free(scanLinePtr);
     RELEASE_ARRAYS(env, data, NULL);
     return data->abortFlag;
