File: bixobmp.cpp | Size: 5,709 bytes | Download file | Back to directory listing | BWPOW's homepage
/***********************************************************************************
    Bixobmp - Utility for converting BMP to Bixolon format PRN
    Copyright (C) 2012 SAGE team s.r.o.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see http://www.gnu.org/licenses/.
***********************************************************************************/
#include <cstdio>
#include <allegro.h>
#include <winalleg.h>
 
#define ACTION_ENCODE 1
#define ACTION_DECODE 2
 
void main_koniec(const int e,const char *str)
{
    allegro_exit();
    if(str!=NULL){
        fprintf(stderr,"%s\n",str);
    }
    exit(e);
}
 
void main_koniec(const int e)
{
    main_koniec(e,NULL);
}
 
void main_koniec(const char *str)
{
    main_koniec(1,str);
}
 
void main_usage(const char *argv0)
{
    char text[4096];
    sprintf(text,"Usage: %s ACTION FROM_FILE TO_FILE\n",argv0);
    strcat(text,"ACTION:\n");
    strcat(text,"\tencode - convert bmp to prn\n");
    strcat(text,"\tdecode - convert prn to bmp");
    main_koniec(1,text);
}
 
int main(int argc,char *argv[])
{
    int action=-1,c,cr,cg,cb;
    int x=0,y=0,yy=0,w=0,h=0;
    char infile[MAX_PATH],outfile[MAX_PATH];
    BITMAP *bmp=NULL;
    FILE *s=NULL;
 
    if(allegro_init()!=0) main_koniec(1,"INIT failed");
 
    if(argc<4) main_usage(argv[0]);
    if(!strcasecmp(argv[1],"encode")) action=ACTION_ENCODE;
    if(!strcasecmp(argv[1],"decode")) action=ACTION_DECODE;
    if(action<0) main_usage(argv[0]);
 
    if(strlen(argv[2])>=MAX_PATH||strlen(argv[3])>=MAX_PATH) main_usage(argv[0]);
    strcpy(infile,argv[2]);
    strcpy(outfile,argv[3]);
 
    set_color_depth(24);
    if(action==ACTION_ENCODE){
        bmp=load_bitmap(infile,NULL);
        if(bmp==NULL) main_koniec(2,"Can't read input file!");
        s=fopen(outfile,"wb");
        if(s==NULL) main_koniec(3,"Can't write to output file!");
        try{
            w=bmp->w;
            if(w>255){
                w=255;
                printf("Maximum width is 255. Truncated.\n");
            }
            h=bmp->h;
            if(h&7){
                h+=7-(h&7);
            }
            fputc(0x1b,s);
            fputc(0x33,s);
            fputc(0x10,s);
            for(y=0;y<h;y+=8){
                fputc(0x1b,s);
                fputc(0x2a,s);
                fputc(0,s);
                fputc(w,s);
                fputc(0,s);
 
                for(x=0;x<w;x++){
                    uint8_t b=0;
                    for(int yy=0;yy<8;yy++){
                        c=getpixel(bmp,x,y+7-yy);
                        cr=getr(c);
                        cg=getg(c);
                        cb=getb(c);
                        if(cb<128&&cr<128&&cg<128){
                            b|=1<<yy;
                        }
                    }
                    fputc(b,s);
                }
                fputc(10,s);
            }
            fputc(0x1b,s);
            fputc(0x32,s);
        }
        catch(int e)
        {
            fclose(s);
            main_koniec(4,"Error while converting!");
        }
        fclose(s);
        printf("Converted OK\n");
    }
 
    if(action==ACTION_DECODE){
        s=fopen(infile,"rb");
        if(s==NULL) main_koniec(2,"Can't read input file!");
        uint8_t pole[256][1024];
        memset(pole,0,sizeof(pole));
        try{
            y=0;
            while(1){
                if(feof(s)) break;
                c=fgetc(s);
                if(c==EOF) break;
                if(c==10||c==13) continue;
                if(c!=0x1b) throw 2;
 
                if(feof(s)) throw 3;
                c=fgetc(s);
 
                if(c==0x33){ fgetc(s); continue; }
                if(c==0x32){ continue; }
                if(c==0x2a){
                    if(feof(s)) throw 4;
                    c=fgetc(s);
                    if(c!=0x00) throw 5;
 
                    if(feof(s)) throw 6;
                    w=fgetc(s);
 
                    if(feof(s)) throw 7;
                    c=fgetc(s);
                    if(c!=0x00) throw 8;
 
                    for(x=0;x<w;x++){
                        if(feof(s)) throw 9;
                        c=fgetc(s);
                        for(yy=0;yy<8;yy++){
                            pole[x][y+7-yy]=c&(1<<yy);
                        }
                    }
                    y+=8;
                    if(y>=1024) throw 10;
                }
            }
 
        }
        catch(int e)
        {
            fclose(s);
            main_koniec(e,"Wrong or unsupported input file!");
        }
        fclose(s);
        h=y;
        if(w<=0||h<=0) main_koniec(4,"Bad size!");
        bmp=create_bitmap(w,h);
        if(bmp==NULL) main_koniec(1,"Bad alloc");
        for(y=0;y<h;y++){
            for(x=0;x<w;x++){
                putpixel(bmp,x,y,pole[x][y]?0:makecol(255,255,255));
            }
        }
        save_bitmap(outfile,bmp,NULL);
        printf("Converted OK\n");
    }
 
    main_koniec(0);
    return 0;
}
END_OF_MAIN()