2010-05-07 96 views
0

我有一個問題想通過索引進行顏色映射。在Java中使用我的自定義顏色映射圖像

我嘗試這個代碼發現

http://www.podgoretsky.pri.ee/ftp/Docs/Java/Tricks%20of%20the%20Java%20Programming%20Gurus/ch12.htm

// Gradient.java 
// Imports 
import java.applet.Applet; 
import java.awt.*; 
import java.awt.image.*; 

public class Gradient extends Applet { 
    final int colors = 32; 
    final int width = 200; 
    final int height = 200; 
    Image img; 

    public void init() { 
    // Create the color map 
    byte[] rbmap = new byte[colors]; 
    byte[] gmap = new byte[colors]; 
    for (int i = 0; i < colors; i++) 
     gmap[i] = (byte)((i * 255)/(colors - 1)); 

    // Create the color model 
    int bits = (int)Math.ceil(Math.log(colors)/Math.log(2)); 
    IndexColorModel model = new IndexColorModel(bits, colors, 
     rbmap, gmap, rbmap); 

    // Create the pixels 
    int pixels[] = new int[width * height]; 
    int index = 0; 
    for (int y = 0; y < height; y++) 
     for (int x = 0; x < width; x++) 
     pixels[index++] = (x * colors)/width; 

    // Create the image 
    img = createImage(new MemoryImageSource(width, height, model, 
     pixels, 0, width)); 
    } 

    public void paint(Graphics g) { 
    g.drawImage(img, 0, 0, this); 
    } 
} 

它的工作很好,但我試圖加載映射在我自己的顏色表自定義圖片JPEG,但它沒有工作的權利。我在白色背景上看到只有一堆綠色和藍色像素。 我的自定義顏色映射方法在這裏:

public void inintByteArrays() { 
    double[][] c = // basic color map 
    { { 0.0000, 0.0000, 0.5625 }, { 0.0000, 0.0000, 0.6250 }, 
    { 0.0000, 0.0000, 0.6875 }, { 0.0000, 0.0000, 0.6875 }, 
    { 0.0000, 0.0000, 0.7500 }, { 0.0000, 0.0000, 0.8125 }, 
    { 0.0000, 0.0000, 0.8750 }, { 0.0000, 0.0000, 0.9375 }, 
    { 0.0000, 0.0000, 1.0000 }, { 0.0000, 0.0625, 1.0000 }, 
    { 0.0000, 0.1250, 1.0000 }, { 0.0000, 0.1875, 1.0000 }, 
    { 0.0000, 0.2500, 1.0000 }, { 0.0000, 0.3125, 1.0000 }, 
    { 0.0000, 0.3750, 1.0000 }, { 0.0000, 0.4375, 1.0000 }, 
    { 0.0000, 0.5000, 1.0000 }, { 0.0000, 0.5625, 1.0000 }, 
    { 0.0000, 0.6250, 1.0000 }, { 0.0000, 0.6875, 1.0000 }, 
    { 0.0000, 0.7500, 1.0000 }, { 0.0000, 0.8125, 1.0000 }, 
    { 0.0000, 0.8750, 1.0000 }, { 0.0000, 0.9375, 1.0000 }, 
    { 0.0000, 1.0000, 1.0000 }, { 0.0625, 1.0000, 0.9375 }, 
    { 0.1250, 1.0000, 0.8750 }, { 0.1875, 1.0000, 0.8125 }, 
    { 0.2500, 1.0000, 0.7500 }, { 0.3125, 1.0000, 0.6875 }, 
    { 0.3750, 1.0000, 0.6250 }, { 0.4375, 1.0000, 0.5625 }, 
    { 0.5000, 1.0000, 0.5000 }, { 0.5625, 1.0000, 0.4375 }, 
    { 0.6250, 1.0000, 0.3750 }, { 0.6875, 1.0000, 0.3125 }, 
    { 0.7500, 1.0000, 0.2500 }, { 0.8125, 1.0000, 0.1875 }, 
    { 0.8750, 1.0000, 0.1250 }, { 0.9375, 1.0000, 0.0625 }, 
    { 1.0000, 1.0000, 0.0000 }, { 1.0000, 0.9375, 0.0000 }, 
    { 1.0000, 0.8750, 0.0000 }, { 1.0000, 0.8125, 0.0000 }, 
    { 1.0000, 0.7500, 0.0000 }, { 1.0000, 0.6875, 0.0000 }, 
    { 1.0000, 0.6250, 0.0000 }, { 1.0000, 0.5625, 0.0000 }, 
    { 1.0000, 0.5000, 0.0000 }, { 1.0000, 0.4375, 0.0000 }, 
    { 1.0000, 0.3750, 0.0000 }, { 1.0000, 0.3125, 0.0000 }, 
    { 1.0000, 0.2500, 0.0000 }, { 1.0000, 0.1875, 0.0000 }, 
    { 1.0000, 0.1250, 0.0000 }, { 1.0000, 0.0625, 0.0000 }, 
    { 1.0000, 0.0000, 0.0000 }, { 0.9375, 0.0000, 0.0000 }, 
    { 0.8750, 0.0000, 0.0000 }, { 0.8125, 0.0000, 0.0000 }, 
    { 0.7500, 0.0000, 0.0000 }, { 0.6875, 0.0000, 0.0000 }, 
    { 0.6250, 0.0000, 0.0000 }, { 0.5625, 0.0000, 0.0000 }, 
    { 0.5000, 0.0000, 0.0000 } }; 

    for (int i = 0; i < c.length; i++) { 

    for (int j = 0; j < c[i].length; j++) { 
    if (j == 0) 
    r[i] = (byte) ((byte) c[i][j]*255); 
    if (j == 1) 
    g[i] = (byte) ((byte) c[i][j]*255); 
    if (j == 2) 
    b[i] = (byte) ((byte) c[i][j]*255); 
    } 
    } 

我的問題是如何使用我的色彩表,因爲我想加載和地圖以正確的方式任何圖像。非常感謝你!

Greetings,protein1。

回答

0

Jpegs不使用索引顏色,它們存儲RGB值。

你想達到什麼效果?例如,您可以將JPEG轉換爲灰度,將灰度存儲爲索引,然後使用顏色映射。