📄 image.java
字号:
columns = targa_header.width; rows = targa_header.height; numPixels = columns * rows; if (dim != null) { dim.width = columns; dim.height = rows; } pic = new byte[numPixels * 4]; // targa_rgba; if (targa_header.id_length != 0) targa_header.data.position(targa_header.id_length); // skip TARGA image comment buf_p = targa_header.data; byte red,green,blue,alphabyte; red = green = blue = alphabyte = 0; int packetHeader, packetSize, j; if (targa_header.image_type==2) { // Uncompressed, RGB images for(row=rows-1; row>=0; row--) { pixbuf = row * columns * 4; for(column=0; column<columns; column++) { switch (targa_header.pixel_size) { case 24: blue = buf_p.get(); green = buf_p.get(); red = buf_p.get(); pic[pixbuf++] = red; pic[pixbuf++] = green; pic[pixbuf++] = blue; pic[pixbuf++] = (byte)255; break; case 32: blue = buf_p.get(); green = buf_p.get(); red = buf_p.get(); alphabyte = buf_p.get(); pic[pixbuf++] = red; pic[pixbuf++] = green; pic[pixbuf++] = blue; pic[pixbuf++] = alphabyte; break; } } } } else if (targa_header.image_type==10) { // Runlength encoded RGB images for(row=rows-1; row>=0; row--) { pixbuf = row * columns * 4; try { for(column=0; column<columns; ) { packetHeader= buf_p.get() & 0xFF; packetSize = 1 + (packetHeader & 0x7f); if ((packetHeader & 0x80) != 0) { // run-length packet switch (targa_header.pixel_size) { case 24: blue = buf_p.get(); green = buf_p.get(); red = buf_p.get(); alphabyte = (byte)255; break; case 32: blue = buf_p.get(); green = buf_p.get(); red = buf_p.get(); alphabyte = buf_p.get(); break; } for(j=0;j<packetSize;j++) { pic[pixbuf++]=red; pic[pixbuf++]=green; pic[pixbuf++]=blue; pic[pixbuf++]=alphabyte; column++; if (column==columns) { // run spans across rows column=0; if (row>0) row--; else // goto label breakOut; throw gotoBreakOut; pixbuf = row * columns * 4; } } } else { // non run-length packet for(j=0;j<packetSize;j++) { switch (targa_header.pixel_size) { case 24: blue = buf_p.get(); green = buf_p.get(); red = buf_p.get(); pic[pixbuf++] = red; pic[pixbuf++] = green; pic[pixbuf++] = blue; pic[pixbuf++] = (byte)255; break; case 32: blue = buf_p.get(); green = buf_p.get(); red = buf_p.get(); alphabyte = buf_p.get(); pic[pixbuf++] = red; pic[pixbuf++] = green; pic[pixbuf++] = blue; pic[pixbuf++] = alphabyte; break; } column++; if (column==columns) { // pixel packet run spans across rows column=0; if (row>0) row--; else // goto label breakOut; throw gotoBreakOut; pixbuf = row * columns * 4; } } } } } catch (Throwable e){ // label breakOut: } } } return pic; } /* ==================================================================== IMAGE FLOOD FILLING ==================================================================== */ /* ================= Mod_FloodFillSkin Fill background pixels so mipmapping doesn't have haloes ================= */ static class floodfill_t { short x, y; } // must be a power of 2 static final int FLOODFILL_FIFO_SIZE = 0x1000; static final int FLOODFILL_FIFO_MASK = FLOODFILL_FIFO_SIZE - 1; // // #define FLOODFILL_STEP( off, dx, dy ) \ // { \ // if (pos[off] == fillcolor) \ // { \ // pos[off] = 255; \ // fifo[inpt].x = x + (dx), fifo[inpt].y = y + (dy); \ // inpt = (inpt + 1) & FLOODFILL_FIFO_MASK; \ // } \ // else if (pos[off] != 255) fdc = pos[off]; \ // } // void FLOODFILL_STEP( int off, int dx, int dy ) // { // if (pos[off] == fillcolor) // { // pos[off] = 255; // fifo[inpt].x = x + dx; fifo[inpt].y = y + dy; // inpt = (inpt + 1) & FLOODFILL_FIFO_MASK; // } // else if (pos[off] != 255) fdc = pos[off]; // } static floodfill_t[] fifo = new floodfill_t[FLOODFILL_FIFO_SIZE]; static { for (int j = 0; j < fifo.length; j++) { fifo[j] = new floodfill_t(); } } // TODO check this: R_FloodFillSkin( byte[] skin, int skinwidth, int skinheight) void R_FloodFillSkin(byte[] skin, int skinwidth, int skinheight) { // byte fillcolor = *skin; // assume this is the pixel to fill int fillcolor = skin[0] & 0xff;// floodfill_t[] fifo = new floodfill_t[FLOODFILL_FIFO_SIZE]; int inpt = 0, outpt = 0; int filledcolor = -1; int i;// for (int j = 0; j < fifo.length; j++) {// fifo[j] = new floodfill_t();// } if (filledcolor == -1) { filledcolor = 0; // attempt to find opaque black for (i = 0; i < 256; ++i) // TODO check this if (d_8to24table[i] == 0xFF000000) { // alpha 1.0 //if (d_8to24table[i] == (255 << 0)) // alpha 1.0 filledcolor = i; break; } } // can't fill to filled color or to transparent color (used as visited marker) if ((fillcolor == filledcolor) || (fillcolor == 255)) { return; } fifo[inpt].x = 0; fifo[inpt].y = 0; inpt = (inpt + 1) & FLOODFILL_FIFO_MASK; while (outpt != inpt) { int x = fifo[outpt].x; int y = fifo[outpt].y; int fdc = filledcolor; // byte *pos = &skin[x + skinwidth * y]; int pos = x + skinwidth * y; // outpt = (outpt + 1) & FLOODFILL_FIFO_MASK; int off, dx, dy; if (x > 0) { // FLOODFILL_STEP( -1, -1, 0 ); off = -1; dx = -1; dy = 0; if (skin[pos + off] == (byte) fillcolor) { skin[pos + off] = (byte) 255; fifo[inpt].x = (short) (x + dx); fifo[inpt].y = (short) (y + dy); inpt = (inpt + 1) & FLOODFILL_FIFO_MASK; } else if (skin[pos + off] != (byte) 255) fdc = skin[pos + off] & 0xff; } if (x < skinwidth - 1) { // FLOODFILL_STEP( 1, 1, 0 ); off = 1; dx = 1; dy = 0; if (skin[pos + off] == (byte) fillcolor) { skin[pos + off] = (byte) 255; fifo[inpt].x = (short) (x + dx); fifo[inpt].y = (short) (y + dy); inpt = (inpt + 1) & FLOODFILL_FIFO_MASK; } else if (skin[pos + off] != (byte) 255) fdc = skin[pos + off] & 0xff; } if (y > 0) { // FLOODFILL_STEP( -skinwidth, 0, -1 ); off = -skinwidth; dx = 0; dy = -1; if (skin[pos + off] == (byte) fillcolor) { skin[pos + off] = (byte) 255; fifo[inpt].x = (short) (x + dx); fifo[inpt].y = (short) (y + dy); inpt = (inpt + 1) & FLOODFILL_FIFO_MASK; } else if (skin[pos + off] != (byte) 255) fdc = skin[pos + off] & 0xff; } if (y < skinheight - 1) { // FLOODFILL_STEP( skinwidth, 0, 1 ); off = skinwidth; dx = 0; dy = 1; if (skin[pos + off] == (byte) fillcolor) { skin[pos + off] = (byte) 255; fifo[inpt].x = (short) (x + dx); fifo[inpt].y = (short) (y + dy); inpt = (inpt + 1) & FLOODFILL_FIFO_MASK; } else if (skin[pos + off] != (byte) 255) fdc = skin[pos + off] & 0xff; } skin[x + skinwidth * y] = (byte) fdc; } } // ======================================================= /* ================ GL_ResampleTexture ================ */ // cwei :-) void GL_ResampleTexture(int[] in, int inwidth, int inheight, int[] out, int outwidth, int outheight) { // int i, j; // unsigned *inrow, *inrow2; // int frac, fracstep; // int[] p1 = new int[1024]; // int[] p2 = new int[1024]; // // *** this source do the same *** BufferedImage image = new BufferedImage(inwidth, inheight, BufferedImage.TYPE_INT_ARGB); image.setRGB(0, 0, inwidth, inheight, in, 0, inwidth); AffineTransformOp op = new AffineTransformOp( AffineTransform.getScaleInstance(outwidth * 1.0 / inwidth, outheight * 1.0 / inheight), AffineTransformOp.TYPE_NEAREST_NEIGHBOR); BufferedImage tmp = op.filter(image, null); tmp.getRGB(0, 0, outwidth, outheight, out, 0, outwidth); // *** end *** // byte *pix1, *pix2, *pix3, *pix4; // // fracstep = inwidth*0x10000/outwidth; // // frac = fracstep>>2; // for (i=0 ; i<outwidth ; i++) // { // p1[i] = 4*(frac>>16); // frac += fracstep; // } // frac = 3*(fracstep>>2); // for (i=0 ; i<outwidth ; i++) // { // p2[i] = 4*(frac>>16); // frac += fracstep; // } // // for (i=0 ; i<outheight ; i++, out += outwidth) // { // inrow = in + inwidth*(int)((i+0.25)*inheight/outheight); // inrow2 = in + inwidth*(int)((i+0.75)*inheight/outheight); // frac = fracstep >> 1; // for (j=0 ; j<outwidth ; j++) // { // pix1 = (byte *)inrow + p1[j]; // pix2 = (byte *)inrow + p2[j]; // pix3 = (byte *)inrow2 + p1[j]; // pix4 = (byte *)inrow2 + p2[j]; // ((byte *)(out+j))[0] = (pix1[0] + pix2[0] + pix3[0] + pix4[0])>>2; // ((byte *)(out+j))[1] = (pix1[1] + pix2[1] + pix3[1] + pix4[1])>>2; // ((byte *)(out+j))[2] = (pix1[2] + pix2[2] + pix3[2] + pix4[2])>>2; // ((byte *)(out+j))[3] = (pix1[3] + pix2[3] + pix3[3] + pix4[3])>>2; // } // } } /* ================ GL_LightScaleTexture Scale up the pixel values in a texture to increase the lighting range ================ */ void GL_LightScaleTexture(int[] in, int inwidth, int inheight, boolean only_gamma) { if (only_gamma) { int i, c; int r, g, b, color; c = inwidth * inheight; for (i = 0; i < c; i++) { color = in[i]; r = (color >> 0) & 0xFF; g = (color >> 8) & 0xFF; b = (color >> 16) & 0xFF; r = gammatable[r] & 0xFF; g = gammatable[g] & 0xFF; b = gammatable[b] & 0xFF; in[i] = (r << 0) | (g << 8) | (b << 16) | (color & 0xFF000000); } } else { int i, c; int r, g, b, color; c = inwidth * inheight; for (i = 0; i < c; i++) { color = in[i]; r = (color >> 0) & 0xFF; g = (color >> 8) & 0xFF; b = (color >> 16) & 0xFF; r = gammatable[intensitytable[r] & 0xFF] & 0xFF; g = gammatable[intensitytable[g] & 0xFF] & 0xFF; b = gammatable[intensitytable[b] & 0xFF] & 0xFF; in[i] = (r << 0) | (g << 8) | (b << 16) | (color & 0xFF000000); } } } /* ================ GL_MipMap Operates in place, quartering the size of the texture ================ */ void GL_MipMap(int[] in, int width, int height) { int i, j; int[] out; out = in; int inIndex = 0; int outIndex = 0; int r, g, b, a; int p1, p2, p3, p4; for (i = 0; i < height; i += 2, inIndex += width) { for (j = 0; j < width; j += 2, outIndex += 1, inIndex += 2) { p1 = in[inIndex + 0]; p2 = in[inIndex + 1]; p3 = in[inIndex + width + 0]; p4 = in[inIndex + width + 1]; r = (((p1 >> 0) & 0xFF) + ((p2 >> 0) & 0xFF) + ((p3 >> 0) & 0xFF) + ((p4 >> 0) & 0xFF)) >> 2; g = (((p1 >> 8) & 0xFF) + ((p2 >> 8) & 0xFF) + ((p3 >> 8) & 0xFF) + ((p4 >> 8) & 0xFF)) >> 2; b = (((p1 >> 16) & 0xFF) + ((p2 >> 16) & 0xFF) + ((p3 >> 16) & 0xFF) + ((p4 >> 16) & 0xFF)) >> 2; a = (((p1 >> 24) & 0xFF) + ((p2 >> 24) & 0xFF) + ((p3 >> 24) & 0xFF) + ((p4 >> 24) & 0xFF)) >> 2; out[outIndex] = (r << 0) | (g << 8) | (b << 16) | (a << 24); } } } /* =============== GL_Upload32 Returns has_alpha =============== */ void GL_BuildPalettedTexture(ByteBuffer paletted_texture, int[] scaled, int scaled_width, int scaled_height) { int r, g, b, c; int size = scaled_width * scaled_height; for (int i = 0; i < size; i++) { r = (scaled[i] >> 3) & 31; g = (scaled[i] >> 10) & 63; b = (scaled[i] >> 19) & 31; c = r | (g << 5) | (b << 11); paletted_texture.put(i, gl_state.d_16to8table[c]); } } int upload_width, upload_height; boolean uploaded_paletted; /* =============== GL_Upload32 Returns has_alpha =============== */ int[] scaled = new int[256 * 256]; //byte[] paletted_texture = new byte[256 * 256]; ByteBuffer paletted_texture=BufferUtils.createByteBuffer(256*256); IntBuffer tex = Lib.newIntBuffer(512 * 256, ByteOrder.LITTLE_ENDIAN); boolean GL_Upload32(int[] data, int width, int height, boolean mipmap) { int samples; int scaled_width, scaled_height; int i, c; int comp; Arrays.fill(scaled, 0); // Arrays.fill(paletted_texture, (byte)0); paletted_texture.clear(); for (int j=0; j<256*256; j++) paletted_texture.put(j,(byte)0); uploaded_paletted = false; for (scaled_width = 1; scaled_width < width; scaled_width <<= 1); if (gl_round_down.value > 0.0f && scaled_width > width && mipmap) scaled_width >>= 1; for (scaled_height = 1; scaled_height < height; scaled_height <<= 1); if (gl_round_down.value > 0.0f && scaled_height > height && mipmap) scaled_height >>= 1; // let people sample down the world textures for speed if (mipmap) { scaled_width >>= (int) gl_picmip.value; scaled_height >>= (int) gl_picmip.value; } // don't ever bother with >256 textures if (scaled_width > 256) scaled_width = 256; if (scaled_height > 256) scaled_height = 256; if (scaled_width < 1) scaled_width = 1; if (scaled_height < 1) scaled_height = 1; upload_width = scaled_width; upload_height = scaled_height; if (scaled_width * scaled_height > 256 * 256) Com.Error(Defines.ERR_DROP, "GL_Upload32: too big"); // scan the texture for any non-255 alpha c = width * height; samples = gl_solid_format; for (i = 0; i < c; i++) { if ((data[i] & 0xff000000) != 0xff000000) { samples = gl_alpha_format; break; } } if (samples == gl_solid_format) comp = gl_tex_solid_format; else if (samples == gl_alpha_format) comp = gl_tex_alpha_format; else { VID.Printf(Defines.PRINT_ALL, "Unknown number of texture components " + samples + '\n'); comp = samples; } // simulates a goto try { if (scaled_width == width && scaled_height == height) { if (!mipmap) { if (qglColorTableEXT && gl_ext_palettedtexture.value != 0.0f && samples == gl_solid_format) { uploaded_paletted = true; GL_BuildPalettedTexture(paletted_texture, data, scaled_width, scaled_height); gl.glTexImage2D(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -