Project briefing illustration for grayscale filters: a flat tactical intelligence sheet in the style of Star Fox Assault.
ACADEMIC PROJECT / Computer Science Course

Java Grayscale Image Filters

When I revisited my grayscale matrix exercise, I focused on preserving its two independent filters while making boundaries, integer arithmetic, and malformed input explicit.

Javaarraysimage processingtestingrestoration
TRY THE IDEA

Interactive demonstration

A new browser adaptation made in October 2026 using sample data. It illustrates the concepts; it is not the original submitted program.

Input pixels

Filtered output

66871359214717883115165

Each result averages the original pixel and its available up, down, left, and right neighbors. Integer division rounds down. Output never feeds back into the input.

Why I returned to this small image exercise

When I revisited this grayscale exercise, I wanted to understand the decisions hidden inside a small two-dimensional array: what counts as a neighbor, what happens at a corner, and whether a filter reads the original pixels or a partly updated result. Keeping the image small enough to inspect on paper let me explain those decisions precisely.

In October 2026, I revisited the March 2021 source, preserved its two independent filter definitions, and added input validation, controlled errors, and checks. I followed the behavior visible in the program because the original assignment prompt was not recovered.

How I defined the input before filtering

I started with the data contract: a text file gives the row count, column count, and pixel values in row-major order. Each pixel must be an integer from 0 through 255. I can trace both filters with this minimal fixture:

Illustrative text example
2 2
0 100
200 255

In the restored console, I support multiple matrices in one file and validate dimensions before allocating each matrix. After allocation, I require exactly the declared number of pixels for that matrix. Missing pixels and non-integer tokens produce controlled input failures instead of uncaught scanner exceptions.

I also needed the scanner's stopping rule to match its token-based input. I check whether another token exists instead of assuming that another line means a complete image remains. That keeps blank lines as formatting while treating a partial matrix as an error, which makes the console behavior predictable.

How I worked through the cross-shaped neighborhood

I kept the first filter's neighborhood cross-shaped: the current cell plus its up, down, left, and right neighbors. Diagonals do not participate. I include only coordinates inside the matrix, so the divisor follows the number of contributing cells.

For a rectangular image with more than one row and column, I expect three values at a corner, four at an edge, and five in the interior. I use the same bounds rule for degenerate dimensions rather than assuming those counts always apply: a one-pixel image contributes only itself.

I use this compact excerpt to explain the arithmetic:

Illustrative java example
int sum = 0;
int count = 0;
for (int[] offset : offsets) { // center, up, down, left, right
    int nr = row + offset[0];
    int nc = column + offset[1];
    if (nr >= 0 && nr < rows && nc >= 0 && nc < columns) {
        sum += source[nr][nc];
        count++;
    }
}
result[row][column] = sum / count;

I label this as an explanatory algorithm excerpt, not the complete archived program. Java integer division is intentional: the upper-left fixture pixel averages 0, 100, and 200 to 100, while the upper-right averages 100, 0, and 255 to 118 after truncation. My complete expected result is:

Illustrative text example
100 118
151 185

I use those hand-calculated values as an independent oracle. Comparing two implementations would be weaker if both happened to share the same mistaken formula.

THE CHALLENGE

The challenge: keeping every read tied to the original image

PROJECT STEPS

How I keep the two filters independent

Project briefing illustration for grayscale filters: a flat tactical intelligence sheet in the style of Star Fox Assault.
STEP 01 OF 05
Validate the matrix

In my 2026 restoration, I read the image grid’s row and column counts before creating storage, then require whole-number pixel values from 0 through 255. I reject missing pixels and values that are not integers so a partly read image cannot enter the calculation. These checks give both filters the same clearly defined starting point.

Select a step to follow the project.
Read all step descriptions
  1. Validate the matrix

    In my 2026 restoration, I read the image grid’s row and column counts before creating storage, then require whole-number pixel values from 0 through 255. I reject missing pixels and values that are not integers so a partly read image cannot enter the calculation. These checks give both filters the same clearly defined starting point.

  2. Retain the source

    I keep the original image grid unchanged while both filters run, so later calculations read the same starting pixels as earlier ones. If I changed the original as I worked, the answer could depend on which pixel I visited first. I also give the second filter the original image rather than the first filter’s result.

  3. Apply separate rules

    I preserve the recovered program’s two rules: average a pixel with its existing up, down, left, and right neighbors, or separately replace values below 128 with 255 minus their value. I leave diagonals out of the average and deliberately drop its fractional remainder through whole-number division. I calculate both results from the original image, leaving values of 128 or higher unchanged in the second filter.

  4. Write fresh results

    I create a separate result grid for each filter in the restoration, so writing an answer cannot change a pixel I still need to read. This keeps both calculations independent throughout the work. I also compare the input afterward to check that both operations actually left it unchanged.

  5. Check known pixels

    I compare the restored results with every hand-calculated pixel in a fixed two-by-two example, giving me an independent check of the arithmetic. I also check one-pixel images, single rows, and single columns to see whether the filters use only neighbors inside the grid, then compare the original image afterward. These checks assess the restoration’s behavior; I did not recover an image-quality benchmark.

Restored data flow: both filters read the original matrix and write to their own result arrays.

The design constraint I had to preserve was that every destination pixel reads the original matrix. If I wrote back into the input while scanning, later pixels could see neighbors I had already filtered. That would make the result depend on traversal order and change the algorithm.

My response in the restoration was to allocate a fresh result array for each filter. That also keeps the second filter independent of the first. I included checks that the input remains unchanged after filtering, making this ownership rule part of the observed contract rather than just a coding preference.

How I preserved the independent threshold rule

I preserved the second filter's separate rule: values below 128 become 255 - value, while values at or above 128 remain unchanged. I pass it the original image, not the averaged result.

For the same fixture, I therefore expect:

Illustrative text example
255 155
200 255

I check the threshold directly: 127 becomes 128, while 128 stays 128. Passing the first filter's result into the second would define another pipeline, but I wanted to reproduce the recovered program's independent outputs.

What I checked and learned

I structured the verification around the fixed 2×2 matrix and every expected output cell, then added a one-pixel image, a single row, and a single column to exercise the bounds rule. I checked 127 and 128 directly, compared saved inputs after each filter, and sent a truncated file through the console path because algorithm checks alone cannot establish parser behavior.

The October 2026 work recorded six passing unit checks, processing of the preserved sample file, and a separate truncated-input check. I did not perform a timing or image-quality benchmark.

What I took from this work is a small, inspectable contract: bounded pixels, clipped neighborhoods, deliberate integer arithmetic, and transformations that do not silently depend on one another. The result remains a console matrix exercise; it does not decode PNG or JPEG files, provide a graphical editor, or implement a general convolution framework.

PROJECT WALKTHROUGH

Recovered source repaired in October 2026; six unit checks recorded.

Validation & limitations

  • Recovered source repaired in October 2026; six unit checks recorded.

Project records

  • Project artifacts and historical records, March 2021, approximate month
  • Restoration and public documentation, October 1, 2026
END OF PROJECT FILE RETURN TO ARCHIVE