home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / WIN95 / IAVAZIP.EXE / DATA.Z / ImageLoaderTGA.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-08-18  |  5.7 KB  |  368 lines

  1. package com.sfs.iavazip.viewer;
  2.  
  3. import java.awt.Image;
  4. import java.io.IOException;
  5.  
  6. public class ImageLoaderTGA extends ImageLoader implements ProgressStatus {
  7.    // $FF: renamed from: zl int
  8.    private int field_0;
  9.    private int redColor15 = 31744;
  10.    private int greenColor15 = 992;
  11.    private int blueColor15 = 31;
  12.    private int redColor24 = 16711680;
  13.    private int greenColor24 = 65280;
  14.    private int blueColor24 = 255;
  15.    private int orientation;
  16.    private int imgType;
  17.    private int IDlength;
  18.    private int cMapFirst;
  19.    private int xOrigin;
  20.    private int yOrigin;
  21.    private int description;
  22.    private int depth;
  23.    private int pixels;
  24.    private int cMapType;
  25.    private int cMapLength;
  26.    private int cMapEntrySize;
  27.    private int framePtr;
  28.    private int cachePtr;
  29.    private int red;
  30.    private int green;
  31.    private int blue;
  32.    private int col;
  33.    private int cacheStep;
  34.    private int stepFrameCol;
  35.    private int stepFrameRow;
  36.    private int pixelCt;
  37.    private int pixel;
  38.    private int alpha;
  39.    private int fileLength;
  40.    private int RLEtype;
  41.    private int RLElength;
  42.    private int RLEcounter;
  43.    private int RLEpause;
  44.    // $FF: renamed from: w int
  45.    private int field_1;
  46.    // $FF: renamed from: h int
  47.    private int field_2;
  48.    private int value;
  49.    private boolean loaded;
  50.    private boolean RLEencoded;
  51.    private byte[] cache;
  52.    private int[] palette;
  53.  
  54.    protected void loadPicture() throws IOException {
  55.       this.depth = super.imageFile.readByte();
  56.       this.description = super.imageFile.readByte();
  57.       this.orientation = (this.description & 240) >> 4;
  58.       ((ImageLoader)this).print("ID field length: " + this.IDlength);
  59.       ((ImageLoader)this).print("Color map type: " + this.cMapType);
  60.       ((ImageLoader)this).print("Image type: " + this.imgType);
  61.       ((ImageLoader)this).print("First color map entry: " + this.cMapFirst);
  62.       ((ImageLoader)this).print("Color map length: " + this.cMapLength);
  63.       ((ImageLoader)this).print("Color map entry size: " + this.cMapEntrySize);
  64.       ((ImageLoader)this).print("X Origin: " + this.xOrigin);
  65.       ((ImageLoader)this).print("Y Origin: " + this.yOrigin);
  66.       ((ImageLoader)this).print("width: " + this.field_1);
  67.       ((ImageLoader)this).print("height: " + this.field_2);
  68.       ((ImageLoader)this).print("depth: " + this.depth);
  69.       ((ImageLoader)this).print("orientation: " + this.orientation);
  70.       this.RLEpause = 0;
  71.       this.RLEcounter = 0;
  72.       this.RLElength = 1;
  73.       if (this.imgType == 9 | this.imgType == 10 | this.imgType == 11) {
  74.          ((ImageLoader)this).print("RLE encoded picture");
  75.          this.RLEencoded = true;
  76.       }
  77.  
  78.       super.imageFile.skipBytes(this.IDlength);
  79.       if (this.cMapType == 1) {
  80.          if (this.cMapEntrySize == 16) {
  81.          }
  82.  
  83.          ((ImageLoader)this).print("loading color map");
  84.          this.palette = new int[this.cMapLength * (this.cMapEntrySize / 8)];
  85.          this.value = 0;
  86.  
  87.          for(this.field_0 = 0; this.field_0 < this.cMapLength; ++this.field_0) {
  88.             if (this.cMapEntrySize == 24) {
  89.                this.palette[this.value++] = super.imageFile.readByte();
  90.                this.palette[this.value++] = super.imageFile.readByte();
  91.                this.palette[this.value++] = super.imageFile.readByte();
  92.             }
  93.          }
  94.       }
  95.  
  96.       this.pixels = this.field_1 * this.field_2;
  97.       if (!this.RLEencoded) {
  98.          this.cache = super.imageFile.readBytes(this.pixels * (this.depth / 8), this);
  99.          ((ImageLoader)this).print("data bytes in cache: " + this.cache.length);
  100.       }
  101.  
  102.       ((ImageLoader)this).print("converting " + super.frameBuffer.length + " pixels");
  103.       this.stepFrameCol = 1;
  104.       this.stepFrameRow = -(2 * super.xRes);
  105.       this.framePtr = (super.yRes - 1) * super.xRes;
  106.       this.pixelCt = 0;
  107.       if (this.orientation == 16) {
  108.          this.stepFrameCol = -1;
  109.          this.stepFrameRow = 0;
  110.          this.framePtr = this.pixels - 1;
  111.       } else if (this.orientation == 32) {
  112.          this.stepFrameCol = 1;
  113.          this.stepFrameRow = 0;
  114.          this.framePtr = 0;
  115.       } else if (this.orientation == 48) {
  116.          this.stepFrameCol = -1;
  117.          this.stepFrameRow = 2 * super.xRes;
  118.          this.framePtr = super.xRes - 1;
  119.       }
  120.  
  121.       if (this.imgType == 1 | this.imgType == 9) {
  122.          this.decodeCMAP();
  123.       } else if (this.imgType == 2 | this.imgType == 10) {
  124.          this.decodeTC();
  125.       } else if (this.imgType == 3 | this.imgType == 11) {
  126.          this.decodeGray();
  127.       }
  128.  
  129.       ((ImageLoader)this).print("image decoded, closing file");
  130.       super.imageFile.close();
  131.       super.imageLoaded = true;
  132.       this.cache = null;
  133.       this.palette = null;
  134.       System.gc();
  135.    }
  136.  
  137.    private void decodeGray() throws IOException {
  138.       ((ImageLoader)this).print("loading a grayscale image");
  139.  
  140.       while(this.pixelCt < this.pixels) {
  141.          if (this.RLEencoded) {
  142.             if (this.RLEpause == 0) {
  143.                this.decodeRLE();
  144.             }
  145.  
  146.             this.pixel = super.imageFile.readByte();
  147.          } else {
  148.             this.pixel = this.cache[this.cachePtr++] & 255;
  149.          }
  150.  
  151.          this.value = (this.alpha << 24) + (this.pixel << 16) + (this.pixel << 8) + this.pixel;
  152.  
  153.          for(this.RLEcounter = 0; this.RLEcounter < this.RLElength; ++this.RLEcounter) {
  154.             super.frameBuffer[this.framePtr] = this.value;
  155.             this.framePtr += this.stepFrameCol;
  156.             ++this.col;
  157.             if (this.col == super.xRes) {
  158.                this.framePtr += this.stepFrameRow;
  159.                this.col = 0;
  160.             }
  161.  
  162.             ++this.pixelCt;
  163.          }
  164.  
  165.          if (this.RLEpause > 0) {
  166.             this.RLEpause += -1;
  167.          }
  168.       }
  169.  
  170.    }
  171.  
  172.    private void decodeCMAP() throws IOException {
  173.       if (this.depth != 16 && this.depth == 8) {
  174.          ((ImageLoader)this).print("decoding an 8-bit TGA image with palette");
  175.  
  176.          while(this.pixelCt < this.pixels) {
  177.             if (this.RLEencoded) {
  178.                if (this.RLEpause == 0) {
  179.                   this.decodeRLE();
  180.                }
  181.  
  182.                this.pixel = super.imageFile.readByte() * 3;
  183.             } else {
  184.                this.pixel = (this.cache[this.cachePtr] & 255) * 3;
  185.                ++this.cachePtr;
  186.             }
  187.  
  188.             this.red = this.palette[this.pixel + 2];
  189.             this.green = this.palette[this.pixel + 1];
  190.             this.blue = this.palette[this.pixel];
  191.             this.value = (this.red << 16) + (this.green << 8) + this.blue;
  192.  
  193.             for(this.RLEcounter = 0; this.RLEcounter < this.RLElength; ++this.RLEcounter) {
  194.                super.frameBuffer[this.framePtr] = this.value;
  195.                this.framePtr += this.stepFrameCol;
  196.                ++this.col;
  197.                if (this.col == super.xRes) {
  198.                   this.framePtr += this.stepFrameRow;
  199.                   this.col = 0;
  200.                }
  201.  
  202.                ++this.pixelCt;
  203.             }
  204.  
  205.             if (this.RLEpause > 0) {
  206.                this.RLEpause += -1;
  207.             }
  208.          }
  209.       }
  210.  
  211.    }
  212.  
  213.    public void imageLoaded(int var1, Image var2, String var3) {
  214.    }
  215.  
  216.    private final void decodeRLE() throws IOException {
  217.       int var1 = super.imageFile.readByte();
  218.       this.RLEtype = var1 & 128;
  219.       this.RLElength = (var1 & 127) + 1;
  220.       if (this.RLEtype == 0) {
  221.          this.RLEpause = this.RLElength;
  222.          this.RLElength = 1;
  223.       }
  224.  
  225.    }
  226.  
  227.    public String getInfo() {
  228.       return "";
  229.    }
  230.  
  231.    protected void readHeader() throws IOException {
  232.       ((ImageLoader)this).print(this.getInfo() + " running");
  233.       super.imageFile.setIntelSwitch(true);
  234.       this.IDlength = super.imageFile.readByte();
  235.       this.cMapType = super.imageFile.readByte();
  236.       this.imgType = super.imageFile.readByte();
  237.       this.cMapFirst = super.imageFile.read16Bit();
  238.       this.cMapLength = super.imageFile.read16Bit();
  239.       this.cMapEntrySize = super.imageFile.readByte();
  240.       this.xOrigin = super.imageFile.read16Bit();
  241.       this.yOrigin = super.imageFile.read16Bit();
  242.       this.field_1 = super.imageFile.read16Bit();
  243.       this.field_2 = super.imageFile.read16Bit();
  244.       ((ImageLoader)this).initFrameBuffer(this.field_1, this.field_2);
  245.    }
  246.  
  247.    private final void decodeTC() throws IOException {
  248.       ((ImageLoader)this).print("loading a True Color image");
  249.       if (this.depth == 16) {
  250.          while(this.pixelCt < this.pixels) {
  251.             if (this.RLEencoded) {
  252.                if (this.RLEpause == 0) {
  253.                   this.decodeRLE();
  254.                }
  255.  
  256.                this.pixel = super.imageFile.readByte();
  257.                this.pixel += super.imageFile.readByte() * 256;
  258.             } else {
  259.                this.pixel = this.cache[this.cachePtr + 1] & 255;
  260.                this.pixel = (this.pixel << 8) + (this.cache[this.cachePtr] & 255);
  261.                this.cachePtr += 2;
  262.             }
  263.  
  264.             this.red = (this.pixel & this.redColor15) >> 10;
  265.             this.green = (this.pixel & this.greenColor15) >> 5;
  266.             this.blue = this.pixel & this.blueColor15;
  267.             this.red = this.red << 8 >> 5;
  268.             this.green = this.green << 8 >> 5;
  269.             this.blue = this.blue << 8 >> 5;
  270.             this.value = (this.alpha << 24) + (this.red << 16) + (this.green << 8) + this.blue;
  271.  
  272.             for(this.RLEcounter = 0; this.RLEcounter < this.RLElength; ++this.RLEcounter) {
  273.                super.frameBuffer[this.framePtr] = this.value;
  274.                this.framePtr += this.stepFrameCol;
  275.                ++this.col;
  276.                if (this.col == super.xRes) {
  277.                   this.framePtr += this.stepFrameRow;
  278.                   this.col = 0;
  279.                }
  280.  
  281.                ++this.pixelCt;
  282.             }
  283.  
  284.             if (this.RLEpause > 0) {
  285.                this.RLEpause += -1;
  286.             }
  287.          }
  288.  
  289.       } else if (this.depth == 24) {
  290.          while(this.pixelCt < this.pixels) {
  291.             if (this.RLEencoded) {
  292.                if (this.RLEpause == 0) {
  293.                   this.decodeRLE();
  294.                }
  295.  
  296.                this.blue = super.imageFile.readByte();
  297.                this.green = super.imageFile.readByte();
  298.                this.red = super.imageFile.readByte();
  299.             } else {
  300.                this.red = this.cache[this.cachePtr + 2];
  301.                this.green = this.cache[this.cachePtr + 1];
  302.                this.blue = this.cache[this.cachePtr];
  303.                this.cachePtr += 3;
  304.             }
  305.  
  306.             this.value = (this.alpha << 24) + (this.red << 16) + (this.green << 8) + this.blue;
  307.  
  308.             for(this.RLEcounter = 0; this.RLEcounter < this.RLElength; ++this.RLEcounter) {
  309.                super.frameBuffer[this.framePtr] = this.value;
  310.                this.framePtr += this.stepFrameCol;
  311.                ++this.col;
  312.                if (this.col == super.xRes) {
  313.                   this.framePtr += this.stepFrameRow;
  314.                   this.col = 0;
  315.                }
  316.  
  317.                ++this.pixelCt;
  318.             }
  319.  
  320.             if (this.RLEpause > 0) {
  321.                this.RLEpause += -1;
  322.             }
  323.          }
  324.  
  325.       } else {
  326.          if (this.depth == 32) {
  327.             while(this.pixelCt < this.pixels) {
  328.                if (this.RLEencoded) {
  329.                   if (this.RLEpause == 0) {
  330.                      this.decodeRLE();
  331.                   }
  332.  
  333.                   this.blue = super.imageFile.readByte();
  334.                   this.green = super.imageFile.readByte();
  335.                   this.red = super.imageFile.readByte();
  336.                   this.alpha = super.imageFile.readByte();
  337.                } else {
  338.                   this.red = this.cache[this.cachePtr + 2];
  339.                   this.green = this.cache[this.cachePtr + 1];
  340.                   this.blue = this.cache[this.cachePtr];
  341.                   this.alpha = this.cache[this.cachePtr + 3];
  342.                   this.cachePtr += 4;
  343.                }
  344.  
  345.                this.value = (this.alpha << 24) + (this.red << 16) + (this.green << 8) + this.blue;
  346.  
  347.                for(this.RLEcounter = 0; this.RLEcounter < this.RLElength; ++this.RLEcounter) {
  348.                   super.frameBuffer[this.framePtr] = this.value;
  349.                   this.framePtr += this.stepFrameCol;
  350.                   ++this.col;
  351.                   if (this.col == super.xRes) {
  352.                      this.framePtr += this.stepFrameRow;
  353.                      this.col = 0;
  354.                   }
  355.  
  356.                   ++this.pixelCt;
  357.                }
  358.  
  359.                if (this.RLEpause > 0) {
  360.                   this.RLEpause += -1;
  361.                }
  362.             }
  363.          }
  364.  
  365.       }
  366.    }
  367. }
  368.