Source for gnu.javax.imageio.jpeg.JPEGComponent

   1: /* JPEGComponent.java --
   2:    Copyright (C)  2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.javax.imageio.jpeg;
  39: 
  40: import java.util.ArrayList;
  41: import java.io.IOException;
  42: import java.awt.image.WritableRaster;
  43: 
  44: import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
  45: 
  46: /**
  47:  * This class holds the methods to decode and write a component information to
  48:  * a raster.
  49:  */
  50: public class JPEGComponent
  51: {
  52:   public byte factorH, factorV, component_id, quant_id;
  53:   public int width = 0, height = 0, maxV = 0, maxH = 0;
  54:   public HuffmanTable ACTable;
  55:   public HuffmanTable DCTable;
  56:   public int[] quantizationTable;
  57:   public double previousDC = 0;
  58:   ArrayList data = new ArrayList();
  59: 
  60:   /**
  61:    * Initializes the component
  62:    *
  63:    * @param id
  64:    * @param factorHorizontal
  65:    * @param factorVertical
  66:    * @param quantizationID
  67:    */
  68:   public JPEGComponent(byte id, byte factorHorizontal, byte factorVertical,
  69:                        byte quantizationID)
  70:   {
  71:     component_id = id;
  72:     factorH = factorHorizontal;
  73:     factorV = factorVertical;
  74:     quant_id = quantizationID;
  75:   }
  76: 
  77:   /**
  78:    * If a restart marker is found with too little of an MCU count (i.e. our
  79:    * Restart Interval is 63 and we have 61 we copy the last MCU until it's
  80:    * full)
  81:    *
  82:    * @param index
  83:    * @param length
  84:    */
  85:   public void padMCU(int index, int length)
  86:   {
  87:     double[] src = (double[]) data.get(index - 1);
  88:     for (int i = 0; i < length; i++)
  89:       data.add(index, src);
  90:   }
  91: 
  92:   /**
  93:    * Reset the interval by setting the previous DC value
  94:    */
  95:   public void resetInterval()
  96:   {
  97:     previousDC = 0;
  98:   }
  99: 
 100:   /**
 101:    * Run the Quantization backward method on all of the block data.
 102:    */
 103:   public void quantitizeData()
 104:   {
 105:     for (int i = 0; i < data.size(); i++)
 106:       {
 107:         double[] mydata = (double[]) data.get(i);
 108:         for (int j = 0; j < mydata.length; j++)
 109:           mydata[j] *= quantizationTable[j];
 110:       }
 111:   }
 112: 
 113:   public void setDCTable(JPEGHuffmanTable table)
 114:   {
 115:     DCTable = new HuffmanTable(table);
 116:   }
 117: 
 118:   public void setACTable(JPEGHuffmanTable table)
 119:   {
 120:     ACTable = new HuffmanTable(table);
 121:   }
 122: 
 123:   /**
 124:    * Run the Inverse DCT method on all of the block data
 125:    */
 126:   public void idctData(DCT myDCT)
 127:   {
 128:     for (int i = 0; i < data.size(); i++)
 129:       data.add(i,myDCT.fast_idct(ZigZag.decode8x8_map((double[]) data.remove(i))));
 130:   }
 131: 
 132:   /**
 133:    * This scales up the component size based on the factor size. This
 134:    * calculates everyting up automatically so it's simply ran at the end of
 135:    * the frame to normalize the size of all of the components.
 136:    */
 137:   public void scaleByFactors()
 138:   {
 139:     int factorUpVertical = maxV / factorV;
 140:     int factorUpHorizontal = maxH / factorH;
 141: 
 142:     if (factorUpVertical > 1)
 143:       {
 144:         for (int i = 0; i < data.size(); i++)
 145:           {
 146:             double[][] src = (double[][]) data.remove(i);
 147:             double[][] dest =
 148:               new double[src.length * factorUpVertical][src[0].length];
 149:             for (int j = 0; j < src.length; j++)
 150:               {
 151:                 for (int u = 0; u < factorUpVertical; u++)
 152:                   {
 153:                     dest[j * factorUpVertical + u] = src[j];
 154:                   }
 155:               }
 156:             data.add(i, dest);
 157:           }
 158:       }
 159: 
 160:     if (factorUpHorizontal > 1)
 161:       {
 162:         for (int i = 0; i < data.size(); i++)
 163:           {
 164:             double[][] src = (double[][]) data.remove(i);
 165:             double[][] dest =
 166:               new double[src.length][src[0].length * factorUpHorizontal];
 167:             for (int j = 0; j < src.length; j++)
 168:               {
 169:                 for (int u = 0; u < src[0].length; u++)
 170:                   {
 171:                     for (int v = 0; v < factorUpHorizontal; v++)
 172:                       dest[j][u * factorUpHorizontal + v] = src[j][u];
 173:                   }
 174:               }
 175:             data.add(i, dest);
 176:           }
 177:       }
 178:   }
 179: 
 180:   /**
 181:    * This write the block of data to the raster throwing out anything that
 182:    * spills over the raster width or height.
 183:    *
 184:    * @param raster
 185:    * @param data
 186:    * @param compIndex
 187:    * @param x
 188:    * @param y
 189:    */
 190:   public void writeBlock(WritableRaster raster, double[][] data,
 191:                          int compIndex, int x, int y)
 192:   {
 193:     for (int yIndex = 0; yIndex < data.length; yIndex++)
 194:       {
 195:         for (int xIndex = 0; xIndex < data[yIndex].length; xIndex++)
 196:           {
 197:             // The if statement is needed because blocks can spill over the
 198:             // frame width because they are padded to make sure we keep the
 199:             // height of the block the same as the width of the block
 200:             if (x + xIndex < raster.getWidth()
 201:                 && y + yIndex < raster.getHeight())
 202:               raster.setSample(x + xIndex, y + yIndex, compIndex,
 203:                                data[yIndex][xIndex]);
 204:           }
 205:       }
 206:   }
 207: 
 208:   /**
 209:    * This writes data to a raster block, so really it's reading not writing
 210:    * but it writes the data to the raster block by factor size in a zig zag
 211:    * fashion. This has the helper function writeBlock which does the actual
 212:    * writing.
 213:    *
 214:    * @param raster
 215:    * @param componentIndex
 216:    */
 217:   public void writeData(WritableRaster raster, int componentIndex)
 218:   {
 219:     int x = 0, y = 0, lastblockheight = 0, incrementblock = 0;
 220: 
 221:     // Keep looping through all of the blocks until there are no more.
 222:     while(data.size() > 0)
 223:       {
 224:         int blockwidth = 0;
 225:         int blockheight = 0;
 226: 
 227:         if (x >= raster.getWidth())
 228:           {
 229:             x = 0;
 230:             y += incrementblock;
 231:           }
 232: 
 233:         // Loop through the horizontal component blocks of the MCU first
 234:         // then for each horizontal line write out all of the vertical
 235:         // components
 236:         for (int factorVIndex = 0; factorVIndex < factorV; factorVIndex++)
 237:           {
 238:             blockwidth = 0;
 239: 
 240:             for (int factorHIndex = 0; factorHIndex < factorH; factorHIndex++)
 241:               {
 242:                 // Captures the width of this block so we can increment the
 243:                 // X coordinate
 244:                 double[][] blockdata = (double[][]) data.remove(0);
 245: 
 246:                 // Writes the data at the specific X and Y coordinate of
 247:                 // this component
 248:                 writeBlock(raster, blockdata, componentIndex, x, y);
 249:                 blockwidth += blockdata[0].length;
 250:                 x += blockdata[0].length;
 251:                 blockheight = blockdata.length;
 252:               }
 253:             y += blockheight;
 254:             x -= blockwidth;
 255:             lastblockheight += blockheight;
 256:           }
 257:         y -= lastblockheight;
 258:         incrementblock = lastblockheight;
 259:         lastblockheight = 0;
 260:         x += blockwidth;
 261:       }
 262:   }
 263: 
 264:   /**
 265:    * Set the quantization table for this component.
 266:    *
 267:    * @param quanttable
 268:    */
 269:   public void setQuantizationTable(int[] quanttable)
 270:   {
 271:     quantizationTable = quanttable;
 272:   }
 273: 
 274:   /**
 275:    * Read in a partial MCU for this component
 276:    *
 277:    * @param stream TODO
 278:    * @throws JPEGException TODO
 279:    * @throws IOException TODO
 280:    */
 281:   public void readComponentMCU(JPEGImageInputStream stream)
 282:     throws JPEGException, IOException
 283:   {
 284:     for (int i = 0; i < factorH * factorV; i++)
 285:       {
 286:         double dc = decode_dc_coefficient(stream);
 287:         double[] datablock = decode_ac_coefficients(stream);
 288:         datablock[0] = dc;
 289:         data.add(datablock);
 290:       }
 291:   }
 292: 
 293:   /**
 294:    * Generated from text on F-22, F.2.2.1 - Huffman decoding of DC
 295:    * coefficients on ISO DIS 10918-1. Requirements and Guidelines.
 296:    *
 297:    * @param JPEGStream TODO
 298:    *
 299:    * @return TODO
 300:    * @throws JPEGException TODO
 301:    * @throws IOException TODO
 302:    */
 303:   public double decode_dc_coefficient(JPEGImageInputStream JPEGStream)
 304:         throws JPEGException, IOException
 305:   {
 306:     int t = DCTable.decode(JPEGStream);
 307:     double diff = JPEGStream.readBits(t);
 308:     diff = HuffmanTable.extend((int) diff, t);
 309:     diff = (previousDC + diff);
 310:     previousDC = diff;
 311:     return diff;
 312:   }
 313: 
 314:   /**
 315:    * Generated from text on F-23, F.13 - Huffman decoded of AC coefficients
 316:    * on ISO DIS 10918-1. Requirements and Guidelines.
 317:    *
 318:    * @param JPEGStream TODO
 319:    * @return TODO
 320:    *
 321:    * @throws JPEGException TODO
 322:    * @throws IOException TODO
 323:    */
 324:   public double[] decode_ac_coefficients(JPEGImageInputStream JPEGStream)
 325:     throws JPEGException, IOException
 326:   {
 327:     double[] zz = new double[64];
 328: 
 329:     for (int k = 1; k < 64; k++)
 330:       {
 331:         int s = ACTable.decode(JPEGStream);
 332:         int r = s >> 4;
 333:         s &= 15;
 334: 
 335:         if (s != 0)
 336:           {
 337:             k += r;
 338:             r = (int) JPEGStream.readBits(s);
 339:             s = HuffmanTable.extend(r, s);
 340:             zz[k] = s;
 341:           }
 342:         else
 343:           {
 344:             if (r != 15)
 345:               return (zz);
 346:             k += 15;
 347:           }
 348:       }
 349:     return zz;
 350:   }
 351: }