--- src/frmts/jp2kak/jp2kakdataset.cpp.orig	2007-03-07 17:24:27.000000000 -0800
+++ src/frmts/jp2kak/jp2kakdataset.cpp	2007-03-07 16:53:57.000000000 -0800
@@ -202,7 +202,8 @@
 
     void        ApplyPalette( jp2_palette oJP2Palette );
     void        ProcessYCbCrTile(kdu_tile tile, GByte *pabyBuffer, 
-                                 int nBlockXOff, int nBlockYOff );
+                                 int nBlockXOff, int nBlockYOff,
+                                 int offsetx, int offsety );
     void        ProcessTile(kdu_tile tile, GByte *pabyBuffer );
 };
 
@@ -577,18 +578,17 @@
                 kdu_dims tile_dims; res.get_dims(tile_dims);
                 kdu_coords offset = tile_dims.pos - dims.pos;
 
-                GByte *pabyDest;
-
-                pabyDest = ((GByte *)pImage) 
-                    + (offset.x + offset.y*nBlockXSize) * nWordSize;
-
                 try 
                 {
                     if( tile.get_ycc() && nBand < 4 )
-                        ProcessYCbCrTile( tile, pabyDest, 
-                                          nBlockXOff, nBlockYOff );
-                    else
-                        ProcessTile( tile, pabyDest );
+                        ProcessYCbCrTile( tile, (GByte *)pImage, 
+                                          nBlockXOff, nBlockYOff,
+                                          offset.x, offset.y );
+                    else {
+                        GByte *pabyDest = ((GByte *)pImage) 
+                                + (offset.x + offset.y*nBlockXSize) * nWordSize;
+                       ProcessTile( tile, pabyDest );
+                    }
                     tile.close();
                 }
                 catch( ... )
@@ -670,7 +670,8 @@
 /************************************************************************/
 
 void JP2KAKRasterBand::ProcessYCbCrTile( kdu_tile tile, GByte *pabyDest, 
-                                         int nTileXOff, int nTileYOff )
+                                         int nTileXOff, int nTileYOff, 
+                                         int offsetx, int offsety )
 
 {
     // Open tile-components and create processing engines and resources
@@ -779,7 +780,7 @@
         kdu_convert_ycc_to_rgb(line[0], line[1], line[2]);
 
         for( iBand = 0; iBand < 3; iBand++ )
-            transfer_bytes(apabyBandDest[iBand] + y * nBlockXSize * nWordSize,
+            transfer_bytes(apabyBandDest[iBand] + (offsetx + (offsety+y) * nBlockXSize) * nWordSize,
                            line[iBand], nWordSize, bit_depth, eDataType );
     }
 

