Example #1
0
static bool nsbmp_convert(struct content *c)
{
	nsbmp_content *bmp = (nsbmp_content *) c;
	bmp_result res;
	union content_msg_data msg_data;
	uint32_t swidth;
	const char *data;
	unsigned long size;
	char *title;

	/* set the bmp data */
	data = content__get_source_data(c, &size);

	/* analyse the BMP */
	res = bmp_analyse(bmp->bmp, size, (unsigned char *) data);
	switch (res) {
		case BMP_OK:
			break;
		case BMP_INSUFFICIENT_MEMORY:
			msg_data.error = messages_get("NoMemory");
			content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
			return false;
		case BMP_INSUFFICIENT_DATA:
		case BMP_DATA_ERROR:
			msg_data.error = messages_get("BadBMP");
			content_broadcast(c, CONTENT_MSG_ERROR, msg_data);
			return false;
	}

	/* Store our content width and description */
	c->width = bmp->bmp->width;
	c->height = bmp->bmp->height;
	swidth = bmp->bmp->bitmap_callbacks.bitmap_get_bpp(bmp->bmp->bitmap) * 
			bmp->bmp->width;
	c->size += (swidth * bmp->bmp->height) + 16 + 44;

	/* set title text */
	title = messages_get_buff("BMPTitle",
			nsurl_access_leaf(llcache_handle_get_url(c->llcache)),
			c->width, c->height);
	if (title != NULL) {
		content__set_title(c, title);
		free(title);
	}

	/* exit as a success */
	bmp->bitmap = bmp->bmp->bitmap;
	guit->bitmap->modified(bmp->bitmap);

	content_set_ready(c);
	content_set_done(c);

	/* Done: update status bar */
	content_set_status(c, "");
	return true;
}
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