Just about every store nowadays uses Universal Product Code (UPC) barcodes to simplify the checking out process. If the name doesn't mean anything to you, you will surely recognize what they look like:
The most common system is UPC-A, which uses 12 digits to represent each specific product. Each digit is encoded into a series of black and white stripes to allow machines to read the code, a length of seven bits. There are a total of 11 bits worth of patterns that indicate the beginning, middle, and end of the barcode. This comes to a total barcode length of 12 × 7 + 11 = 95 bits. (From now on, when binary is used to refer to the color of each bit,
0 is white and
1 is black.)
The beginning and end both have a pattern of
101. The digits are then divided into 2 groups of 6 and encoded as shown below, with a pattern
01010 between the left and right groups. This table lists the pattern for each number. Note that the pattern is different depending on if the digit is on the right or left side (This allows the barcode to be scanned upside down). However, the pattern for the right is the opposite (swap black for white and vice versa) of that of the left.
If you can't see the image above, this is each number's binary equivalent.
# Left Right 0 0001101 1110010 1 0011001 1100110 2 0010011 1101100 3 0111101 1000010 4 0100011 1011100 5 0110001 1001110 6 0101111 1010000 7 0111011 1000100 8 0110111 1001000 9 0001011 1110100
Say you have the UPC
022000 125033. (Those aren't random numbers. Leave a comment if you figure out their significance.) You start with this boilerplate that is the same in every barcode:
For the digits, you replace each one with the corresponding encoding for the side (left or right) it's on. If you're still confused, see the image below.
Here is the output in binary with
| pipes separating the parts.
Write a program that outputs the UPC-A barcode for the user input. The image's dimensions should be 95 × 30 pixels, with each "bit" being one pixel wide and 30 pixels tall. Black stripes are in
rgb(0, 0, 0) and white stripes are consistently transparent or
rgb(255, 255, 255).
- Take input from stdin or the command line, or write a function that takes a string or integer (note that input can have leading zeroes, and most languages remove them or convert the number to octal).
- Output the image in one of the following ways:
- Save it to a file with a name and format (PNG, PBM, etc.) of your choice.
- Display it on the screen.
- Output its file data to stdout.
- You may not use libraries or builtins that generate barcodes (I'm looking at you, Mathematica), although you may use image or graphics libraries.
- The last digit of a UPC is usually a check digit, but for these purposes you don't have to worry about it.
Here are some more examples to test your code with. The binary output is also given for convenience.
This is code golf, so the shortest submission (in bytes wins). Tiebreaker goes to the earliest post.