9
2
Yes, the good old GIF. Loved for its versatility, hated for its patents and partly obsoleted due to its limitations (and patents), GIF consists, at the core, of a color palette and a palette-indexed image compressed using the LZW algorithm.
Your task is to write a program that reads an image in ASCII PPM format ("P3" magic number) from the standard input, and writes the same image (identical pixel-by-pixel) in GIF format to the standard output. The output can either be in binary form, or ASCII text with each byte represented by a number between 0 and 255 (inclusive), separated by whitespace.
The input image is guaranteed not to have more than 256 different colors.
Scoring:
Your program will be tested on 3 sample images, and your score will be calculated as:
program size + sum(output size - reference size for each sample image)
Lowest score wins.
Requirements:
- Your program must work with any similar kinds of images of various sizes, and not be limited to the sample images. You could, for example, restrict the dimensions to be multiples of 2 or assume that the ppm max color is 255, but it should still work with a wide variety of input images.
- The output must be a valid GIF file that can be loaded with any compliant program (after converting back to binary if using the ASCII output option).
- You can not use any image processing functions (built-in or third-party), your program must contain all the relevant code.
- Your program must be runnable in Linux using freely available software.
- The source code must use only ASCII characters.
Sample images:
Here are the 3 sample images that will be used for scoring. You can download a zip archive with the ppm files (use the download button on the top of that page). Or you can convert them from the png images below, using ImageMagick with the following command:
convert file.png -compress none file.ppm
I am also providing the MD5 checksums of the ppm files for confirmation.
1. amber
Reference size: 38055
MD5 checksum of ppm: d1ad863cb556869332074717eb278080
2. blueeyes
Reference size: 28638
MD5 checksum of ppm: e9ad410057a5f6c25a22a534259dcf3a
3. peppers
Reference size: 53586
MD5 checksum of ppm: 74112dbdbb8b7de5216f9e24c2e1a627
1
Moderator note: Off-topic/obsolete comments removed. Please see meta for discussion on the sample images in this question.
– Doorknob – 2015-03-15T23:03:58.583It seems the second image was treated with something like this: http://www.websiteoptimization.com/speed/tweak/lossy/ that would explain better compression ratio it has and sensitivity to the LZW encoder tweaks.
– nutki – 2015-03-17T18:34:21.9001“The source code must use only ASCII characters.”—so, in other words, we are not allowed to do this challenge in APL? – FUZxxl – 2015-03-18T14:56:59.553
@FUZxxl true, but you can use J. You are also not allowed to use Aheui or to do base conversion tricks in GolfScript/CJam. – aditsu quit because SE is EVIL – 2015-03-18T20:59:06.583