Updated to worldwind release 20070817
[worldwind-tracker.git] / gov / nasa / worldwind / formats / nitfs / Cib2DdsCompress.java
blob4ef228b55609da64994ac0eef7ea2a38a042de94
1 package gov.nasa.worldwind.formats.nitfs;
3 import gov.nasa.worldwind.*;
4 /*
5 Copyright (C) 2001, 2007 United States Government
6 as represented by the Administrator of the
7 National Aeronautics and Space Administration.
8 All Rights Reserved.
9 */
11 /**
12 * @author Lado Garakanidze
13 * @version $Id: Cib2DdsCompress Apr 23, 2007 10:59:01 AM lado
15 class Cib2DdsCompress extends AbstractRpf2DdsCompress
17 public DDSBlock4x4 compressDxt1Block4x4(NitfsImageBand imageBand, byte[] pixelCodes, boolean hasTransparentPixels)
19 int[] grayPixels = new int[16];
20 int minColor = Integer.MAX_VALUE;
21 int maxColor = Integer.MIN_VALUE;
23 for(int i = 0; i < pixelCodes.length; i++ )
25 grayPixels[i] = imageBand.lookupGray( 0xFF & pixelCodes[i]);
26 if(grayPixels[i] < minColor)
27 minColor = grayPixels[i];
28 if(grayPixels[i] > maxColor)
29 maxColor = grayPixels[i];
32 DDSBlock4x4 ddsBlock = new DDSBlock4x4(
33 (short) DDSConverter.getPixel565( new Color( maxColor, maxColor, maxColor ) ),
34 (short) DDSConverter.getPixel565( new Color( minColor, minColor, minColor ) ),
35 0);
37 if(maxColor != minColor)
39 int[] ext = new int[] { maxColor, minColor, (2 * maxColor + minColor)/3, (maxColor + 2 * minColor)/3 };
40 ddsBlock.bitmask = 0;
41 for (int i = 0; i < grayPixels.length; i++)
43 int closest = Integer.MAX_VALUE;
44 int mask = 0;
45 for (int j = 0; j < ext.length; j++)
47 int d = ( ext[j] >= grayPixels[i] ) ? (ext[j] - grayPixels[i]) : (grayPixels[i] - ext[j]);
48 if (d < closest)
50 closest = d;
51 mask = j;
54 ddsBlock.bitmask |= mask << i * 2;
57 return ddsBlock;