/*
code39 barcode bmp gen
2001.06.12
telmetwo@mac.com
*/
#include <stdio.h>
#include <string.h>
int m,width,v;
unsigned char l[64];
char header[64]={
0x42,0x4d, /* BM */
0xbe,0x07,0x00,0x00, /* file size */
0x00,0x00,0x00,0x00,
0x3e,0x00,0x00,0x00, /* offset */
0x28,0x00,0x00,0x00, /* info size */
0x40,0x01,0x00,0x00, /* X */
0x30,0x00,0x00,0x00, /* Y */
0x01,0x00,
0x01,0x00,
0x00,0x00,0x00,0x00,
0x80,0x07,0x00,0x00,
0xc4,0x0e,0x00,0x00,
0xc4,0x0e,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0xff,0xff,0xff,0x00
};
char fnt[13][8]={
{0x3c,0x42,0x46,0x5a,0x62,0x42,0x3c,0x00}, /* 0 */
{0x08,0x18,0x28,0x08,0x08,0x08,0x3e,0x00},
{0x3c,0x42,0x02,0x0c,0x30,0x40,0x7e,0x00},
{0x3c,0x42,0x02,0x1c,0x02,0x42,0x3c,0x00},
{0x04,0x0c,0x14,0x24,0x7e,0x04,0x04,0x00},
{0x7e,0x40,0x78,0x04,0x02,0x44,0x38,0x00},
{0x1c,0x20,0x40,0x7c,0x42,0x42,0x3c,0x00},
{0x7e,0x42,0x04,0x08,0x10,0x10,0x10,0x00},
{0x3c,0x42,0x42,0x3c,0x42,0x42,0x3c,0x00},
{0x3c,0x42,0x42,0x3e,0x02,0x04,0x38,0x00}, /* 9 */
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* space */
{0x08,0x1e,0x28,0x1c,0x0a,0x3c,0x08,0x00}, /* $ */
{0x08,0x2a,0x1c,0x3e,0x1c,0x2a,0x08,0x00} /* * */
};
char ccode[20]="0123456789_$*";
char bitmap[60][48];
bitfill(int m,int width,int v);
bitout(char *bstr);
lineout(char *pstr);
bitfill(m,width,v){
char c,i,bp;
// fprintf(stderr," *%d %d %d",m,width,v);
for(i=0;i<width;i++){
c=l[(m+i)/8];
bp=7-(m+i)%8;
if(v==1) {
c |= 1<<bp;
l[(m+i)/8]=c;
}
}
}
int bitout(char *bstr){
int i,c;
// fprintf(stderr,"%d %s\n",strlen(bstr),bstr);
if(strlen(bstr)==1) {
width=1;
bitfill(m,width,0);
m+=width;
}
else {
i=0;
while(c=*bstr++){
if(c=='1')
width=2;
else
width=1;
if(i%2==0)
bitfill(m,width,1);
else
bitfill(m,width,0);
m+=width;
i++;
}
}
return m;
}
lineout(char *pstr)
{
char *ptr;
int len;int c,i,j;char bstr[256];
char bcode[20][10]={
{"000110100"}, /* 0 */
{"100100001"}, /* 1 */
{"001100001"},
{"101100000"},
{"000110001"},
{"100110000"},
{"001110000"},
{"000100101"},
{"100100100"},
{"001100100"}, /* 9 */
{"011000100"}, /* space */
{"010101000"}, /* $ */
{"010010100"} /* * */
};
for(i=0;i<40;i++)
l[i]=0x00;
m=16;
// fprintf(stderr,"Code39\n");
bitout("g"); /* gap*/
bitout(bcode[12]);
bitout("g"); /* gap*/
ptr=pstr;
// fprintf(stderr,"[%d %s]\n",strlen(ptr),ptr);
for(i=0;i<strlen(pstr);i++){
c=*ptr++;
for(j=0;j<strlen(ccode);j++)
if(ccode[j]==c)
bitout(bcode[j]);
bitout("g"); /* gap*/
}
bitout(bcode[12]);
bitout("g"); /* gap*/
// for(i=0;i<15;i++)
// fprintf(stderr,"%02x ",l[i]&0xff);
m+=23;
}
strout(char *pstr){
char *ptr;int i,j,k;int c;
for(k=0;k<8;k++) {
ptr=pstr;
// fprintf(stderr,"[%d %s]\n",strlen(ptr),ptr);
for(i=0;i<strlen(pstr);i++){
c=*ptr++;
for(j=0;j<strlen(ccode);j++)
if(ccode[j]==c)
bitmap[k+1][i+5]=~fnt[j][7-k];
}
}
}
main(int ac,char *av[]){
int i,j;
char *pstr="";
for(i=0;i<62;i++)
putchar(header[i]);
for (j=0;j<48;j++)
for(i=0;i<40;i++) {
bitmap[j][i]=0xff;
}
if(ac>1) strncat(pstr,av[1],18);
else strcat(pstr,"836011");
lineout(pstr);
strout(pstr);
for (j=10;j<47;j++)
for(i=0;i<(m/8);i++) {
bitmap[j][i]=~l[i];
}
for(i=0;i<40;i++) {
bitmap[0][i]=0xaa;
}
for (j=0;j<48;j++) {
if(j%2==0) bitmap[j][39] &=0xfe;
}
for (j=0;j<48;j++)
for(i=0;i<40;i++) {
putchar(bitmap[j][i]&0xff);
}
}