use of java.awt.image.MemoryImageSource in project JMRI by JMRI.
the class NamedIcon method createRotatedImage.
/**
* The following was based on a text-rotating applet from David Risner,
* available at http://www.risner.org/java/rotate_text.html
*
* @param pImage Image to transform
* @param pComponent Component containing the image, needed to obtain a
* MediaTracker to process the image consistently with
* display
* @param pRotation 0-3 number of 90-degree rotations needed
* @return new Image object containing the rotated input image
*/
public Image createRotatedImage(Image pImage, Component pComponent, int pRotation) {
if (log.isDebugEnabled()) {
log.debug("createRotatedImage: pRotation= " + pRotation + ", mRotation= " + mRotation);
}
if (pRotation == 0) {
return pImage;
}
MediaTracker mt = new MediaTracker(pComponent);
mt.addImage(pImage, 0);
try {
mt.waitForAll();
} catch (InterruptedException ie) {
// retain if needed later
Thread.currentThread().interrupt();
}
int w = pImage.getWidth(null);
int h = pImage.getHeight(null);
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(pImage, 0, 0, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (InterruptedException ie) {
}
int[] newPixels = new int[w * h];
// transform the pixels
MemoryImageSource imageSource = null;
switch(pRotation) {
case // 90 degrees
1:
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
newPixels[x * h + y] = pixels[y * w + (w - 1 - x)];
}
}
imageSource = new MemoryImageSource(h, w, ColorModel.getRGBdefault(), newPixels, 0, h);
break;
case // 180 degrees
2:
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
newPixels[x * h + y] = pixels[(w - 1 - x) * h + (h - 1 - y)];
}
}
imageSource = new MemoryImageSource(w, h, ColorModel.getRGBdefault(), newPixels, 0, w);
break;
case // 270 degrees
3:
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
newPixels[x * h + y] = pixels[(h - 1 - y) * w + x];
}
}
imageSource = new MemoryImageSource(h, w, ColorModel.getRGBdefault(), newPixels, 0, h);
break;
default:
log.warn("Unhandled rotation code: {}", pRotation);
break;
}
Image myImage = pComponent.createImage(imageSource);
mt.addImage(myImage, 1);
try {
mt.waitForAll();
} catch (InterruptedException ie) {
}
return myImage;
}
use of java.awt.image.MemoryImageSource in project Lucee by lucee.
the class ImageCombiningFilter method filter.
public ImageProducer filter(Image image1, Image image2, int x, int y, int w, int h) {
int[] pixels1 = new int[w * h];
int[] pixels2 = new int[w * h];
int[] pixels3 = new int[w * h];
PixelGrabber pg1 = new PixelGrabber(image1, x, y, w, h, pixels1, 0, w);
PixelGrabber pg2 = new PixelGrabber(image2, x, y, w, h, pixels2, 0, w);
try {
pg1.grabPixels();
pg2.grabPixels();
} catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
return null;
}
if ((pg1.status() & ImageObserver.ABORT) != 0) {
System.err.println("image fetch aborted or errored");
return null;
}
if ((pg2.status() & ImageObserver.ABORT) != 0) {
System.err.println("image fetch aborted or errored");
return null;
}
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
int k = j * w + i;
pixels3[k] = filterRGB(x + i, y + j, pixels1[k], pixels2[k]);
}
}
return new MemoryImageSource(w, h, pixels3, 0, w);
}
use of java.awt.image.MemoryImageSource in project chipKIT32-MAX by chipKIT32.
the class PGraphics2D method allocate.
//public void setParent(PApplet parent)
//public void setPrimary(boolean primary)
//public void setPath(String path)
//public void setSize(int iwidth, int iheight)
protected void allocate() {
pixelCount = width * height;
pixels = new int[pixelCount];
if (primarySurface) {
cm = new DirectColorModel(32, 0x00ff0000, 0x0000ff00, 0x000000ff);
;
mis = new MemoryImageSource(width, height, pixels, 0, width);
mis.setFullBufferUpdates(true);
mis.setAnimated(true);
image = Toolkit.getDefaultToolkit().createImage(mis);
}
}
use of java.awt.image.MemoryImageSource in project narchy by automenta.
the class RdesktopCanvas method createCursor.
/**
* Create an AWT Cursor object
*
* @param x
* @param y
* @param w
* @param h
* @param andmask
* @param xormask
* @param cache_idx
* @return Created Cursor
*/
public Cursor createCursor(int cache_idx, int x, int y, int w, int h, byte[] andmask, byte[] xormask, int bpp) {
byte[] cursor, mask;
Point p = new Point(x, y);
int pmask = 0, pcursor = 0, pandmask = 0;
int scanline, offset, delta;
int i, j, k = 0;
scanline = (w + 7) / 8;
offset = scanline * h;
cursor = new byte[offset];
mask = new byte[offset];
if (bpp == 1) {
offset = 0;
delta = scanline;
} else {
offset = scanline * h - scanline;
delta = -scanline;
}
for (i = 0; i < h; i++) {
pcursor = offset;
pmask = offset;
for (j = 0; j < scanline; j++) {
for (int nextbit = 0x80; nextbit != 0; nextbit >>= 1) {
// -----
int rv = 0;
int pxormask = 0;
int s8 = 0;
switch(bpp) {
case 1:
s8 = pxormask + (k / 8);
rv = xormask[s8] & (0x80 >> (k % 8));
rv = rv != 0 ? 0xffffff : 0;
k += 1;
break;
case 8:
s8 = pxormask + k;
rv = xormask[s8];
rv = rv != 0 ? 0xffffff : 0;
k += 1;
break;
case 15:
{
int temp = (xormask[k] << 8) | xormask[k + 1];
// r
int red = (((temp >> 7) & 0xf8) | ((temp >> 12) & 0x7));
// g
int green = (((temp >> 2) & 0xf8) | ((temp >> 8) & 0x7));
// b
int blue = (((temp << 3) & 0xf8) | ((temp >> 2) & 0x7));
rv = (red << 16) | (green << 8) | blue;
k += 1;
}
break;
case 16:
{
int temp = (xormask[k] << 8) | xormask[k + 1];
int red = ((temp >> 8) & 0xf8) | ((temp >> 13) & 0x7);
int green = ((temp >> 3) & 0xfc) | ((temp >> 9) & 0x3);
int blue = ((temp << 3) & 0xf8) | ((temp >> 2) & 0x7);
rv = (red << 16) | (green << 8) | blue;
k += 1;
}
break;
case 24:
s8 = pxormask + k;
rv = (xormask[s8] << 16) | (xormask[s8 + 1] << 8) | xormask[s8 + 2];
k += 3;
break;
case 32:
s8 = pxormask + k;
rv = (xormask[s8 + 1] << 16) | (xormask[s8 + 2] << 8) | xormask[s8 + 3];
k += 4;
break;
default:
System.out.println("get_next_xor_pixel未知bpp" + bpp);
break;
}
if (rv != 0) {
cursor[pcursor] |= (~(andmask[pandmask]) & nextbit);
mask[pmask] |= nextbit;
} else {
cursor[pcursor] |= ((andmask[pandmask]) & nextbit);
mask[pmask] |= (~(andmask[pandmask]) & nextbit);
}
// -----
}
pandmask++;
pcursor++;
pmask++;
}
offset += delta;
}
int[] cursormap = new int[w * h];
int pcursormap = 0;
int fg = 0xFFFFFFFF;
int bg = 0xFF000000;
pmask = 0;
pcursor = 0;
for (i = 0; i < h; i++) {
for (j = 0; j < scanline; j++) {
for (int nextbit = 0x80; nextbit != 0; nextbit >>= 1) {
boolean isCursor = (cursor[pcursor] & nextbit) != 0;
boolean isMask = (mask[pmask] & nextbit) != 0;
if (isMask) {
if (isCursor) {
cursormap[pcursormap] = fg;
} else {
cursormap[pcursormap] = bg;
}
}
pcursormap++;
}
pcursor++;
pmask++;
}
}
Image wincursor = this.createImage(new MemoryImageSource(w, h, cursormap, 0, w));
return createCustomCursor(wincursor, p, "", cache_idx);
}
use of java.awt.image.MemoryImageSource in project narchy by automenta.
the class ClipBMP method loadbitmap.
/**
* loadbitmap() method converted from Windows C code. Reads only
* uncompressed 24- and 8-bit images. Tested with images saved using
* Microsoft Paint in Windows 95. If the image is not a 24- or 8-bit image,
* the program refuses to even try. I guess one could include 4-bit images
* by masking the byte by first 1100 and then 0011. I am not really
* interested in such images. If a compressed image is attempted, the
* routine will probably fail by generating an IOException. Look for
* variable ncompression to be different from 0 to indicate compression is
* present.
*
* Arguments: sdir and sfile are the result of the FileDialog()
* getDirectory() and getFile() methods.
*
* Returns: Image Object, be sure to check for (Image)null !!!!
*/
public static Image loadbitmap(InputStream fs) {
Image image;
try {
// int bflen = 14; // 14 byte BITMAPFILEHEADER
// byte bf[] = new byte[bflen];
// fs.read(bf, 0, bflen);
// 40-byte BITMAPINFOHEADER
int bilen = 40;
byte[] bi = new byte[bilen];
fs.read(bi, 0, bilen);
// Interperet data.
// int nsize = (((int) bf[5] & 0xff) << 24)
// | (((int) bf[4] & 0xff) << 16)
// | (((int) bf[3] & 0xff) << 8) | (int) bf[2] & 0xff;
// System.out.println("Size of file is :" + nsize);
// int nbisize = (((int) bi[3] & 0xff) << 24)
// | (((int) bi[2] & 0xff) << 16)
// | (((int) bi[1] & 0xff) << 8) | (int) bi[0] & 0xff;
// System.out.println("Size of bitmapinfoheader is :" + nbisize);
int nwidth = (((int) bi[7] & 0xff) << 24) | (((int) bi[6] & 0xff) << 16) | (((int) bi[5] & 0xff) << 8) | (int) bi[4] & 0xff;
// System.out.println("Width is :" + nwidth);
int nheight = (((int) bi[11] & 0xff) << 24) | (((int) bi[10] & 0xff) << 16) | (((int) bi[9] & 0xff) << 8) | (int) bi[8] & 0xff;
// System.out.println("Height is :" + nheight);
// int nplanes = (((int) bi[13] & 0xff) << 8) | (int) bi[12] & 0xff;
// System.out.println("Planes is :" + nplanes);
int nbitcount = (((int) bi[15] & 0xff) << 8) | (int) bi[14] & 0xff;
// System.out.println("BitCount is :" + nbitcount);
// Look for non-zero values to indicate compression
// int ncompression = (((int) bi[19]) << 24) | (((int) bi[18]) <<
// 16)
// | (((int) bi[17]) << 8) | (int) bi[16];
// System.out.println("Compression is :" + ncompression);
int nsizeimage = (((int) bi[23] & 0xff) << 24) | (((int) bi[22] & 0xff) << 16) | (((int) bi[21] & 0xff) << 8) | (int) bi[20] & 0xff;
// System.out.println("SizeImage is :" + nsizeimage);
// int nxpm = (((int) bi[27] & 0xff) << 24)
// | (((int) bi[26] & 0xff) << 16)
// | (((int) bi[25] & 0xff) << 8) | (int) bi[24] & 0xff;
// System.out.println("X-Pixels per meter is :" + nxpm);
// int nypm = (((int) bi[31] & 0xff) << 24)
// | (((int) bi[30] & 0xff) << 16)
// | (((int) bi[29] & 0xff) << 8) | (int) bi[28] & 0xff;
// System.out.println("Y-Pixels per meter is :" + nypm);
int nclrused = (((int) bi[35] & 0xff) << 24) | (((int) bi[34] & 0xff) << 16) | (((int) bi[33] & 0xff) << 8) | (int) bi[32] & 0xff;
if (nbitcount == 24) {
// No Palatte data for 24-bit format but scan lines are
// padded out to even 4-byte boundaries.
int npad = (nsizeimage / nheight) - nwidth * 3;
int[] ndata = new int[nheight * nwidth];
byte[] brgb = new byte[(nwidth + npad) * 3 * nheight];
fs.read(brgb, 0, (nwidth + npad) * 3 * nheight);
int nindex = 0;
for (int j = 0; j < nheight; j++) {
for (int i = 0; i < nwidth; i++) {
ndata[nwidth * (nheight - j - 1) + i] = (255 & 0xff) << 24 | ((brgb[nindex + 2] & 0xff) << 16) | ((brgb[nindex + 1] & 0xff) << 8) | brgb[nindex] & 0xff;
// System.out.println("Encoded Color at ("
// +i+","+j+")is:"+nrgb+" (R,G,B)= (" +((int)(brgb[2]) &
// 0xff)+"," +((int)brgb[1]&0xff)+","
// +((int)brgb[0]&0xff)+")";
nindex += 3;
}
nindex += npad;
}
image = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(nwidth, nheight, ndata, 0, nwidth));
} else if (nbitcount == 16) {
// Have to determine the number of colors, the clrsused
// parameter is dominant if it is greater than zero. If
// zero, calculate colors based on bitsperpixel.
int nNumColors = 0;
if (nclrused > 0) {
nNumColors = nclrused;
} else {
nNumColors = (1 & 0xff) << nbitcount;
}
// Ferret out these cases and fix 'em.
if (nsizeimage == 0) {
nsizeimage = ((((nwidth * nbitcount) + 31) & ~31) >> 3);
nsizeimage *= nheight;
// System.out.println("nsizeimage (backup) is " +
// nsizeimage);
}
// Read the palatte colors.
int[] npalette = new int[nNumColors];
byte[] bpalette = new byte[nNumColors * 4];
fs.read(bpalette, 0, nNumColors * 4);
int nindex8 = 0;
for (int n = 0; n < nNumColors; n++) {
npalette[n] = (255 & 0xff) << 24 | (((int) bpalette[nindex8 + 2] & 0xff) << 16) | (((int) bpalette[nindex8 + 1] & 0xff) << 8) | (int) bpalette[nindex8] & 0xff;
// System.out.println ("Palette Color "+n +"
// is:"+npalette[n]+" (res,R,G,B)=
// ("+((int)(bpalette[nindex8+3]) & 0xff)+","
// +((int)(bpalette[nindex8+2]) & 0xff)+","
// +((int)bpalette[nindex8+1]&0xff)+","
// +((int)bpalette[nindex8]&0xff)+")");
nindex8 += 4;
}
// Read the image data (actually indices into the palette)
// Scan lines are still padded out to even 4-byte
// boundaries.
int npad8 = (nsizeimage / nheight) - nwidth;
// System.out.println("nPad is:" + npad8);
int[] ndata8 = new int[nwidth * nheight];
byte[] bdata = new byte[(nwidth + npad8) * nheight];
fs.read(bdata, 0, (nwidth + npad8) * nheight);
nindex8 = 0;
for (int j8 = 0; j8 < nheight; j8++) {
for (int i8 = 0; i8 < nwidth; i8++) {
ndata8[nwidth * (nheight - j8 - 1) + i8] = npalette[((int) bdata[nindex8] & 0xff)] | npalette[((int) bdata[nindex8 + 1] & 0xff)] << 8;
nindex8 += 2;
}
nindex8 += npad8;
}
image = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(nwidth, nheight, ndata8, 0, nwidth));
} else if (nbitcount == 8) {
// Have to determine the number of colors, the clrsused
// parameter is dominant if it is greater than zero. If
// zero, calculate colors based on bitsperpixel.
int nNumColors = 0;
if (nclrused > 0) {
nNumColors = nclrused;
} else {
nNumColors = (1 & 0xff) << nbitcount;
}
// Ferret out these cases and fix 'em.
if (nsizeimage == 0) {
nsizeimage = ((((nwidth * nbitcount) + 31) & ~31) >> 3);
nsizeimage *= nheight;
// System.out.println("nsizeimage (backup) is " +
// nsizeimage);
}
// Read the palatte colors.
int[] npalette = new int[nNumColors];
byte[] bpalette = new byte[nNumColors * 4];
fs.read(bpalette, 0, nNumColors * 4);
int nindex8 = 0;
for (int n = 0; n < nNumColors; n++) {
npalette[n] = (255 & 0xff) << 24 | (((int) bpalette[nindex8 + 2] & 0xff) << 16) | (((int) bpalette[nindex8 + 1] & 0xff) << 8) | (int) bpalette[nindex8] & 0xff;
// System.out.println ("Palette Color "+n +"
// is:"+npalette[n]+" (res,R,G,B)=
// ("+((int)(bpalette[nindex8+3]) & 0xff)+","
// +((int)(bpalette[nindex8+2]) & 0xff)+","
// +((int)bpalette[nindex8+1]&0xff)+","
// +((int)bpalette[nindex8]&0xff)+")");
nindex8 += 4;
}
// Read the image data (actually indices into the palette)
// Scan lines are still padded out to even 4-byte
// boundaries.
int npad8 = (nsizeimage / nheight) - nwidth;
// System.out.println("nPad is:" + npad8);
int[] ndata8 = new int[nwidth * nheight];
byte[] bdata = new byte[(nwidth + npad8) * nheight];
fs.read(bdata, 0, (nwidth + npad8) * nheight);
nindex8 = 0;
for (int j8 = 0; j8 < nheight; j8++) {
for (int i8 = 0; i8 < nwidth; i8++) {
ndata8[nwidth * (nheight - j8 - 1) + i8] = npalette[((int) bdata[nindex8] & 0xff)];
nindex8++;
}
nindex8 += npad8;
}
image = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(nwidth, nheight, ndata8, 0, nwidth));
} else if (nbitcount == 4) {
// Have to determine the number of colors, the clrsused
// parameter is dominant if it is greater than zero. If
// zero, calculate colors based on bitsperpixel.
int nNumColors = 0;
if (nclrused > 0) {
nNumColors = nclrused;
} else {
nNumColors = (1 & 0xff) << nbitcount;
}
// Ferret out these cases and fix 'em.
if (nsizeimage == 0) {
nsizeimage = ((((nwidth * nbitcount) + 31) & ~31) >> 3);
nsizeimage *= nheight;
// System.out.println("nsizeimage (backup) is " +
// nsizeimage);
}
// Read the palatte colors.
int[] npalette = new int[nNumColors + 1];
byte[] bpalette = new byte[nNumColors * 4];
fs.read(bpalette, 0, nNumColors * 4);
int nindex8 = 0;
for (int n = 0; n < nNumColors; n++) {
npalette[n] = (255 & 0xff) << 24 | (((int) bpalette[nindex8 + 2] & 0xff) << 16) | (((int) bpalette[nindex8 + 1] & 0xff) << 8) | (int) bpalette[nindex8] & 0xff;
nindex8 += 4;
}
// Read the image data (actually indices into the palette)
// Scan lines are still padded out to even 4-byte
// boundaries.
int npad8 = (nsizeimage * 2 / nheight) - nwidth;
// System.out.println("nPad is:" + npad8);
if (npad8 == 4)
npad8 = 0;
int[] ndata8 = new int[nwidth * nheight];
byte[] bdata = new byte[(nwidth / 2 + npad8) * nheight];
// (nwidth)
fs.read(bdata, 0, (nwidth / 2 + npad8) * nheight);
// *
// nheight);
nindex8 = 0;
// nheight);
for (int j8 = 0; j8 < nheight; j8++) {
for (int i8 = 0; i8 < (nwidth) - 1; i8 += 2) {
ndata8[nwidth * (nheight - j8 - 1) + i8] = npalette[((int) (bdata[nindex8] & 0x0f))];
ndata8[nwidth * (nheight - j8 - 1) + i8 + 1] = npalette[((int) (bdata[nindex8] & 0xf0) / 0xf)];
System.out.print("1:" + (bdata[nindex8] & 0x0f) + '\t');
System.out.print("2:" + ((bdata[nindex8] & 0xf0) / 0xf) + '\t');
// System.out.print(nindex8 + "/" + nsizeimage + "\t");
// ndata8[nwidth * j8 + i8] = npalette[((int)
// (bdata[nindex8] & 0x0f))];
// ndata8[nwidth * j8 + i8 + 1] = npalette[((int)
// (bdata[nindex8] & 0xf0) / 0xf)];
// System.out.print("\t" + (nheight * j8 + i8) + "=(" +
// npalette[((int) (bdata[nindex8] & 0x0f))] + ")");
// System.out.print("\t" + (nheight * j8 + i8 + 1) +
// "=(" + npalette[((int) (bdata[nindex8] & 0xf0) /
// 0xf)] + ")");
nindex8++;
}
// nindex8 += npad8;
}
// image = null;
image = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(nwidth, nheight, ndata8, 0, nwidth));
} else {
logger.warn("Not a 24-bit or 8-bit Windows Bitmap, aborting...");
image = (Image) null;
}
fs.close();
return image;
} catch (Exception e) {
// System.out.println("\nCaught exception in loadbitmap: " +
// e.getMessage() + " " + e.getClass().getName());
}
return (Image) null;
}
Aggregations