static void do_read_bitmap(deark *c, lctx *d, de_int64 pos) { de_int64 ver_num; dbuf *unc_pixels = NULL; ver_num = de_getui32be(pos); de_dbg(c, "version number: %u\n", (unsigned int)ver_num); if(ver_num!=0 && ver_num!=2 && ver_num!=3) { de_warn(c, "Unrecognized version number: %u\n", (unsigned int)ver_num); } pos += 512; unc_pixels = dbuf_create_membuf(c, MACPAINT_IMAGE_BYTES, 1); de_fmtutil_uncompress_packbits(c->infile, pos, c->infile->len - pos, unc_pixels, NULL); if(unc_pixels->len < MACPAINT_IMAGE_BYTES) { de_warn(c, "Image decompressed to %d bytes, expected %d.\n", (int)unc_pixels->len, (int)MACPAINT_IMAGE_BYTES); } de_convert_and_write_image_bilevel(unc_pixels, 0, MACPAINT_WIDTH, MACPAINT_HEIGHT, MACPAINT_WIDTH/8, DE_CVTF_WHITEISZERO, NULL, 0); dbuf_close(unc_pixels); }
static int do_gem_img(deark *c, lctx *d) { dbuf *unc_pixels = NULL; de_bitmap *img = NULL; de_finfo *fi = NULL; int is_color = 0; i64 k; if(d->header_size_in_words==9 && (d->nplanes==3 || d->nplanes==4)) { i64 x; x = de_getu16be(8*2); if(x==0) { is_color = 1; } } de_dbg(c, "image at %d", (int)d->header_size_in_bytes); unc_pixels = dbuf_create_membuf(c, d->rowspan_total*d->h, 0); uncompress_pixels(c, d, unc_pixels, d->header_size_in_bytes, c->infile->len-d->header_size_in_bytes); img = de_bitmap_create(c, d->w, d->h, is_color?3:1); fi = de_finfo_create(c); set_density(c, d, fi); if(d->nplanes==1) { de_convert_image_bilevel(unc_pixels, 0, d->rowspan_per_plane, img, DE_CVTF_WHITEISZERO); } else if(is_color && d->nplanes==3) { for(k=0; k<8; k++) { d->pal[k] = pal3bit[k]; } read_paletted_image(c, d, unc_pixels, img); } else if(is_color && d->nplanes==4) { for(k=0; k<16; k++) { d->pal[k] = pal4bit[k]; } read_paletted_image(c, d, unc_pixels, img); } else { de_make_grayscale_palette(d->pal, ((i64)1)<<((unsigned int)d->nplanes), 1); read_paletted_image(c, d, unc_pixels, img); } de_bitmap_write_to_file_finfo(img, fi, 0); de_bitmap_destroy(img); de_finfo_destroy(c, fi); dbuf_close(unc_pixels); return 1; }
static void do_vorbis_page(deark *c, lctx *d, struct page_info *pgi, struct stream_info *si) { u8 firstbyte; if(pgi->is_first_page) { // The first Ogg page of a bitstream usually contains enough data to be // useful. So, we'll try to process it directly, without reconstructing // the codec bitstream. do_vorbis_id_header(c, d, pgi, si); } // We want to save a copy of the Comment and Setup header data, // but not the Identification header which is handled elsewhere. if(si->stream_state!=0) { // We've already handled the Comment & Setup headers. goto done; } if(pgi->dlen<1) goto done; firstbyte = de_getbyte(pgi->dpos); if(si->page_count==0 || pgi->page_seq_num==0 || (pgi->hdr_type&0x02)) { // This appears to be the Identification header page. Skip it. goto done; } if(pgi->page_seq_num>=1 && (firstbyte&0x01)) { // This appears to be one of the pages we care about. // Save its data for later. if(!si->header_stream) { si->header_stream = dbuf_create_membuf(c, 1048576, 1); } dbuf_copy(c->infile, pgi->dpos, pgi->dlen, si->header_stream); } else if((firstbyte&0x01)==0) { // Reached the end of headers (by encountering a non-header page). // (There is required to be an Ogg page break immediately after the // Vorbis headers, so the start of the first data *page* must correspond // to the start of the first data *packet*. A Vorbis data packet always // begins with a byte whose low bit is 0.) do_theora_vorbis_after_headers(c, d, si); } done: ; }
static void do_decode_24bit(deark *c, lctx *d, struct page_ctx *pg) { dbuf *unc_pixels = NULL; struct deark_bitmap *img = NULL; de_int64 i, j; de_byte cr, cg, cb, ca; de_int64 w, h; de_int64 skip; w = pg->type_info->width; h = pg->type_info->height; // TODO: Try to support uncompressed 24-bit images, assuming they exist. // Apparently, some 'it32' icons begin with four extra 0x00 bytes. // Skip over the first four bytes if they are 0x00. // (I don't know the reason for these bytes, but this is the same // logic libicns uses.) skip = 0; if(pg->code4cc.id==0x69743332) { // 'it32' (128x128) if(!dbuf_memcmp(c->infile, pg->image_pos, "\0\0\0\0", 4)) { skip = 4; } } unc_pixels = dbuf_create_membuf(c, w*h*3, 1); do_uncompress_24(c, d, pg, unc_pixels, skip); img = de_bitmap_create(c, w, h, 4); for(j=0; j<pg->type_info->height; j++) { for(i=0; i<pg->type_info->width; i++) { cr = dbuf_getbyte(unc_pixels, j*w + i); cg = dbuf_getbyte(unc_pixels, (h+j)*w + i); cb = dbuf_getbyte(unc_pixels, (2*h+j)*w + i); if(pg->mask_pos) ca = de_getbyte(pg->mask_pos + j*w + i); else ca = 0xff; de_bitmap_setpixel_rgba(img, i, j, DE_MAKE_RGBA(cr,cg,cb,ca)); } } de_bitmap_write_to_file(img, pg->filename_token, 0); de_bitmap_destroy(img); if(unc_pixels) dbuf_close(unc_pixels); }
static void uncompress_pixels(deark *c, lctx *d, dbuf *unc_pixels, i64 pos1, i64 len) { i64 bytes_consumed; i64 pos; i64 ypos; i64 repeat_count; i64 k; i64 plane; dbuf *unc_line = NULL; d->pattern_buf = de_malloc(c, d->patlen); unc_line = dbuf_create_membuf(c, d->rowspan_total, 0); pos = pos1; ypos = 0; while(1) { if(ypos >= d->h) break; repeat_count = 1; dbuf_empty(unc_line); for(plane=0; plane<d->nplanes; plane++) { uncompress_line(c, d, unc_line, pos, ypos, &bytes_consumed, &repeat_count); pos+=bytes_consumed; if(bytes_consumed<1) goto done1; } for(k=0; k<repeat_count; k++) { if(ypos >= d->h) break; dbuf_copy(unc_line, 0, d->rowspan_total, unc_pixels); ypos++; } } done1: dbuf_close(unc_line); de_free(c, d->pattern_buf); d->pattern_buf = NULL; }
// Read and process the keyword, language, translated keyword, or main text // field of a tEXt/zTXt/iTXt chunk. // 'bytes_consumed' does not include the NUL separator/terminator. // This is a wrapper that first decompresses the field if necessary. static int do_text_field(deark *c, lctx *d, struct text_chunk_ctx *tcc, int which_field, i64 pos, i64 bytes_avail, int is_nul_terminated, int is_compressed, int encoding, i64 *bytes_consumed) { dbuf *tmpdbuf = NULL; int retval = 0; i64 bytes_consumed2; if(!is_compressed) { retval = do_unc_text_field(c, d, tcc, which_field, c->infile, pos, bytes_avail, is_nul_terminated, encoding, bytes_consumed); goto done; } // Decompress to a membuf, then call do_unc_text_field() with that membuf. // Note that a compressed field cannot be NUL-terminated. *bytes_consumed = bytes_avail; tmpdbuf = dbuf_create_membuf(c, 0, 0); if(!de_decompress_deflate(c->infile, pos, bytes_avail, tmpdbuf, 0, NULL, d->is_CgBI ? 0 : DE_DEFLATEFLAG_ISZLIB)) { goto done; } retval = do_unc_text_field(c, d, tcc, which_field, tmpdbuf, 0, tmpdbuf->len, 0, encoding, &bytes_consumed2); done: dbuf_close(tmpdbuf); return retval; }
static void de_run_tga(deark *c, de_module_params *mparams) { lctx *d = NULL; i64 pos; dbuf *unc_pixels = NULL; int saved_indent_level; i64 rowspan_tmp; de_dbg_indent_save(c, &saved_indent_level); d = de_malloc(c, sizeof(lctx)); detect_file_format(c, d); if(d->file_format==FMT_VST) { de_declare_fmt(c, "TrueVista"); } else { de_declare_fmt(c, "TGA"); } pos = 0; if(d->file_format==FMT_VST) { if(!do_read_vst_headers(c, d)) goto done; } else { if(!do_read_tga_headers(c, d)) goto done; } pos += 18; if(d->id_field_len>0) { de_dbg(c, "image ID at %d (len=%d)", (int)pos, (int)d->id_field_len); pos += d->id_field_len; } if(d->color_map_type!=0) { d->bytes_per_pal_entry = (d->cmap_depth+7)/8; d->pal_size_in_bytes = d->cmap_length * d->bytes_per_pal_entry; de_dbg(c, "color map at %d (%d colors, %d bytes)", (int)pos, (int)d->cmap_length, (int)d->pal_size_in_bytes); de_dbg_indent(c, 1); if(!do_read_palette(c, d, pos)) goto done; de_dbg_indent(c, -1); pos += d->pal_size_in_bytes; } de_dbg(c, "bitmap at %d", (int)pos); de_dbg_indent(c, 1); d->bytes_per_pixel = ((d->pixel_depth+7)/8); if(d->pixel_depth==1) { rowspan_tmp = (d->main_image.width+7)/8; } else { rowspan_tmp = d->main_image.width * d->bytes_per_pixel; } d->main_image.img_size_in_bytes = d->main_image.height * rowspan_tmp; if(d->color_type!=TGA_CLRTYPE_PALETTE && d->color_type!=TGA_CLRTYPE_TRUECOLOR && d->color_type!=TGA_CLRTYPE_GRAYSCALE) { de_err(c, "Unsupported color type (%d: %s)", (int)d->color_type, d->clrtype_name); goto done; } if( (d->color_type==TGA_CLRTYPE_PALETTE && d->pixel_depth==8) || (d->color_type==TGA_CLRTYPE_TRUECOLOR && d->pixel_depth==15) || (d->color_type==TGA_CLRTYPE_TRUECOLOR && d->pixel_depth==16) || (d->color_type==TGA_CLRTYPE_TRUECOLOR && d->pixel_depth==24) || (d->color_type==TGA_CLRTYPE_TRUECOLOR && d->pixel_depth==32) || (d->color_type==TGA_CLRTYPE_GRAYSCALE && d->pixel_depth==1) || (d->color_type==TGA_CLRTYPE_GRAYSCALE && d->pixel_depth==8) ) { ; } else { de_err(c, "Unsupported TGA image type (%s, depth=%d)", d->clrtype_name, (int)d->pixel_depth); goto done; } if(d->cmpr_type==TGA_CMPR_RLE) { if(d->pixel_depth<8) { de_err(c, "RLE compression not supported when depth (%d) is less than 8", (int)d->pixel_depth); goto done; } unc_pixels = dbuf_create_membuf(c, d->main_image.img_size_in_bytes, 1); if(!do_decode_rle(c, d, pos, unc_pixels)) goto done; } else if(d->cmpr_type==TGA_CMPR_NONE) { unc_pixels = dbuf_open_input_subfile(c->infile, pos, d->main_image.img_size_in_bytes); } else { de_err(c, "Unsupported compression type (%d, %s)", (int)d->cmpr_type, d->cmpr_name); goto done; } // Maybe scan the image, to help detect transparency. do_prescan_image(c, d, unc_pixels); if(d->pixel_depth==32) { de_dbg(c, "using alpha channel: %s", d->has_alpha_channel?"yes":"no"); } // TODO: 16-bit images could theoretically have a transparency bit, but I don't // know how to detect that. do_decode_image(c, d, &d->main_image, unc_pixels, NULL, 0); de_dbg_indent(c, -1); if(d->thumbnail_offset!=0) { do_decode_thumbnail(c, d); } done: dbuf_close(unc_pixels); de_dbg_indent_restore(c, saved_indent_level); de_free(c, d); }
static void de_run_alphabmp(deark *c, de_module_params *mparams) { unsigned int flags; lctx *d = NULL; i64 pos; i64 palsize; dbuf *unc_pixels = NULL; int saved_indent_level; de_dbg_indent_save(c, &saved_indent_level); d = de_malloc(c, sizeof(lctx)); de_declare_fmt(c, "Alpha Microsystems BMP"); pos = 10; de_dbg(c, "bitmap image definition block at %d", (int)pos); de_dbg_indent(c, 1); d->w = de_getu16le(pos); d->h = de_getu16le(pos+2); de_dbg_dimensions(c, d->w, d->h); if(!de_good_image_dimensions(c, d->w, d->h)) goto done; d->bpp = de_getu16le(pos+4); de_dbg(c, "bits/pixel: %d", (int)d->bpp); flags = (unsigned int)de_getu16le(pos+6); d->has_palette = flags & 0x01; d->palette_is_hls = (flags>>1) & 0x01; de_dbg(c, "has-palette: %d", (int)d->has_palette); if(d->has_palette) de_dbg(c, "palette-is-HLS: %d", (int)d->palette_is_hls); d->compression = de_getu16le(pos+8); de_dbg(c, "compression: %d", (int)d->compression); de_dbg_indent(c, -1); pos += 70; if(d->has_palette) { if(d->palette_is_hls && d->bpp<=8) { de_err(c, "HLS palettes are not supported"); goto done; } if(!do_read_palette(c, d, pos, &palsize)) goto done; pos += palsize; } else if(d->bpp<=8) { de_err(c, "Paletted images without an embedded palette are not supported"); goto done; } de_dbg(c, "bitmap at %d", (int)pos); de_dbg_indent(c, 1); if(d->compression) { unc_pixels = dbuf_create_membuf(c, 32768, 0); if(!do_uncompress_image(c, d, pos, unc_pixels)) goto done; } else { unc_pixels = dbuf_open_input_subfile(c->infile, pos, c->infile->len - pos); } if(d->bpp!=1 && d->bpp!=4 && d->bpp!=8 && d->bpp!=24) { de_err(c, "%d bits/pixel is not supported", (int)d->bpp); goto done; } do_bitmap(c, d, unc_pixels); de_dbg_indent(c, -1); done: de_dbg_indent_restore(c, saved_indent_level); dbuf_close(unc_pixels); de_free(c, d); }
// Generic (ImageMagick?) profile. Hex-encoded, with three header lines. static void on_im_generic_profile_main(deark *c, lctx *d, struct text_chunk_ctx *tcc, dbuf *inf, i64 pos1, i64 len) { int k; i64 pos = pos1; i64 dlen; int dump_to_file = 0; int decode_to_membuf = 0; const char *ext = NULL; // Skip the first three lines for(k=0; k<3; k++) { int ret; i64 foundpos = 0; ret = dbuf_search_byte(inf, 0x0a, pos, pos1+len-pos, &foundpos); if(!ret) goto done; pos = foundpos+1; } dlen = pos1+len-pos; if(tcc->im_generic_profile_type==PROFILETYPE_XMP) { dump_to_file = 1; ext = "xmp"; } else if(tcc->im_generic_profile_type==PROFILETYPE_8BIM) { decode_to_membuf = 1; } else if(tcc->im_generic_profile_type==PROFILETYPE_IPTC) { if(c->extract_level>=2) { dump_to_file = 1; ext = "iptc"; } else { decode_to_membuf = 1; } } else if(tcc->im_generic_profile_type==PROFILETYPE_ICC) { dump_to_file = 1; ext = "icc"; } else { if(c->extract_level>=2) { dump_to_file = 1; ext = "profile.bin"; } } if(dump_to_file) { dbuf *outf; outf = dbuf_create_output_file(c, ext?ext:"bin", NULL, DE_CREATEFLAG_IS_AUX); de_decode_base16(c, inf, pos, dlen, outf, 0); dbuf_close(outf); } if(decode_to_membuf) { dbuf *tmpf; tmpf = dbuf_create_membuf(c, 0, 0); de_decode_base16(c, inf, pos, dlen, tmpf, 0); if(tcc->im_generic_profile_type==PROFILETYPE_8BIM) { de_fmtutil_handle_photoshop_rsrc(c, tmpf, 0, tmpf->len, 0x0); } else if(tcc->im_generic_profile_type==PROFILETYPE_IPTC) { de_fmtutil_handle_iptc(c, tmpf, 0, tmpf->len, 0x0); } dbuf_close(tmpf); } done: ; }
// XIMG and similar formats. // TODO: Should this function be merged with do_gem_img()? static int do_gem_ximg(deark *c, lctx *d) { dbuf *unc_pixels = NULL; de_bitmap *img = NULL; de_finfo *fi = NULL; int retval = 0; int saved_indent_level; de_dbg_indent_save(c, &saved_indent_level); de_dbg(c, "header (continued) at %d", 8*2); de_dbg_indent(c, 1); if((d->nplanes>=1 && d->nplanes<=8) /* || d->nplanes==24 */) { ; } else { if(d->is_ximg) de_err(c, "%d-plane XIMG images are not supported", (int)d->nplanes); else de_err(c, "This type of %d-plane image is not supported", (int)d->nplanes); goto done; } if(d->header_size_in_words==25 && !d->is_ximg) { i64 pal_pos = d->header_size_in_bytes-32; de_dbg(c, "palette at %d", (int)pal_pos); de_dbg_indent(c, 1); de_fmtutil_read_atari_palette(c, c->infile, pal_pos, d->pal, 16, ((i64)1)<<d->nplanes, 0); de_dbg_indent(c, -1); } else { read_palette_ximg(c, d); } if(d->nplanes==1 && d->pal[0]==d->pal[1]) { de_dbg(c, "Palette doesn't seem to be present. Using a default palette."); d->pal[0] = DE_STOCKCOLOR_WHITE; d->pal[1] = DE_STOCKCOLOR_BLACK; } de_dbg_indent(c, -1); de_dbg(c, "image at %d", (int)d->header_size_in_bytes); unc_pixels = dbuf_create_membuf(c, d->rowspan_total*d->h, 0); uncompress_pixels(c, d, unc_pixels, d->header_size_in_bytes, c->infile->len-d->header_size_in_bytes); img = de_bitmap_create(c, d->w, d->h, 3); fi = de_finfo_create(c); set_density(c, d, fi); if(d->nplanes>8) { read_rgb_image(c, d, unc_pixels, img); } else { read_paletted_image(c, d, unc_pixels, img); } de_bitmap_write_to_file_finfo(img, fi, 0); retval = 1; done: de_bitmap_destroy(img); de_finfo_destroy(c, fi); dbuf_close(unc_pixels); de_dbg_indent_restore(c, saved_indent_level); return retval; }