How do I convert picture formats to pixel vector ?

I have a c++ routin that analyse pictures.

The argument list is:analyse(int x, int y, vector<int> pixels)

where x and y are the dimension of the picture, and

pixels.at(0) == the first R value

pixels.at(1) == the first G value

pixels.at(2) == the first B value

pixels.at(3) == the second R value,,and so on

My goal is to make this routin private and use it from within new methods added.

Ex:

analyse(jpgpic & picture){

// convert the picture and call analyse(int x, int y, vector<int> pixels)

// NOTE: jpgpic is a matter if "seudo".

}

So, what I need to know is how to make this convertion from jpg, and bmp ictures (this list of functions will grow and take in acount all formats.

I would be very glad if anyone could help me with this,,or give me a startpoint.

Thanks/carlos

[907 byte] By [t95_des] at [2007-9-27 1:47:04]
# 1

one approach would be to simply load the image files (for example using a media tracker) and "convert" them into a bufferedImage.

for example:

Image img = Toolkit.getDefaultToolkit().getImage(url);

BufferdImage buf = new BufferdImage(img.getWidth(), img.getHeight, BufferedImage.TYPE_INT_RGB);

Graphics2D gBuf = buf.createGraphics();

now you can draw the loaded file into the buffered image,

gBuf.drawImage(img, 0,0,comp);

which means that you have access to all the classes of the BufferedImage like Raster, DataBuffer, SampleModel, etc. which you need in order to access the RGB values of each pixel.

For example, if you have a RGB image stored in a DataBuffer of type BYTE with a , each pixel would be composed like:

DataBufferByte db = ((DataBufferByte) buf.getRaster().getDataBuffer()).getData();

//db would be to what you refer as vector, I guess

for (i=0; i<db.length; i+=3){

byte red = db;

byte green = db[i+1];

byte blue = db[i+2];

}

you'll find what the documentation you need in the java.awt.image package. good luck!

>

toytic at 2007-7-4 20:13:00 > top of Java-index,Security,Cryptography...
# 2

sorry, it's not very recommendable to construct the buffered image of type INT and try to retrieve the array of data of type BYTE. Both should match. In the above example, therefore try instead:

DataBufferInt db = ((DataBufferInt) db.getRaster().getDataBuffer()).getData();

then you would have to mask the individual RGB components

int red= db & 0xff0000;

int green = db & 0x00ff00;

int blue= db & 0x0000ff;

toytic at 2007-7-4 20:13:00 > top of Java-index,Security,Cryptography...
# 3

how about changing to pixel array ?

//There are 3 classes, please give a *.gif to try, what u need to focus is the ImageData.java

import java.awt.*;

import java.awt.datatransfer.*;

import java.awt.event.*;

import java.awt.image.*;

import javax.swing.*;

public class ImageEditor extends JPanel {

public final static int LINE_WIDTH = 2;

protected ImageIcon icon;

protected Point start = new Point(0, 0);

protected Point finish = new Point(0, 0);

protected Point pastePoint;

protected JPopupMenu popupMenu;

protected AbstractAction cutAction;

protected AbstractAction copyAction;

protected AbstractAction pasteAction;

public static void main(String[] args) {

if (args.length == 0) {

System.out.println("You must specify the name of an image file");

return;

}

ImageEditor editor = new ImageEditor(args[0]);

JFrame f = new JFrame(args[0]);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setContentPane(editor);

f.setSize(400, 300);

f.setVisible(true);

}

public ImageEditor(String name) {

super();

buildPopupMenu();

setBackground(Color.black);

setLayout(new GridLayout(1, 1, 0, 0));

icon = new ImageIcon(name);

JLabel label = new JLabel(icon);

label.setHorizontalAlignment(SwingConstants.LEFT);

label.setVerticalAlignment(SwingConstants.TOP);

label.addMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent event) {

handleMouseDown(event);

}

});

label.addMouseMotionListener(new MouseMotionAdapter() {

public void mouseDragged(MouseEvent event) {

handleMouseDrag(event);

}

});

JScrollPane jsp = new JScrollPane(label);

add(jsp);

}

protected void handleMouseDown(MouseEvent event) {

if ((event.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {

start = event.getPoint();

finish = event.getPoint();

}

else if ((event.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {

displayPopupMenu(event.getPoint());

pastePoint = event.getPoint();

}

}

protected void handleMouseDrag(MouseEvent event) {

finish = event.getPoint();

repaint();

}

protected void buildPopupMenu() {

popupMenu = new JPopupMenu();

copyAction = new AbstractAction("Copy") {

public void actionPerformed(ActionEvent event) {

performCopy();

}

};

popupMenu.add(copyAction);

cutAction = new AbstractAction("Cut") {

public void actionPerformed(ActionEvent event) {

performCut();

}

};

popupMenu.add(cutAction);

pasteAction = new AbstractAction("Paste") {

public void actionPerformed(ActionEvent event) {

performPaste();

}

};

popupMenu.add(pasteAction);

}

protected void displayPopupMenu(Point p) {

Clipboard cb = getToolkit().getSystemClipboard();

Transferable t = cb.getContents(this);

boolean isSelected = !(start.equals(finish));

cutAction.setEnabled(isSelected);

copyAction.setEnabled(isSelected);

boolean canPaste = ((t != null) &&

(t.isDataFlavorSupported(

ImageSelection.IMAGE_DATA_FLAVOR)));

pasteAction.setEnabled(canPaste);

popupMenu.show(this, p.x, p.y);

}

protected void performCopy() {

Rectangle r = getSelectedArea();

int[] pixels = getPixels(r);

ImageData data = new ImageData(r.width, r.height, pixels);

ImageSelection selection = new ImageSelection(data);

Clipboard cb = getToolkit().getSystemClipboard();

cb.setContents(selection, selection);

}

protected void performCut() {

Rectangle r = getSelectedArea();

int[] pixels = getPixels(r);

ImageData data = new ImageData(r.width, r.height, pixels);

ImageSelection selection = new ImageSelection(data);

Clipboard cb = getToolkit().getSystemClipboard();

cb.setContents(selection, selection);

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

pixels = 0;

}

setPixels(pixels, r);

}

protected void performPaste() {

Clipboard cb = getToolkit().getSystemClipboard();

try {

Transferable t = cb.getContents(this);

if (t.isDataFlavorSupported(

ImageSelection.IMAGE_DATA_FLAVOR)) {

ImageData data = (ImageData)(t.getTransferData(

ImageSelection.IMAGE_DATA_FLAVOR));

Rectangle area = new Rectangle(start.x, start.y,

data.getWidth(), data.getHeight());

int[] pixels = data.getPixelData();

setPixels(pixels, area);

}

}

catch (Exception e) {

JOptionPane.showMessageDialog(this,

"Unable to paste clipboard data");

}

}

protected Rectangle getSelectedArea() {

int width = finish.x - start.x;

int height = finish.y - start.y;

return new Rectangle(start.x, start.y, width, height);

}

protected int[] getPixels(Rectangle area) {

int[] pixels = new int[area.width * area.height];

PixelGrabber pg = new PixelGrabber(icon.getImage(), area.x,

area.y, area.width,

area.height, pixels, 0,

area.width);

try {

pg.grabPixels();

} catch (Exception e) {}

;

return pixels;

}

protected void setPixels(int[] newPixels, Rectangle area) {

int pixel;

Image image = icon.getImage();

int imageWidth = icon.getIconWidth();

int imageHeight = icon.getIconHeight();

int[] oldPixels = new int[imageWidth * imageHeight];

PixelGrabber pg = new PixelGrabber(image, 0, 0, imageWidth,

imageHeight, oldPixels, 0,

imageWidth);

try {

pg.grabPixels();

} catch (Exception e) {}

;

for (int y = 0; y < area.height; y++) {

if (imageHeight <= area.y + y) {

break;

}

for (int x = 0; x < area.width; x++) {

if (imageWidth <= area.x + x) {

break;

}

oldPixels[((area.y + y) * imageWidth) + area.x + x] =

newPixels[(area.width * y) + x];

}

}

MemoryImageSource mis = new MemoryImageSource(imageWidth,

imageHeight, oldPixels, 0, imageWidth);

icon.setImage(createImage(mis));

repaint();

}

public void paint(Graphics g) {

super.paint(g);

int width = finish.x - start.x;

int height = finish.y - start.y;

if ((width > 0) && (height > 0)) {

g.setColor(Color.blue);

for (int i = 0; i < LINE_WIDTH; i++) {

g.drawRect(start.x + i, start.y + i, width, height);

}

}

}

}

import java.awt.*;

import java.awt.datatransfer.*;

import java.awt.image.*;

import java.io.*;

import com.sun.image.codec.jpeg.*;

public class ImageSelection implements Transferable, ClipboardOwner {

protected ImageData imageData;

public final static DataFlavor IMAGE_DATA_FLAVOR =

new DataFlavor(ImageData.class, "Image Data");

public final static DataFlavor JPEG_MIME_FLAVOR =

new DataFlavor("image/jpeg", "JPEG Image Data");

protected final static DataFlavor[] flavors = {

JPEG_MIME_FLAVOR, IMAGE_DATA_FLAVOR

};

public ImageSelection(ImageData data) {

imageData = data;

}

public Object getTransferData(DataFlavor flavor)

throws java.io.IOException, UnsupportedFlavorException {

if (flavor.equals(IMAGE_DATA_FLAVOR)) {

return imageData;

} else if (flavor.equals(JPEG_MIME_FLAVOR)) {

return getJPEGInputStream();

}

throw new UnsupportedFlavorException(flavor);

}

protected InputStream getJPEGInputStream() throws IOException {

int width = imageData.getWidth();

int height = imageData.getHeight();

MemoryImageSource mis = new MemoryImageSource(width, height,

imageData.getPixelData(), 0, width);

BufferedImage bi =

new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);

Graphics2D g2d = bi.createGraphics();

Image img = Toolkit.getDefaultToolkit().createImage(mis);

g2d.drawImage(img, 0, 0, null);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

JPEGImageEncoder jie = JPEGCodec.createJPEGEncoder(baos);

jie.encode(bi);

baos.close();

return new ByteArrayInputStream(baos.toByteArray());

}

public DataFlavor[] getTransferDataFlavors() {

return flavors;

}

public boolean isDataFlavorSupported(DataFlavor flavor) {

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

if (flavor.equals(flavors)) {

return true;

}

}

return false;

}

public void lostOwnership(Clipboard cb, Transferable t) {}

}

public class ImageData implements java.io.Serializable {

protected int width;

protected int height;

protected int[] pixelData;

public ImageData(int width, int height, int[] pixels) {

this.width = width;

this.height = height;

pixelData = (int[])(pixels.clone());

}

public int getWidth() {

return width;

}

public int getHeight() {

return height;

}

public int[] getPixelData() {

return pixelData;

}

}

yundi at 2007-7-4 20:13:00 > top of Java-index,Security,Cryptography...