u32 dbuf_getRGB(dbuf *f, i64 pos, unsigned int flags) { u8 buf[3]; dbuf_read(f, buf, pos, 3); if(flags&DE_GETRGBFLAG_BGR) return DE_MAKE_RGB(buf[2], buf[1], buf[0]); return DE_MAKE_RGB(buf[0], buf[1], buf[2]); }
static int do_image_pgm_ppm_binary(deark *c, lctx *d, struct page_ctx *pg, de_int64 pos1) { struct deark_bitmap *img = NULL; de_int64 rowspan; de_int64 nsamples; // For both input and output de_int64 bytes_per_sample; de_int64 i, j, k; de_int64 pos = pos1; unsigned int samp_ori[3]; de_byte samp_adj[3]; if(fmt_is_ppm(pg->fmt)) nsamples=3; else nsamples=1; if(pg->maxval<=255) bytes_per_sample=1; else bytes_per_sample=2; rowspan = pg->width * nsamples * bytes_per_sample; pg->image_data_len = rowspan * pg->height; img = de_bitmap_create(c, pg->width, pg->height, (int)nsamples); for(j=0; j<pg->height; j++) { for(i=0; i<pg->width; i++) { for(k=0; k<nsamples; k++) { if(bytes_per_sample==1) { samp_ori[k] = de_getbyte(pos++); } else { samp_ori[k] = (unsigned int)de_getbyte(pos++) << 8 ; samp_ori[k] |= (unsigned int)de_getbyte(pos++); } samp_adj[k] = de_scale_n_to_255(pg->maxval, samp_ori[k]); } if(nsamples==1) { de_bitmap_setpixel_gray(img, i, j, samp_adj[0]); } else { de_uint32 clr; clr = DE_MAKE_RGB(samp_adj[0], samp_adj[1], samp_adj[2]); de_bitmap_setpixel_rgb(img, i, j, clr); } } } de_bitmap_write_to_file(img, NULL, 0); de_bitmap_destroy(img); return 1; }
static void read_palette_ximg(deark *c, lctx *d) { i64 pal_entries_in_file; i64 pal_entries_to_read; i64 i; i64 cr1, cg1, cb1; u8 cr, cg, cb; int range_warned = 0; char tmps[64]; pal_entries_in_file = (d->header_size_in_bytes-22)/3; if(pal_entries_in_file<1) return; if(d->nplanes<=8) pal_entries_to_read = (i64)(1<<((unsigned int)d->nplanes)); else pal_entries_to_read = 0; if(pal_entries_to_read>pal_entries_in_file) pal_entries_to_read = pal_entries_in_file; if(pal_entries_to_read>256) pal_entries_to_read = 256; if(pal_entries_in_file<1) return; de_dbg(c, "palette at %d", 22); de_dbg_indent(c, 1); for(i=0; i<pal_entries_to_read; i++) { cr1 = de_getu16be(22 + 6*i); cg1 = de_getu16be(22 + 6*i + 2); cb1 = de_getu16be(22 + 6*i + 4); cr = de_scale_1000_to_255(cr1); cg = de_scale_1000_to_255(cg1); cb = de_scale_1000_to_255(cb1); d->pal[i] = DE_MAKE_RGB(cr, cg, cb); de_snprintf(tmps, sizeof(tmps), "(%4d,%4d,%4d) "DE_CHAR_RIGHTARROW" ", (int)cr1, (int)cg1, (int)cb1); de_dbg_pal_entry2(c, (int)i, d->pal[i], tmps, NULL, NULL); // TODO: Maybe some out-of-range colors have special meaning? if(!range_warned && (cr1>1000 || cg1>1000 || cb1>1000)) { de_warn(c, "Bad palette color #%d: is (%d,%d,%d), max=(1000,1000,1000).", (int)i, (int)cr1, (int)cg1, (int)cb1); range_warned=1; } } de_dbg_indent(c, -1); }
static de_uint32 getpal256(int k) { de_byte r, g, b; if(k<0 || k>255) return 0; if(k<=214) { // The first 215 palette entries follow a simple pattern. r = (de_byte)((5-k/36)*0x33); g = (5-(k%36)/6)*0x33; b = (5-k%6)*0x33; return DE_MAKE_RGB(r,g,b); } return supplpal256[k-215]; }
static u32 getpal256(int k) { int x; u8 r, g, b; if(k<0 || k>255) return 0; // The first and last 108 entries together make up the simple palette once // known as the "web safe" palette. The middle 40 entries are // supplementary grayscale and red/green/blue shades. if(k>=108 && k<148) { return supplpal[k-108]; } x = k<108 ? k : k-40; r = (x%6)*0x33; g = ((x%36)/6)*0x33; b = (u8)((x/36)*0x33); return DE_MAKE_RGB(r,g,b); }
static void wri_convert_image_pal8(deark *c, i64 fpos, i64 rowspan, de_bitmap *img) { i64 i, j; int badcolorflag = 0; // Palette is from libwps (except I might have red/blue swapped it). // I haven't confirmed that it's correct. static const u32 pal_part1[8] = { 0x000000,0x800000,0x008000,0x808000,0x000080,0x800080,0x008080,0xc0c0c0 }; static const u32 pal_part2[8] = { 0x808080,0xff0000,0x00ff00,0xffff00,0x0000ff,0xff00ff,0x00ffff,0xffffff }; for(j=0; j<img->height; j++) { for(i=0; i<img->width; i++) { unsigned int palent; u32 clr; palent = de_getbyte(fpos+j*rowspan+i); if(palent<8) { clr = pal_part1[palent]; } else if(palent>=248) { clr = pal_part2[palent-248]; } else { clr = DE_MAKE_RGB(254,palent,254); // Just an arbitrary color badcolorflag = 1; } de_bitmap_setpixel_rgb(img, i, j, clr); } } if(badcolorflag) { de_warn(c, "Image uses nonportable colors"); } }