static void nsbmp_destroy(struct content *c) { nsbmp_content *bmp = (nsbmp_content *) c; bmp_finalise(bmp->bmp); free(bmp->bmp); }
int main(const int argc, const char **argv){ initstdio(); if(argc<2){ fprintf(stderr, "codeiq124 in.bmp >out.txt\n" "codeiq124 io.bmp <in.txt\n" ); return -1; } int pixels=0,count=0,color,last; if(isatty(fileno(stdin))){ //decode struct stat st; int x,y; FILE *in=fopen(argv[1],"rb"); if(!in){ fprintf(stderr,"cannot open %s\n",argv[2]); return 1; } fstat(fileno(in),&st); void *imgbuf=malloc(st.st_size); fread(imgbuf,1,st.st_size,in); fclose(in); bmp_image gif; bmp_bitmap_callback_vt vt={ bitmap_create, bitmap_destroy, bitmap_set_suspendable, bitmap_get_buffer, bitmap_get_bpp }; bmp_create(&gif,&vt); if(bmp_analyse(&gif, st.st_size, imgbuf)||bmp_decode(&gif)){fprintf(stderr,"decode error\n");bmp_finalise(&gif);free(imgbuf);return 1;} free(imgbuf); for(y=0;y<gif.height;y++){ for(x=0;x<gif.width;x++){ u32 coor=y*gif.width+x; u8 b=((((u32*)gif.bitmap)[coor]&0xff0000)>>16)&0xff; u8 g=((((u32*)gif.bitmap)[coor]&0x00ff00)>>8)&0xff; u8 r=((((u32*)gif.bitmap)[coor]&0x0000ff)>>0)&0xff; u8 mes=((r&7)<<5)|((g&3)<<3)|((b&7)<<0); if(!mes)break; putchar(mes); } } bmp_finalise(&gif); }else{ //encode