Reputation: 8720
I am displaying an Image from a list of Pixels as follows (this works):
private Image GetImage()
{
PaletteData palette=new PaletteData(0xff0000,0x00ff00,0x0000ff);
ImageData imageData = new ImageData(bmpHW, bmpHW,32,palette);
currentImagePixelVec = getPixelsFromBMP(0, 0, graphicsMemory);
int pixelVecLoc=0;
for (int h = 0; h<bmpHW; h++)
{
for (int w = 0; w<bmpHW; w++)
{
int p = 0;
Pixel pixel = currentImagePixelVec.get(pixelVecLoc);
p = (pixel.Alpha<<24) | (pixel.Red<<16) | (pixel.Green<<8) | pixel.Blue;
imageData.setPixel(w, h, p);
pixelVecLoc++;
}
}
imageData = imageData.scaledTo(700, 700);
Image image = ImageDescriptor.createFromImageData(imageData).createImage();
return image;
}
I am getting the user to select a rectangle of the Image as follows(this works):
if(drag)
{
GC gc1 = e.gc;
//gc.setBackground(top.getDefault().getSystemColor(SWT.COLOR_BLACK));
gc1.setAlpha(128);
int minX = Math.min(startX, endX);
int minY = Math.min(startY, endY);
int maxX = Math.max(startX, endX);
int maxY = Math.max(startY, endY);
int width = maxX - minX;
int height = maxY - minY;
gc1.fillRectangle(minX, minY, width, height);
}
I would like to create a new image from the rectangle selection:
private Image GetZoomedImage()
{
int minX = Math.min(startX, endX);
int minY = Math.min(startY, endY);
int maxX = Math.max(startX, endX);
int maxY = Math.max(startY, endY);
int width = maxX - minX;
int height = maxY - minY;
PaletteData palette=new PaletteData(0xff0000,0x00ff00,0x0000ff);
ImageData imageData = new ImageData(1300, 1300,32,palette);
int newHeight = 0;
int newWidth = 0;
for (int h = minX; h<maxX; h++)
{
for (int w = minY; w<maxY; w++)
{
int p = 0;
//Pixel pixel = currentImagePixelVec.get((h * w)-1);
int actualPix = imageDisplayed.getImageData().getPixel(h, w);
//p = (pixel.Alpha<<24) | (pixel.Red<<16) | (pixel.Green<<8) | pixel.Blue;
System.out.println("Pixel: " + Integer.toString(actualPix));
//imageData.setPixel(newWidth,newHeight, p);
imageData.setPixel(newWidth,newHeight, actualPix);
newWidth++;
}
newHeight++;
}
imageData = imageData.scaledTo(700, 700);
Image image = ImageDescriptor.createFromImageData(imageData).createImage();
return image;
}
Am I on the right track on this one?
Upvotes: 0
Views: 158
Reputation: 111142
You should only get the image data of the existing image once as this can be an expensive operation:
ImageData imageDisplayedData = imageDisplayed.getImageData();
Your new image data should be just the width and height you want:
ImageData imageData = new ImageData(width, height, 32, palette);
You can use the
public void getPixels(int x, int y, int getWidth, int[] pixels, int startIndex)
public void setPixels(int x, int y, int putWidth, int[] pixels, int startIndex)
methods of ImageData
to read and write the data for a whole row at a time:
int [] pixels = new int [width];
int newRow = 0;
for (int row = minY; row < maxY; row++)
{
imageDisplayedData.getPixels(minX, row, width, pixels, 0);
imageData.setPixels(0, newRow, width, pixels, 0);
newRow++;
}
Upvotes: 1