A photo-editing program stores a grayscale image with pixel values from 0 to 255, where a higher value means greater brightness.
Design a class ImageEditor to accept an image of size rows × cols, where 3 ≤ rows, cols ≤ 6, and perform the following task:
- Brighten the Image: Accept a brightness value b and add it to every pixel. If the result exceeds 255, store 255. Store the brighened image separately and display both the images.
- Identify a Bright Spot: A pixel is a bright spot if its value is greater than all its immediate neighbors (above, below, left and right). For a missing boundary neighbor, use 0. Display the position and value of every bright spot.
Test your program for the following data and some random data:
Example 1
INPUT:
Rows: 4
Columns: 3
250 5 3
6 4 7
1 9 5
4 2 240
Brightest value: 20
OUTPUT:
Original Image:
250 5 3
6 4 7
1 9 5
4 2 240
Brightened Image:
255 25 23
26 24 27
21 29 25
24 22 255
Bright Spots:
Row 1, Column 1: 250
Row 2, Column 3: 7
Row 3, Column 2: 9
Row 4, Column 1: 4
Row 4, Column 3: 240
Example 2
Rows: 3
Columns: 3
5 2 3
1 4 1
2 1 2
Brightness value: 0
OUTPUT:
Original Image:
5 2 3
1 4 1
2 1 2
Brightened Image:
5 2 3
1 4 1
2 1 2
Bright Spots:
Row 1, Column 1: 5
Row 1, Column 3: 3
Row 2, Column 2: 4
Row 3, Column 1: 2
Row 3, Column 3: 2
Example 3
INPUT:
Rows: 2
Columns: 7
OUTPUT:
INVALID INPUT
import java.util.Scanner;
class ImageEditor{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Rows: ");
int rows = in.nextInt();
System.out.print("Columns: ");
int cols = in.nextInt();
if(rows < 3 || rows > 6 || cols < 3 || cols > 6){
System.out.println("INVALID INPUT");
return;
}
int m1[][] = new int[rows][cols];
int m2[][] = new int[rows][cols];
System.out.println("Enter pixel values:");
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
m1[i][j] = in.nextInt();
if(m1[i][j] < 0)
m1[i][j] = 0;
if(m1[i][j] > 255)
m1[i][j] = 255;
}
}
System.out.print("Brightness value: ");
int b = in.nextInt();
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
m2[i][j] = m1[i][j] + b;
if(m2[i][j] > 255)
m2[i][j] = 255;
}
}
System.out.println("Original Image:");
display(m1, rows, cols);
System.out.println("Brightened Image:");
display(m2, rows, cols);
System.out.println("Bright Spots:");
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
int up = 0, down = 0, left = 0, right = 0;
if(i > 0)
up = m1[i - 1][j];
if(i < rows - 1)
down = m1[i + 1][j];
if(j > 0)
left = m1[i][j - 1];
if(j < cols - 1)
right = m1[i][j + 1];
if(m1[i][j] > up && m1[i][j] > down && m1[i][j] > left && m1[i][j] > right)
System.out.println("Row " + (i + 1) + ", Column " + (j + 1) + ": " + m1[i][j]);
}
}
}
public static void display(int a[][], int r, int c){
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
}
}