Results 1 to 2 of 2
  1. #1
    shaungoater is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default Image class (getRGB)

    I am trying to use the following method to return the number of red blue and green pixels contained in a part of an image:

    Java Code:
    public int[] getRGB(int startX,int startY,int w, int h,int[] rgbArray,
                                       int offset,int scansize);

    However i don't understand what all the parameters are for. Specifically the offset and scansize. Also, for example, if i need to split an image 9*9 (81 parts) do i need to call this method 81 times and set up 81 different arrays to store the rgb data.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    don't understand what all the parameters are for. Specifically the offset and scansize
    Depends on what you're doing. Seems like we just have to play around with it a little to see how it's working. I tried to make up a small example below.
    if i need to split an image 9*9 (81 parts) do i need to call this method 81 times and set up 81 different arrays to store the rgb data.
    You could do it that way. Or you could get all of the image data in a single rgbArray and use some clever indexing to access the data you want from the array, storing it in a smaller block-size array as you go.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    public class BlockTest {
        BufferedImage image;
        JLabel left;
        JLabel right;
        int cols = 9;
        int rows = 9;
    
        public void setImage(BufferedImage image) {
            right.setIcon(new ImageIcon(image));
        }
    
        private JPanel getContent() {
            left = getLeft();
            new CellSelector(this);
            right = new JLabel((ImageIcon)null, JLabel.CENTER);
            right.setPreferredSize(left.getPreferredSize());
            JPanel panel = new JPanel(new GridBagLayout());
            panel.setBackground(Color.black);
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(5,5,5,5);
            gbc.weightx = 1.0;
            panel.add(left, gbc);
            panel.add(right, gbc);
            return panel;
        }
    
        private JLabel getLeft() {
            int w = 360;
            int h = 360;
            int type = BufferedImage.TYPE_INT_RGB;
            image = new BufferedImage(w, h, type);
            Graphics2D g2 = image.createGraphics();
            g2.setPaint(new GradientPaint(0,0,Color.green.darker(),
                                          w/4,h/4,Color.red, true));
            g2.fillRect(0,0,w,h);
            g2.setPaint(Color.blue);
            for(int j = 0; j <= rows; j++) {
                int y = j*(w/rows);
                g2.drawLine(0,y,h,y);
            }
            for(int j = 0; j <= cols; j++) {
                int x = j*(h/rows);
                g2.drawLine(x,0,x,h);
            }
            g2.dispose();
            return new JLabel(new ImageIcon(image), JLabel.CENTER);
        }
    
        public static void main(String[] args) {
            BlockTest test = new BlockTest();
            JFrame f = new JFrame("Click any cell");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getContent());
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }
    
    class CellSelector extends MouseAdapter {
        BlockTest view;
        JLabel target;
        int[] rgbArray;
    
        CellSelector(BlockTest bt) {
            view = bt;
            target = view.left;
            target.addMouseListener(this);
            // Create rgbArray with same size as left image.
            int w = view.image.getWidth();
            int h = view.image.getHeight();
            rgbArray = new int[w*h];
        }
    
        public void mousePressed(MouseEvent e) {
            Point p = e.getPoint();
    
            // Locate origin of centered image inside left label.
            int targetWidth = target.getWidth();
            int targetHeight = target.getHeight();
            int imageWidth = view.image.getWidth();
            int imageHeight = view.image.getHeight();
            int x0 = (targetWidth - imageWidth)/2;
            int y0 = (targetHeight - imageHeight)/2;
    
            // Make sure mouse selection is over image.
            // User may have changed the layout or something.
            Rectangle r = new Rectangle(x0, y0, imageWidth, imageHeight);
            if(!r.contains(p))
                return;
    
            // Size of each cell in grid.
            int cellWidth = imageWidth/view.cols;
            int cellHeight = imageHeight/view.rows;
    
            // Indices of row and column selected by the user.
            int cellColIndex = (p.x - x0)/cellWidth;
            int cellRowIndex = (p.y - y0)/cellHeight;
            //System.out.printf("cellRowIndex = %d  cellColIndex = %d%n",
            //                   cellRowIndex, cellColIndex);
    
            // Selected location in image coordinate system - not used.
            int imageX = p.x - x0;
            int imageY = p.y - y0;
    
            // Origin of selected cell.
            int x = cellColIndex * cellWidth;
            int y = cellRowIndex * cellHeight;
    
            // Collect the rgb data for the selected
            // cell and write it into rgbArray at the
            // cell location (offset) within the array.
    
            //  ********  Two options:  ********
            // Write directly into the rgbArray.
            writeToArray(x, y, cellWidth, cellHeight, imageWidth);
    
            // Get the data and transfer it into the rgbArray.
            //transferToArray(x, y, cellWidth, cellHeight, imageWidth);
            //  ********************************
    
            // Set rgbArray data into a new image and
            // mount the image in the right label.
            BufferedImage image = new BufferedImage(imageWidth, imageHeight,
                                                    view.image.getType());
            image.setRGB(0, 0, imageWidth, imageHeight,
                         rgbArray, 0, imageWidth);
            view.setImage(image);
        }
    
        /** Write data directly into rgbArray. */
        private void writeToArray(int x, int y, int w, int h, int imageWidth) {
            // Calculate offset into rgbArray to begin
            // writing the rgb data of this cell.
            int offset = y*imageWidth + x;
            int scansize = imageWidth;
            view.image.getRGB(x, y, w, h, rgbArray, offset, scansize);
        }
    
    
        /** Extract cell block data and write it into rgbArray. */
        private void transferToArray(int x, int y, int w, int h, int imageWidth) {
            int offset = 0;
            int scansize = w;
            int[] rgbs = view.image.getRGB(x, y, w, h, null, offset, scansize);
            // Write the cell data into rgbArray.
            int count = 0;
            for(int j = y; j < y+h; j++) {
                for(int k = x; k < x+w; k++) {
                    int index = j*imageWidth + k;
                    rgbArray[index] = rgbs[count++];
                }
            }
        }
    }

Similar Threads

  1. Replies: 0
    Last Post: 04-02-2008, 07:04 AM
  2. Replies: 0
    Last Post: 02-17-2008, 09:01 AM
  3. Inner class accessing outer class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-17-2008, 08:59 AM
  4. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  5. Replies: 5
    Last Post: 01-03-2008, 07:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •