Here is the Fortran program I wrote for generating the barcode. You can compile it with the command
Code:
f77 barcode.f -o barcode
and run it in a pipeline with some other programs for scaling up the PBM and converting it to PNG format like
Code:
echo "1 2 3 4 5 6 7 8 9 0 1 2" | ./barcode | pnmscale 4 | pnmtopng > barcode.png
Code:
c
c output PBM format image of barcode
c
program barcode
integer data_file
integer i, j, k, p, height, width
integer checksum
integer given(13), parity(0:9, 2:7), codes(3, 0:9, 7)
parameter (data_file = 10)
parameter (height = 20, width = 97)
c
c read in parity and codes from data file
c
open (data_file, FILE="barcode_data.dat", STATUS="OLD")
do i = 0, 9
read (data_file, 910) (parity(i, j), j = 2, 7)
enddo
do i = 1, 3
do j = 0, 9
read (data_file, 920) (codes(i, j, k), k = 1, 7)
enddo
enddo
close (data_file)
c
c read in given numbers and calculate checksum
c
read (*,*) (given(i), i = 1, 12)
given(13) = checksum(given)
c
c output
c
write (*, 940) "P1"
write (*, 950) width, height + 2
c top line of white
do i = 1, width
write (*, 960) 0
enddo
do k = 1, height
c left guard bars
write (*, 940) "0 1 0 1"
do i = 2, 7
p = parity(given(1), i)
write (*, 920) (codes(p, given(i), j), j = 1, 7)
enddo
c centre guard bars
write (*, 940) "0 1 0 1 0"
do i = 8, 13
write (*, 920) (codes(3, given(i), j), j = 1, 7)
enddo
c right guard bars
write (*, 940) "1 0 1 0"
enddo
c bottom line of white
do i = 1, width
write (*, 960) 0
enddo
c
c formats
c
910 format (6I2)
920 format (7I2)
940 format (A)
950 format (2I3)
960 format (I2)
stop
end
c
c calculate and return checksum from given digits
c
integer function checksum(given)
integer i, total, given(12)
total = 0
do i = 1, 12
total = total + given(i)
if (0 == MOD(i, 2)) then
total = total + (2 * given(i))
endif
enddo
checksum = MOD(10 - MOD(total, 10), 10)
return
end
The data file barcode_data.dat is:
Code:
1 1 1 1 1 1
1 1 2 1 2 2
1 1 2 2 1 2
1 1 2 2 2 1
1 2 1 1 2 2
1 2 2 1 1 2
1 2 2 2 1 1
1 2 1 2 1 2
1 2 1 2 2 1
1 2 2 1 2 1
0 0 0 1 1 0 1
0 0 1 1 0 0 1
0 0 1 0 0 1 1
0 1 1 1 1 0 1
0 1 0 0 0 1 1
0 1 1 0 0 0 1
0 1 0 1 1 1 1
0 1 1 1 0 1 1
0 1 1 0 1 1 1
0 0 0 1 0 1 1
0 1 0 0 1 1 1
0 1 1 0 0 1 1
0 0 1 1 0 1 1
0 1 0 0 0 0 1
0 0 1 1 1 0 1
0 1 1 1 0 0 1
0 0 0 0 1 0 1
0 0 1 0 0 0 1
0 0 0 1 0 0 1
0 0 1 0 1 1 1
1 1 1 0 0 1 0
1 1 0 0 1 1 0
1 1 0 1 1 0 0
1 0 0 0 0 1 0
1 0 1 1 1 0 0
1 0 0 1 1 1 0
1 0 1 0 0 0 0
1 0 0 0 1 0 0
1 0 0 1 0 0 0
1 1 1 0 1 0 0