-->

How to do certain things to 2D images?

2019-03-06 00:03发布

问题:

Calling invert turns the image backwards right to left, i.e. it exchanges the first column for the last column, the second column for the second to last column, and so on until the entire image is reversed.

Calling exchange swaps the image area defined by a rectangle with width 180 and height 130 starting at row index 50 and column index 60 for a rectangle of the same size starting at row index 50, and column index 260.

Calling shift finds an image which is hidden in the image data, by saving bit 7, shifting bits 0-6 one position to the left, and setting bit 0 to the previously saved value of bit 7.

Calling swap restores an image in which each pixel has been scrambled by exhanging the bottom 2 bits with the top 2 bits. To do this requires that your code do the same exchange to restore the image.

NOTE: The maximum value of a pixel (Picture.MAXVAL) is 255, so only 8 bits are valid for each pixel. These are numbered bits 0-7, where bit 0 is equal to 1 and bit 7 is equal to 128. There are no negative values allowed.

I got invert to work, however, I got it to invert upside down not right to left.Can someone help me how to make it go from right to left? like what part should I look at or how it should be fixed.

as for exchange, I am close but not quite.

swap and shift, I don't know how to go about it.

public class pictures {

// Picture object
Picture picture = null;

// Image data
int height;
int width;
int image[][];

// Constructor
public pictures() {
    picture = new Picture();
}

// Read the image
public void readImage(String inFile) {
    System.out.println("Reading image: " + inFile);
    try {
        picture.readPGM(inFile);
        height = picture.getHeight();
        width  = picture.getWidth();
        image  = picture.getData();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

// Write the image
public void writeImage(String outFile) {
    System.out.println("Writing image: " + outFile);
    try {
        picture.setData(image);
        picture.writePGM(outFile);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

// Get image data
public int[][] imageData() {
    return image;
}

// Invert the image
public void invert() {

    int right;
    int left;
    for (right = 0, left = height - 1; right < left; right++, left--) {
        int[] pic = image[left];
        image[left] = image[right];
        image[right] = pic;
    }
}

// Exchange the image
public void exchange() {

    for (int col = 60; col < 240; col++)
     {
        for (int row = 50; row < 180; row++) {

            int pic= image[row][col];
            image[row][col] = image[row][col+240];
            image[row][col+240] = pic;
        }
    }
}



// Swap 
public void swap() {

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {

            int pic = image[row][col];
            int top  = (pic & 0b11110000) >> 2;
            int bottom = (pic & 0b00001111) << 2;
            pic = top | bottom;
            image[row][col] = pic;
        }
    }
}

public void shift() {
标签: java invert