Author: John McFarlane
(john at mcfarlane.name
)
- The input image is broken into 2x2 pixel squares.
- Each color component of each square is averaged, rounded up or down and written as the output pixel.
- Any remaining odd rows or columns are repated to make up the pair.
- Visual Studio Express 2012
- Wikipedia TGA Article
- Creating TGA Image files
- C++ Reference
- IrfanView
- PearlMountain Image Converter
The program can be broken down roughly as follows:
- Successfully open input and output files
- Read and parse header from input file
- Generate and write header to output file
- Allocate enough memory to store two input rows and one output row
- For each pair of input rows:
- For each row in the pair:
- Read the row from file
- If there are an odd number of columns:
- Push a copy of the last pixel to the end
- For each pair of columns in the input rows:
- For each color component [RGB|I][A]:
- Sum the value from the four input pixels
- Add
2
to the value - Shift right by
2
bits - Store in output row
- For each color component [RGB|I][A]:
- For each row in the pair:
- If there is an odd remainder row:
- Perform the same steps as for pairs of rows (above) but use the single input row to represent two rows
TGA specification has some known prblems. As mentioned on its Wikipedia page.
Additionally, the distinction between pixel attributes and alpha channel was not entirely clear so I ignore the attribute value in the image descriptor byte. In particular, I found examples of TGAs with 32 bits of color and either 0 or 8 attribute bits.
Testing was a chore as no sample files were provided, Windows has poor support for TGA and there are relatively few instances of TGA files remaining in the wild. As a result, I had to run the gauntlet of malware downloads to find tools to handle this format.
The problem as stated would have been prohibitively hard to implement and could easily have lead to a solution with an extremely high maximum memory requirement. To get around this, two exceptions are made to the specification as presented in HalfSize.txt:
- The Standard C++ Library is assumed to be an exception to the 'no library' stipulation.
- Reading, resizing and writing of image data is not performed as three separate steps.