void set_bgimage(void) { bmphandle_t bh; if (chat_count > 0 || no_count > 10) return; bh = bmp_open(bg_image); if (bh == NULL) { perror("bmp_open"); clear_screen(); return; } if (bmp_width(bh) > 320 || bmp_height(bh) > 240) { clear_screen(); fprintf(stderr, "bmp size is too large: %d x %d\n", bmp_width(bh), bmp_height(bh)); bmp_close(bh); return; } buf_bmp(bh, 0, 0); bmp_close(bh); show_vfb(vfb_list[0]); }
// "Tone-map" and write an image float write_bmp(char* output_name, int width, int height, float* r_done, float* g_done, float* b_done, float exposure) { bmp_init(output_name, width, height ); for(int i = 0; i < width * height; i++) { float r = pow(fmax(0.0f, fmin(1.0f, r_done[i] / exposure)), 1.0f/2.2f) * 255.0f; float g = pow(fmax(0.0f, fmin(1.0f, g_done[i] / exposure)), 1.0f/2.2f) * 255.0f; float b = pow(fmax(0.0f, fmin(1.0f, b_done[i] / exposure)), 1.0f/2.2f) * 255.0f; bmp_pixel((int)r, (int)g, (int)b); } bmp_close(); }
static int file_to_fb(const char * srcpath) { int ret = -1; BMP_READ * bmp = NULL; struct FB * fb = NULL; int sw, sh; int srcbpp, dstbpp; void * pdata = NULL, * bmpdata = NULL; RGB_CONVERT_FUN convert_func = NULL; do { bmp = bmp_open(srcpath); if (!bmp) { break; } fb = fb_create(0); if (!fb) { break; } sw = bmp_width(bmp); sh = bmp_height(bmp); bmpdata = bmp_data(bmp); srcbpp = bmp_bpp(bmp); dstbpp = fb_bpp(fb); convert_func = get_convert_func(srcbpp, dstbpp); if (convert_func) { pdata = convert_func(bmpdata, sw, sh); bmpdata = pdata; } if (!bmp_forward(bmp)) { line_reversal(bmpdata, sw, sh, dstbpp); } rgb_copy(bmpdata, fb_bits(fb), sw, sh, fb_width(fb), fb_height(fb), dstbpp); ret = 0; } while (0); fb_destory(fb); bmp_close(bmp); if (pdata) { free(pdata); } return ret; }
static int file_to_file(const char * srcpath, const char * dstpath, int output_rgb) { int ret = -1; BMP_READ * bmp = NULL; int w, h; int srcbpp, dstbpp; void * pdata = NULL, * bmpdata = NULL; RGB_CONVERT_FUN convert_func = NULL; do { bmp = bmp_open(srcpath); if (!bmp) { break; } w = bmp_width(bmp); h = bmp_height(bmp); bmpdata = bmp_data(bmp); srcbpp = bmp_bpp(bmp); dstbpp = g_rgbbpp[output_rgb]; convert_func = get_convert_func(srcbpp, dstbpp); if (convert_func) { pdata = convert_func(bmpdata, w, h); bmpdata = pdata; } if (!bmp_forward(bmp)) { line_reversal(bmpdata, w, h, dstbpp); } ret = save_bmp(dstpath, w, h, bmpdata, dstbpp); } while (0); bmp_close(bmp); if (pdata) { free(pdata); } return ret; }
void draw_icon(struct icon *icon) { int i, j; bmphandle_t bh; if (icon->mode) { bh = bmp_open(icon->name); if (bh == NULL) { perror("bmp_open"); return; } for (i=icon->y; i<icon->y+bmp_height(bh); i++) for (j=icon->x; j<icon->x+bmp_width(bh); j++) *(myfb->fb + i*myfb->fbvar.xres +j) = makepixel(bmp_getpixel(bh, j-icon->x, i-icon->y)); bmp_close(bh); } else { drow_rect(icon->x, icon->y, icon->x + icon->w, icon->y + icon->h, icon->color); put_string_center(icon->x + icon->w/2, icon->y + icon->h/2, icon->name, icon->color); } }
int main(int argc, char *argv[]) { int i, j, g, ret; bmp_rgb *c; bmp_t *b1 = bmp_init(); assert(b1 != NULL); int width = 997, height = 998; ret = bmp_create(b1, argv[1], width, height, BMP_BIT4); if (ret) { printf("%d:%s\n", ret, bmp_error(ret)); return ret; } *bmp_get_pale_rgb(b1, 0) = bmp_make_rgb(255, 0, 0); *bmp_get_pale_rgb(b1, 1) = bmp_make_rgb(0, 255, 0); *bmp_get_pale_rgb(b1, 2) = bmp_make_rgb(0, 0, 255); int color = 0; for (i = 0; i < b1->info.height; i++) { for (j = 0; j < b1->info.width; j++) { bmp_set_bit4(b1, i, j, color); color = (color + 1) % 3; } } ret = bmp_write(b1); if (ret) { printf("%d:%s\n", ret, bmp_error(ret)); return ret; } bmp_close(b1); printf("OK\n"); return 0; }
//for libgd2 < 2.99.999 gdImagePtr gdImageCreateFromBmpPtr(int size, void *data) { gdImagePtr ret; bmphandle_t bh; int h, w, i, j; struct bgrpixel r; int color; bh = bmp_open(data, size); if (bh == NULL) return NULL; h = bmp_height(bh); w = bmp_width(bh); ret = gdImageCreateTrueColor(w, h); for (i = 0; i < w; i++) { for (j = 0; j < h; j++) { r = bmp_getpixel(bh, i, j); color = gdImageColorResolve(ret, r.r, r.g, r.b); gdImageSetPixel(ret, i, j, color); } } bmp_close(bh); return ret; }
bmphandle_t bmp_open(char *buf, int size) { bmphandle_t bh; bh = (bmphandle_t) malloc(sizeof (*bh)); memset(bh, 0, sizeof (*bh)); /* I don't like calloc */ if (bmp_readheader(buf, size, bh)) goto error; if (bh->bpp < 24 && bh->npalette > 256) goto error; if (bh->npalette != 0) { bh->palette = (struct palette_s *) malloc(sizeof (struct palette_s) * bh->npalette); memset(bh->palette, 0, sizeof (struct palette_s) * bh->npalette); if (bmp_readpalette(buf, size, bh)) goto error; } if (bmp_readdata(buf, size, bh)) goto error; switch (bh->bpp) { case 1: case 4: goto error; case 8: bh->getpixel = getpixel_8bpp; break; case 16: bh->getpixel = getpixel_16bpp; if (bh->compression == BI_RGB) { unsigned *mask; if (bh->palette != NULL) /* something wrong */ goto error; mask = (unsigned *) malloc(sizeof (unsigned) * 3); mask[2] = 0x001F; /* blue mask */ mask[1] = 0x03E0; /* green mask */ mask[0] = 0x7C00; /* red mask */ bh->palette = (struct palette_s *) mask; bh->boffset_blue = 0; bh->boffset_green = 5; bh->boffset_red = 10; bh->bsize_blue = 5; bh->bsize_green = 5; bh->bsize_red = 5; } else { /* BI_BITFIELD */ if (bh->palette == NULL) /* something wrong */ goto error; calculate_boffset(bh, 16); } break; case 24: bh->getpixel = getpixel_24bpp; break; case 32: bh->getpixel = getpixel_32bpp; if (bh->compression == BI_RGB) { unsigned *mask; if (bh->palette != NULL) /* something wrong */ goto error; mask = (unsigned *) malloc(sizeof (unsigned) * 3); mask[2] = 0x000000FF; /* blue mask */ mask[1] = 0x0000FF00; /* green mask */ mask[0] = 0x00FF0000; /* red mask */ bh->palette = (struct palette_s *) mask; bh->boffset_blue = 0; bh->boffset_green = 8; bh->boffset_red = 16; bh->bsize_blue = 8; bh->bsize_green = 8; bh->bsize_red = 8; } else { /* BI_BITFILED */ if (bh->palette == NULL) /* something wrong */ goto error; calculate_boffset(bh, 32); } break; default: goto error; } return bh; error: bmp_close(bh); return NULL; }