Image Compression
Time limit1sMemory limit128 MB
Compress a binary square bitmap with a quadtree and a majority threshold, then output the image the encoding reconstructs.
- Level
Medium4 of 10
- Topics
- Divide and conquer, Recursion, Matrix, Simulation
- Solved
- No attempts yet
Problem
Many two-dimensional image compression strategies rely on locating large regions of high similarity. This problem explores one such approach based on a hierarchical (quadtree) decomposition of a bitmap image. Every pixel is one of two colors, written as 0 (white) and 1 (black).
The image is encoded as a tree whose root represents the entire square region. If a region is monochromatic (every pixel the same color), the node for that region is a leaf that stores the region's color. Otherwise the region is split into four equal quadrants about its center, and the same procedure is applied recursively to each quadrant.
This lossless scheme becomes a lossy one with a single change. Instead of decomposing a region until it is perfectly monochromatic, fix an integer threshold (a percentage). A region becomes a leaf as soon as at least of its pixels share one color, and that majority color is stored in the leaf; only when neither color reaches is the region split into quadrants and processed recursively. A single-pixel region is always 100% one color, so the process always terminates.
To decompress (reconstruct) the image from such an encoding, every leaf region is filled entirely with its stored color.
Given an original bitmap and a threshold, output the image that results from compressing it with this lossy scheme and then decompressing it.
Because is at least 51, at most one color can ever reach the threshold, so the leaf's color is never ambiguous.
Input
The input consists of a series of data sets, followed by a line containing only a single 0.
Each data set begins with a line containing two integers and : is the width of the bitmap and is the threshold percentage. Every image is square, and is a power of two with . The threshold satisfies .
The line with and is followed by lines, each a string of exactly characters, each character either 0 or 1, giving one row of the bitmap from top to bottom.
Output
For each data set, first print a line of the form Image k:, numbering the data sets starting from 1. Then print lines, each a string of 0 and 1 characters giving one row of the reconstructed bitmap, from top to bottom.