<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2F2D_Graphics_GUI%2FBMP</id>
		<title>Java/2D Graphics GUI/BMP - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2F2D_Graphics_GUI%2FBMP"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/2D_Graphics_GUI/BMP&amp;action=history"/>
		<updated>2026-04-21T11:27:29Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/2D_Graphics_GUI/BMP&amp;diff=8171&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/2D_Graphics_GUI/BMP&amp;diff=8171&amp;oldid=prev"/>
				<updated>2010-06-01T06:55:48Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 06:55, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/2D_Graphics_GUI/BMP&amp;diff=8170&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/2D_Graphics_GUI/BMP&amp;diff=8170&amp;oldid=prev"/>
				<updated>2010-05-31T18:01:46Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== A decoder for Windows bitmap (.BMP) files ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;   &lt;br /&gt;
/*&lt;br /&gt;
 * BMPLoader.&lt;br /&gt;
 * &lt;br /&gt;
 * JavaZOOM : jlgui@javazoom.net&lt;br /&gt;
 *            http://www.javazoom.net &lt;br /&gt;
 *&lt;br /&gt;
 *-----------------------------------------------------------------------&lt;br /&gt;
 *   This program is free software; you can redistribute it and/or modify&lt;br /&gt;
 *   it under the terms of the GNU Library General Public License as published&lt;br /&gt;
 *   by the Free Software Foundation; either version 2 of the License, or&lt;br /&gt;
 *   (at your option) any later version.&lt;br /&gt;
 *&lt;br /&gt;
 *   This program is distributed in the hope that it will be useful,&lt;br /&gt;
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the&lt;br /&gt;
 *   GNU Library General Public License for more details.&lt;br /&gt;
 *&lt;br /&gt;
 *   You should have received a copy of the GNU Library General Public&lt;br /&gt;
 *   License along with this program; if not, write to the Free Software&lt;br /&gt;
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.&lt;br /&gt;
 *----------------------------------------------------------------------&lt;br /&gt;
 */&lt;br /&gt;
import java.awt.Image;&lt;br /&gt;
import java.awt.Toolkit;&lt;br /&gt;
import java.awt.image.ColorModel;&lt;br /&gt;
import java.awt.image.IndexColorModel;&lt;br /&gt;
import java.awt.image.MemoryImageSource;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
/**&lt;br /&gt;
 * A decoder for Windows bitmap (.BMP) files.&lt;br /&gt;
 * Compression not supported.&lt;br /&gt;
 */&lt;br /&gt;
public class BMPLoader&lt;br /&gt;
{&lt;br /&gt;
    private InputStream is;&lt;br /&gt;
    private int curPos = 0;&lt;br /&gt;
    private int bitmapOffset; // starting position of image data&lt;br /&gt;
    private int width; // image width in pixels&lt;br /&gt;
    private int height; // image height in pixels&lt;br /&gt;
    private short bitsPerPixel; // 1, 4, 8, or 24 (no color map)&lt;br /&gt;
    private int compression; // 0 (none), 1 (8-bit RLE), or 2 (4-bit RLE)&lt;br /&gt;
    private int actualSizeOfBitmap;&lt;br /&gt;
    private int scanLineSize;&lt;br /&gt;
    private int actualColorsUsed;&lt;br /&gt;
    private byte r[], g[], b[]; // color palette&lt;br /&gt;
    private int noOfEntries;&lt;br /&gt;
    private byte[] byteData; // Unpacked data&lt;br /&gt;
    private int[] intData; // Unpacked data&lt;br /&gt;
    public BMPLoader()&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
    public Image getBMPImage(InputStream stream) throws Exception&lt;br /&gt;
    {&lt;br /&gt;
        read(stream);&lt;br /&gt;
        return Toolkit.getDefaultToolkit().createImage(getImageSource());&lt;br /&gt;
    }&lt;br /&gt;
    protected int readInt() throws IOException&lt;br /&gt;
    {&lt;br /&gt;
        int b1 = is.read();&lt;br /&gt;
        int b2 = is.read();&lt;br /&gt;
        int b3 = is.read();&lt;br /&gt;
        int b4 = is.read();&lt;br /&gt;
        curPos += 4;&lt;br /&gt;
        return ((b4 &amp;lt;&amp;lt; 24) + (b3 &amp;lt;&amp;lt; 16) + (b2 &amp;lt;&amp;lt; 8) + (b1 &amp;lt;&amp;lt; 0));&lt;br /&gt;
    }&lt;br /&gt;
    protected short readShort() throws IOException&lt;br /&gt;
    {&lt;br /&gt;
        int b1 = is.read();&lt;br /&gt;
        int b2 = is.read();&lt;br /&gt;
        curPos += 4;&lt;br /&gt;
        return (short) ((b2 &amp;lt;&amp;lt; 8) + b1);&lt;br /&gt;
    }&lt;br /&gt;
    protected void getFileHeader() throws IOException, Exception&lt;br /&gt;
    {&lt;br /&gt;
        // Actual contents (14 bytes):&lt;br /&gt;
        short fileType = 0x4d42;// always &amp;quot;BM&amp;quot;&lt;br /&gt;
        int fileSize; // size of file in bytes&lt;br /&gt;
        short reserved1 = 0; // always 0&lt;br /&gt;
        short reserved2 = 0; // always 0&lt;br /&gt;
        fileType = readShort();&lt;br /&gt;
        if (fileType != 0x4d42) throw new Exception(&amp;quot;Not a BMP file&amp;quot;); // wrong file type&lt;br /&gt;
        fileSize = readInt();&lt;br /&gt;
        reserved1 = readShort();&lt;br /&gt;
        reserved2 = readShort();&lt;br /&gt;
        bitmapOffset = readInt();&lt;br /&gt;
    }&lt;br /&gt;
    protected void getBitmapHeader() throws IOException&lt;br /&gt;
    {&lt;br /&gt;
        // Actual contents (40 bytes):&lt;br /&gt;
        int size; // size of this header in bytes&lt;br /&gt;
        short planes; // no. of color planes: always 1&lt;br /&gt;
        int sizeOfBitmap; // size of bitmap in bytes (may be 0: if so, calculate)&lt;br /&gt;
        int horzResolution; // horizontal resolution, pixels/meter (may be 0)&lt;br /&gt;
        int vertResolution; // vertical resolution, pixels/meter (may be 0)&lt;br /&gt;
        int colorsUsed; // no. of colors in palette (if 0, calculate)&lt;br /&gt;
        int colorsImportant; // no. of important colors (appear first in palette) (0 means all are important)&lt;br /&gt;
        boolean topDown;&lt;br /&gt;
        int noOfPixels;&lt;br /&gt;
        size = readInt();&lt;br /&gt;
        width = readInt();&lt;br /&gt;
        height = readInt();&lt;br /&gt;
        planes = readShort();&lt;br /&gt;
        bitsPerPixel = readShort();&lt;br /&gt;
        compression = readInt();&lt;br /&gt;
        sizeOfBitmap = readInt();&lt;br /&gt;
        horzResolution = readInt();&lt;br /&gt;
        vertResolution = readInt();&lt;br /&gt;
        colorsUsed = readInt();&lt;br /&gt;
        colorsImportant = readInt();&lt;br /&gt;
        topDown = (height &amp;lt; 0);&lt;br /&gt;
        noOfPixels = width * height;&lt;br /&gt;
        // Scan line is padded with zeroes to be a multiple of four bytes&lt;br /&gt;
        scanLineSize = ((width * bitsPerPixel + 31) / 32) * 4;&lt;br /&gt;
        if (sizeOfBitmap != 0) actualSizeOfBitmap = sizeOfBitmap;&lt;br /&gt;
        else&lt;br /&gt;
        // a value of 0 doesn&amp;quot;t mean zero - it means we have to calculate it&lt;br /&gt;
        actualSizeOfBitmap = scanLineSize * height;&lt;br /&gt;
        if (colorsUsed != 0) actualColorsUsed = colorsUsed;&lt;br /&gt;
        else&lt;br /&gt;
        // a value of 0 means we determine this based on the bits per pixel&lt;br /&gt;
        if (bitsPerPixel &amp;lt; 16) actualColorsUsed = 1 &amp;lt;&amp;lt; bitsPerPixel;&lt;br /&gt;
        else actualColorsUsed = 0; // no palette&lt;br /&gt;
    }&lt;br /&gt;
    protected void getPalette() throws IOException&lt;br /&gt;
    {&lt;br /&gt;
        noOfEntries = actualColorsUsed;&lt;br /&gt;
        //IJ.write(&amp;quot;noOfEntries: &amp;quot; + noOfEntries);&lt;br /&gt;
        if (noOfEntries &amp;gt; 0)&lt;br /&gt;
        {&lt;br /&gt;
            r = new byte[noOfEntries];&lt;br /&gt;
            g = new byte[noOfEntries];&lt;br /&gt;
            b = new byte[noOfEntries];&lt;br /&gt;
            int reserved;&lt;br /&gt;
            for (int i = 0; i &amp;lt; noOfEntries; i++)&lt;br /&gt;
            {&lt;br /&gt;
                b[i] = (byte) is.read();&lt;br /&gt;
                g[i] = (byte) is.read();&lt;br /&gt;
                r[i] = (byte) is.read();&lt;br /&gt;
                reserved = is.read();&lt;br /&gt;
                curPos += 4;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    protected void unpack(byte[] rawData, int rawOffset, int[] intData, int intOffset, int w)&lt;br /&gt;
    {&lt;br /&gt;
        int j = intOffset;&lt;br /&gt;
        int k = rawOffset;&lt;br /&gt;
        int mask = 0xff;&lt;br /&gt;
        for (int i = 0; i &amp;lt; w; i++)&lt;br /&gt;
        {&lt;br /&gt;
            int b0 = (((int) (rawData[k++])) &amp;amp; mask);&lt;br /&gt;
            int b1 = (((int) (rawData[k++])) &amp;amp; mask) &amp;lt;&amp;lt; 8;&lt;br /&gt;
            int b2 = (((int) (rawData[k++])) &amp;amp; mask) &amp;lt;&amp;lt; 16;&lt;br /&gt;
            intData[j] = 0xff000000 | b0 | b1 | b2;&lt;br /&gt;
            j++;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    protected void unpack(byte[] rawData, int rawOffset, int bpp, byte[] byteData, int byteOffset, int w) throws Exception&lt;br /&gt;
    {&lt;br /&gt;
        int j = byteOffset;&lt;br /&gt;
        int k = rawOffset;&lt;br /&gt;
        byte mask;&lt;br /&gt;
        int pixPerByte;&lt;br /&gt;
        switch (bpp)&lt;br /&gt;
        {&lt;br /&gt;
        case 1:&lt;br /&gt;
            mask = (byte) 0x01;&lt;br /&gt;
            pixPerByte = 8;&lt;br /&gt;
            break;&lt;br /&gt;
        case 4:&lt;br /&gt;
            mask = (byte) 0x0f;&lt;br /&gt;
            pixPerByte = 2;&lt;br /&gt;
            break;&lt;br /&gt;
        case 8:&lt;br /&gt;
            mask = (byte) 0xff;&lt;br /&gt;
            pixPerByte = 1;&lt;br /&gt;
            break;&lt;br /&gt;
        default:&lt;br /&gt;
            throw new Exception(&amp;quot;Unsupported bits-per-pixel value&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        for (int i = 0;;)&lt;br /&gt;
        {&lt;br /&gt;
            int shift = 8 - bpp;&lt;br /&gt;
            for (int ii = 0; ii &amp;lt; pixPerByte; ii++)&lt;br /&gt;
            {&lt;br /&gt;
                byte br = rawData[k];&lt;br /&gt;
                br &amp;gt;&amp;gt;= shift;&lt;br /&gt;
                byteData[j] = (byte) (br &amp;amp; mask);&lt;br /&gt;
                //System.out.println(&amp;quot;Setting byteData[&amp;quot; + j + &amp;quot;]=&amp;quot; + Test.byteToHex(byteData[j]));&lt;br /&gt;
                j++;&lt;br /&gt;
                i++;&lt;br /&gt;
                if (i == w) return;&lt;br /&gt;
                shift -= bpp;&lt;br /&gt;
            }&lt;br /&gt;
            k++;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    protected int readScanLine(byte[] b, int off, int len) throws IOException&lt;br /&gt;
    {&lt;br /&gt;
        int bytesRead = 0;&lt;br /&gt;
        int l = len;&lt;br /&gt;
        int r = 0;&lt;br /&gt;
        while (len &amp;gt; 0)&lt;br /&gt;
        {&lt;br /&gt;
            bytesRead = is.read(b, off, len);&lt;br /&gt;
            if (bytesRead == -1) return r == 0 ? -1 : r;&lt;br /&gt;
            if (bytesRead == len) return l;&lt;br /&gt;
            len -= bytesRead;&lt;br /&gt;
            off += bytesRead;&lt;br /&gt;
            r += bytesRead;&lt;br /&gt;
        }&lt;br /&gt;
        return l;&lt;br /&gt;
    }&lt;br /&gt;
    protected void getPixelData() throws IOException, Exception&lt;br /&gt;
    {&lt;br /&gt;
        byte[] rawData; // the raw unpacked data&lt;br /&gt;
        // Skip to the start of the bitmap data (if we are not already there)&lt;br /&gt;
        long skip = bitmapOffset - curPos;&lt;br /&gt;
        if (skip &amp;gt; 0)&lt;br /&gt;
        {&lt;br /&gt;
            is.skip(skip);&lt;br /&gt;
            curPos += skip;&lt;br /&gt;
        }&lt;br /&gt;
        int len = scanLineSize;&lt;br /&gt;
        if (bitsPerPixel &amp;gt; 8) intData = new int[width * height];&lt;br /&gt;
        else byteData = new byte[width * height];&lt;br /&gt;
        rawData = new byte[actualSizeOfBitmap];&lt;br /&gt;
        int rawOffset = 0;&lt;br /&gt;
        int offset = (height - 1) * width;&lt;br /&gt;
        for (int i = height - 1; i &amp;gt;= 0; i--)&lt;br /&gt;
        {&lt;br /&gt;
            int n = readScanLine(rawData, rawOffset, len);&lt;br /&gt;
            if (n &amp;lt; len) throw new Exception(&amp;quot;Scan line ended prematurely after &amp;quot; + n + &amp;quot; bytes&amp;quot;);&lt;br /&gt;
            if (bitsPerPixel &amp;gt; 8)&lt;br /&gt;
            {&lt;br /&gt;
                // Unpack and create one int per pixel&lt;br /&gt;
                unpack(rawData, rawOffset, intData, offset, width);&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                // Unpack and create one byte per pixel&lt;br /&gt;
                unpack(rawData, rawOffset, bitsPerPixel, byteData, offset, width);&lt;br /&gt;
            }&lt;br /&gt;
            rawOffset += len;&lt;br /&gt;
            offset -= width;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public void read(InputStream is) throws IOException, Exception&lt;br /&gt;
    {&lt;br /&gt;
        this.is = is;&lt;br /&gt;
        getFileHeader();&lt;br /&gt;
        getBitmapHeader();&lt;br /&gt;
        if (compression != 0) throw new Exception(&amp;quot;BMP Compression not supported&amp;quot;);&lt;br /&gt;
        getPalette();&lt;br /&gt;
        getPixelData();&lt;br /&gt;
    }&lt;br /&gt;
    public MemoryImageSource getImageSource()&lt;br /&gt;
    {&lt;br /&gt;
        ColorModel cm;&lt;br /&gt;
        MemoryImageSource mis;&lt;br /&gt;
        if (noOfEntries &amp;gt; 0)&lt;br /&gt;
        {&lt;br /&gt;
            // There is a color palette; create an IndexColorModel&lt;br /&gt;
            cm = new IndexColorModel(bitsPerPixel, noOfEntries, r, g, b);&lt;br /&gt;
        }&lt;br /&gt;
        else&lt;br /&gt;
        {&lt;br /&gt;
            // There is no palette; use the default RGB color model&lt;br /&gt;
            cm = ColorModel.getRGBdefault();&lt;br /&gt;
        }&lt;br /&gt;
        // Create MemoryImageSource&lt;br /&gt;
        if (bitsPerPixel &amp;gt; 8)&lt;br /&gt;
        {&lt;br /&gt;
            // use one int per pixel&lt;br /&gt;
            mis = new MemoryImageSource(width, height, cm, intData, 0, width);&lt;br /&gt;
        }&lt;br /&gt;
        else&lt;br /&gt;
        {&lt;br /&gt;
            // use one byte per pixel&lt;br /&gt;
            mis = new MemoryImageSource(width, height, cm, byteData, 0, width);&lt;br /&gt;
        }&lt;br /&gt;
        return mis; // this can be used by JComponent.createImage()&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>