//dont work
public static BufferedImage toImage(int w, int h, byte[] data) {
DataBuffer buffer = new DataBufferByte(data, 4614400);
int pixelStride = 4; //assuming r, g, b, skip, r, g, b, skip...
int scanlineStride = 4*w; //no extra padding
int[] bandOffsets = {0, 1, 2}; //r, g, b
WritableRaster raster = Raster.createInterleavedRaster(buffer, w, h, scanlineStride, pixelStride, bandOffsets, null);
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
boolean hasAlpha = false;
boolean isAlphaPremultiplied = false;
int transparency = Transparency.OPAQUE;
int transferType = DataBuffer.TYPE_BYTE;
ColorModel colorModel = new ComponentColorModel(colorSpace, hasAlpha, isAlphaPremultiplied, transparency, transferType);
return new BufferedImage(colorModel, raster, isAlphaPremultiplied, null);
}
//NEW VARIANT
public static BufferedImage toImage1(BufferedImage bi, int w, int h, byte[] data) {
DataBuffer buffer = new DataBufferByte(data, w*h);
WritableRaster biR = bi.getRaster();
boolean isAlphaPremultiplied = bi.isAlphaPremultiplied();
int pixelStride = 4; //assuming r, g, b, skip, r, g, b, skip...
int scanlineStride = 4*w; //no extra padding
int[] bandOffsets = {0, 1, 2}; //r, g, b
// WritableRaster raster =Raster.createPackedRaster(buffer, w, h, scanlineStride, pixelStride, bandOffsets, null);
WritableRaster raster =Raster.createPackedRaster(buffer, w, h, 8, null);
// biR.createRaster(sm, buffer, location)
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
boolean hasAlpha = false;
// boolean isAlphaPremultiplied = false;
int transparency = bi.getTransparency();// Transparency.OPAQUE;
int transferType = bi.getType();//DataBuffer.TYPE_BYTE;
ColorModel colorModel = new ComponentColorModel(colorSpace, hasAlpha, isAlphaPremultiplied, transparency, transferType);
return new BufferedImage(colorModel, raster, isAlphaPremultiplied, null);
}
public static BufferedImage toImage3( int w, int h,byte[] buf){
BufferedImage frame=null;
String sBuf = new String(buf);
int bufLength=sBuf.length();
int alpha=255;
int red=0;
int green=0;
int blue=0;
int iBuf=0;
int pIndex=0;
int[] pixels= new int[bufLength];
/* // This code from other sample, but that sample have 3 byte for pixel
for(int col= h -1 ;col >=0;col--){
iBuf= w*3*col;
for(int row = 0;row < w;row++){
alpha=255;
red=0;
green=0;
blue=0;
blue |= (buf[iBuf++] & 0xff );
green |= (buf[iBuf++] & 0xff );
red |= (buf[iBuf++] & 0xff );
pixels[pIndex]=(alpha << 24) | (red << 16) | (green << 8 ) | blue;
pIndex++;
}
}
*/
DataBuffer buffer = new DataBufferByte(buf, w*h);
WritableRaster raster =Raster.createPackedRaster(buffer, w, h, 8, null);
frame = new BufferedImage(w,h,BufferedImage.TYPE_BYTE_INDEXED);
// frame.setRGB(0, 0, w, h,byte2int(buf,buf.length), 0, w);
frame.setData(raster);
return frame;
}
// SECOND VARIANT
public static BufferedImage toImage2( int w, int h, byte[] data) {
// create ColorModel using my method
ColorModel colorModel = makeDefaultColorModel();
//reconstruct bufferedimage from data,w,h
// get sample model from color model
SampleModel sampleModel = colorModel.createCompatibleSampleModel(w, h);
// make raster using dbuf which I created using a given byte[] of RAW data
DataBuffer resultBuffer = new DataBufferInt(byte2int(data,data.length), data.length);
WritableRaster raster = Raster.createWritableRaster(sampleModel, resultBuffer, null);
// make image
image = new BufferedImage(colorModel, raster, false, null);
return image;
}
public static int[] byte2int( byte[] data, int size) {
int[] int_arr= new int[size];
for (int i=0; i<size; i++){
// int_arr[i] = (data[i] & 0xff );
int_arr[i] = (int) data[i];
}
return int_arr;
}
static ColorModel makeDefaultColorModel()
{
byte[] r = new byte[256];
byte[] g = new byte[256];
byte[] b = new byte[256];
/* for(int i=0; i<256; i++)
{
r[i]=(byte)i;
g[i]=(byte)i;
b[i]=(byte)i;
}
return new IndexColorModel(8, 256, r, g, b);
*/
return ColorModel.getRGBdefault();
}
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
by: CEHJPosted on 2009-10-28 at 06:25:55ID: 25682958
What kind of image is it?