ImBuf *imb_load_jpeg(const unsigned char *buffer, size_t size, int flags, char colorspace[IM_MAX_SPACE]) { struct jpeg_decompress_struct _cinfo, *cinfo = &_cinfo; struct my_error_mgr jerr; ImBuf *ibuf; if (!imb_is_a_jpeg(buffer)) return NULL; colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE); cinfo->err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = jpeg_error; /* Establish the setjmp return context for my_error_exit to use. */ if (setjmp(jerr.setjmp_buffer)) { /* If we get here, the JPEG code has signaled an error. * We need to clean up the JPEG object, close the input file, and return. */ jpeg_destroy_decompress(cinfo); return NULL; } jpeg_create_decompress(cinfo); memory_source(cinfo, buffer, size); ibuf = ibJpegImageFromCinfo(cinfo, flags); return(ibuf); }
struct anim *IMB_open_anim(const char *name, int ib_flags, int streamindex, char colorspace[IM_MAX_SPACE]) { struct anim *anim; anim = (struct anim *)MEM_callocN(sizeof(struct anim), "anim struct"); if (anim != NULL) { if (colorspace) { colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE); BLI_strncpy(anim->colorspace, colorspace, sizeof(anim->colorspace)); } else { colorspace_set_default_role(anim->colorspace, sizeof(anim->colorspace), COLOR_ROLE_DEFAULT_BYTE); } BLI_strncpy(anim->name, name, sizeof(anim->name)); anim->ib_flags = ib_flags; anim->streamindex = streamindex; } return(anim); }
static struct ImBuf *imb_load_dpx_cineon(unsigned char *mem, size_t size, int use_cineon, int flags, char colorspace[IM_MAX_SPACE]) { ImBuf *ibuf; LogImageFile *image; int width, height, depth; colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_FLOAT); logImageSetVerbose((G.f & G_DEBUG) ? 1 : 0); image = logImageOpenFromMemory(mem, size); if (image == 0) { printf("DPX/Cineon: error opening image.\n"); return 0; } logImageGetSize(image, &width, &height, &depth); if (width == 0 || height == 0) { logImageClose(image); return 0; } ibuf = IMB_allocImBuf(width, height, 32, IB_rectfloat | flags); if (ibuf == 0) { logImageClose(image); return 0; } if (logImageGetDataRGBA(image, ibuf->rect_float, 1) != 0) { /* Conversion not possible (probably because the format is unsupported) */ logImageClose(image); MEM_freeN(ibuf); return 0; } logImageClose(image); ibuf->ftype = use_cineon ? CINEON : DPX; IMB_flipy(ibuf); if (flags & IB_rect) IMB_rect_from_float(ibuf); if (flags & IB_alphamode_detect) ibuf->flags |= IB_alphamode_premul; return ibuf; }
static struct ImBuf *imb_load_dpx_cineon(unsigned char *mem, size_t size, int use_cineon, int flags, char colorspace[IM_MAX_SPACE]) { ImBuf *ibuf; LogImageFile *image; int width, height, depth; colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_FLOAT); logImageSetVerbose((G.f & G_DEBUG) ? 1 : 0); image = logImageOpenFromMemory(mem, size); if (image == NULL) { printf("DPX/Cineon: error opening image.\n"); return NULL; } logImageGetSize(image, &width, &height, &depth); ibuf = IMB_allocImBuf(width, height, 32, IB_rectfloat | flags); if (ibuf == NULL) { logImageClose(image); return NULL; } if (!(flags & IB_test)) { if (logImageGetDataRGBA(image, ibuf->rect_float, 1) != 0) { logImageClose(image); IMB_freeImBuf(ibuf); return NULL; } IMB_flipy(ibuf); } logImageClose(image); ibuf->ftype = use_cineon ? CINEON : DPX; if (flags & IB_alphamode_detect) ibuf->flags |= IB_alphamode_premul; return ibuf; }
ImBuf *imb_loadpng(unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]) { struct ImBuf *ibuf = NULL; png_structp png_ptr; png_infop info_ptr; unsigned char *pixels = NULL; unsigned short *pixels16 = NULL; png_bytepp row_pointers = NULL; png_uint_32 width, height; int bit_depth, color_type; PNGReadStruct ps; unsigned char *from, *to; unsigned short *from16; float *to_float; int i, bytesperpixel; if (imb_is_a_png(mem) == 0) return(NULL); /* both 8 and 16 bit PNGs are default to standard byte colorspace */ colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE); png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (png_ptr == NULL) { printf("Cannot png_create_read_struct\n"); return NULL; } info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL) { png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); printf("Cannot png_create_info_struct\n"); return NULL; } ps.size = size; /* XXX, 4gig limit! */ ps.data = mem; ps.seek = 0; png_set_read_fn(png_ptr, (void *) &ps, ReadData); if (setjmp(png_jmpbuf(png_ptr))) { png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); if (pixels) MEM_freeN(pixels); if (pixels16) MEM_freeN(pixels16); if (row_pointers) MEM_freeN(row_pointers); if (ibuf) IMB_freeImBuf(ibuf); return NULL; } // png_set_sig_bytes(png_ptr, 8); png_read_info(png_ptr, info_ptr); png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL); bytesperpixel = png_get_channels(png_ptr, info_ptr); switch (color_type) { case PNG_COLOR_TYPE_RGB: case PNG_COLOR_TYPE_RGB_ALPHA: break; case PNG_COLOR_TYPE_PALETTE: png_set_palette_to_rgb(png_ptr); if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { bytesperpixel = 4; } else { bytesperpixel = 3; } break; case PNG_COLOR_TYPE_GRAY: case PNG_COLOR_TYPE_GRAY_ALPHA: if (bit_depth < 8) { png_set_expand(png_ptr); bit_depth = 8; } break; default: printf("PNG format not supported\n"); longjmp(png_jmpbuf(png_ptr), 1); } ibuf = IMB_allocImBuf(width, height, 8 * bytesperpixel, 0); if (ibuf) { ibuf->ftype = PNG; if (bit_depth == 16) ibuf->ftype |= PNG_16BIT; if (png_get_valid(png_ptr, info_ptr, PNG_INFO_pHYs)) { int unit_type; png_uint_32 xres, yres; if (png_get_pHYs(png_ptr, info_ptr, &xres, &yres, &unit_type)) if (unit_type == PNG_RESOLUTION_METER) { ibuf->ppm[0] = xres; ibuf->ppm[1] = yres; } } } else { printf("Couldn't allocate memory for PNG image\n"); } if (ibuf && ((flags & IB_test) == 0)) { if (bit_depth == 16) { imb_addrectfloatImBuf(ibuf); png_set_swap(png_ptr); pixels16 = MEM_mallocN(ibuf->x * ibuf->y * bytesperpixel * sizeof(png_uint_16), "pixels"); if (pixels16 == NULL) { printf("Cannot allocate pixels array\n"); longjmp(png_jmpbuf(png_ptr), 1); } /* allocate memory for an array of row-pointers */ row_pointers = (png_bytepp) MEM_mallocN(ibuf->y * sizeof(png_uint_16p), "row_pointers"); if (row_pointers == NULL) { printf("Cannot allocate row-pointers array\n"); longjmp(png_jmpbuf(png_ptr), 1); } /* set the individual row-pointers to point at the correct offsets */ for (i = 0; i < ibuf->y; i++) { row_pointers[ibuf->y - 1 - i] = (png_bytep) ((png_uint_16 *)pixels16 + (i * ibuf->x) * bytesperpixel); } png_read_image(png_ptr, row_pointers); /* copy image data */ to_float = ibuf->rect_float; from16 = pixels16; switch (bytesperpixel) { case 4: for (i = ibuf->x * ibuf->y; i > 0; i--) { to_float[0] = from16[0] / 65535.0; to_float[1] = from16[1] / 65535.0; to_float[2] = from16[2] / 65535.0; to_float[3] = from16[3] / 65535.0; to_float += 4; from16 += 4; } break; case 3: for (i = ibuf->x * ibuf->y; i > 0; i--) { to_float[0] = from16[0] / 65535.0; to_float[1] = from16[1] / 65535.0; to_float[2] = from16[2] / 65535.0; to_float[3] = 1.0; to_float += 4; from16 += 3; } break; case 2: for (i = ibuf->x * ibuf->y; i > 0; i--) { to_float[0] = to_float[1] = to_float[2] = from16[0] / 65535.0; to_float[3] = from16[1] / 65535.0; to_float += 4; from16 += 2; } break; case 1: for (i = ibuf->x * ibuf->y; i > 0; i--) { to_float[0] = to_float[1] = to_float[2] = from16[0] / 65535.0; to_float[3] = 1.0; to_float += 4; from16++; } break; } } else { imb_addrectImBuf(ibuf); pixels = MEM_mallocN(ibuf->x * ibuf->y * bytesperpixel * sizeof(unsigned char), "pixels"); if (pixels == NULL) { printf("Cannot allocate pixels array\n"); longjmp(png_jmpbuf(png_ptr), 1); } /* allocate memory for an array of row-pointers */ row_pointers = (png_bytepp) MEM_mallocN(ibuf->y * sizeof(png_bytep), "row_pointers"); if (row_pointers == NULL) { printf("Cannot allocate row-pointers array\n"); longjmp(png_jmpbuf(png_ptr), 1); } /* set the individual row-pointers to point at the correct offsets */ for (i = 0; i < ibuf->y; i++) { row_pointers[ibuf->y - 1 - i] = (png_bytep) ((unsigned char *)pixels + (i * ibuf->x) * bytesperpixel * sizeof(unsigned char)); } png_read_image(png_ptr, row_pointers); /* copy image data */ to = (unsigned char *) ibuf->rect; from = pixels; switch (bytesperpixel) { case 4: for (i = ibuf->x * ibuf->y; i > 0; i--) { to[0] = from[0]; to[1] = from[1]; to[2] = from[2]; to[3] = from[3]; to += 4; from += 4; } break; case 3: for (i = ibuf->x * ibuf->y; i > 0; i--) { to[0] = from[0]; to[1] = from[1]; to[2] = from[2]; to[3] = 0xff; to += 4; from += 3; } break; case 2: for (i = ibuf->x * ibuf->y; i > 0; i--) { to[0] = to[1] = to[2] = from[0]; to[3] = from[1]; to += 4; from += 2; } break; case 1: for (i = ibuf->x * ibuf->y; i > 0; i--) { to[0] = to[1] = to[2] = from[0]; to[3] = 0xff; to += 4; from++; } break; } } if (flags & IB_metadata) { png_text *text_chunks; int count = png_get_text(png_ptr, info_ptr, &text_chunks, NULL); for (i = 0; i < count; i++) { IMB_metadata_add_field(ibuf, text_chunks[i].key, text_chunks[i].text); ibuf->flags |= IB_metadata; } } png_read_end(png_ptr, info_ptr); } /* clean up */ if (pixels) MEM_freeN(pixels); if (pixels16) MEM_freeN(pixels16); if (row_pointers) MEM_freeN(row_pointers); png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); return(ibuf); }
ImBuf *imb_loadtarga(unsigned char *mem, size_t mem_size, int flags, char colorspace[IM_MAX_SPACE]) { TARGA tga; struct ImBuf *ibuf; int col, count, size; unsigned int *rect, *cmap = NULL /*, mincol = 0*/, maxcol = 0; uchar *cp = (uchar *) &col; if (checktarga(&tga, mem) == 0) return(NULL); colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE); if (flags & IB_test) ibuf = IMB_allocImBuf(tga.xsize, tga.ysize, tga.pixsize, 0); else ibuf = IMB_allocImBuf(tga.xsize, tga.ysize, (tga.pixsize + 0x7) & ~0x7, IB_rect); if (ibuf == NULL) return(NULL); ibuf->ftype = TGA; mem = mem + 18 + tga.numid; cp[0] = 0xff; cp[1] = cp[2] = 0; if (tga.mapsize) { /* load color map */ /*mincol = tga.maporig;*/ /*UNUSED*/ maxcol = tga.mapsize; cmap = MEM_callocN(sizeof(unsigned int) * maxcol, "targa cmap"); for (count = 0; count < maxcol; count++) { switch (tga.mapbits >> 3) { case 4: cp[0] = mem[3]; cp[1] = mem[0]; cp[2] = mem[1]; cp[3] = mem[2]; mem += 4; break; case 3: cp[1] = mem[0]; cp[2] = mem[1]; cp[3] = mem[2]; mem += 3; break; case 2: cp[1] = mem[1]; cp[0] = mem[0]; mem += 2; break; case 1: col = *mem++; break; } cmap[count] = col; } size = 0; for (col = maxcol - 1; col > 0; col >>= 1) size++; ibuf->planes = size; if (tga.mapbits != 32) { /* set alpha bits */ cmap[0] &= BIG_LONG(0x00ffffffl); } }
ImBuf *imb_loadtarga(const unsigned char *mem, size_t mem_size, int flags, char colorspace[IM_MAX_SPACE]) { TARGA tga; struct ImBuf *ibuf; int count, size; unsigned int *rect, *cmap = NULL /*, mincol = 0*/, cmap_max = 0; int32_t cp_data; uchar *cp = (uchar *)&cp_data; if (checktarga(&tga, mem) == 0) { return NULL; } colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE); if (flags & IB_test) { ibuf = IMB_allocImBuf(tga.xsize, tga.ysize, tga.pixsize, 0); } else { ibuf = IMB_allocImBuf(tga.xsize, tga.ysize, (tga.pixsize + 0x7) & ~0x7, IB_rect); } if (ibuf == NULL) { return NULL; } ibuf->ftype = IMB_FTYPE_TGA; if (tga.imgtyp < 4) { ibuf->foptions.flag |= RAWTGA; } mem = mem + 18 + tga.numid; cp[0] = 0xff; cp[1] = cp[2] = 0; if (tga.mapsize) { /* load color map */ /*mincol = tga.maporig;*/ /*UNUSED*/ cmap_max = tga.mapsize; cmap = MEM_callocN(sizeof(unsigned int) * cmap_max, "targa cmap"); for (count = 0; count < cmap_max; count++) { switch (tga.mapbits >> 3) { case 4: cp[0] = mem[3]; cp[1] = mem[0]; cp[2] = mem[1]; cp[3] = mem[2]; mem += 4; break; case 3: cp[1] = mem[0]; cp[2] = mem[1]; cp[3] = mem[2]; mem += 3; break; case 2: cp[1] = mem[1]; cp[0] = mem[0]; mem += 2; break; case 1: cp_data = *mem++; break; } cmap[count] = cp_data; } size = 0; for (int cmap_index = cmap_max - 1; cmap_index > 0; cmap_index >>= 1) { size++; } ibuf->planes = size; if (tga.mapbits != 32) { /* set alpha bits */ cmap[0] &= BIG_LONG(0x00ffffffl); } }
struct ImBuf *imb_jp2_decode(unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]) { struct ImBuf *ibuf = NULL; bool use_float = false; /* for precision higher then 8 use float */ bool use_alpha = false; long signed_offsets[4] = {0, 0, 0, 0}; int float_divs[4] = {1, 1, 1, 1}; unsigned int i, i_next, w, h, planes; unsigned int y; int *r, *g, *b, *a; /* matching 'opj_image_comp.data' type */ int is_jp2, is_j2k; opj_dparameters_t parameters; /* decompression parameters */ opj_event_mgr_t event_mgr; /* event manager */ opj_image_t *image = NULL; opj_dinfo_t *dinfo = NULL; /* handle to a decompressor */ opj_cio_t *cio = NULL; is_jp2 = check_jp2(mem); is_j2k = check_j2k(mem); if (!is_jp2 && !is_j2k) return(NULL); /* both 8, 12 and 16 bit JP2Ks are default to standard byte colorspace */ colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE); /* configure the event callbacks (not required) */ memset(&event_mgr, 0, sizeof(opj_event_mgr_t)); event_mgr.error_handler = error_callback; event_mgr.warning_handler = warning_callback; event_mgr.info_handler = info_callback; /* set decoding parameters to default values */ opj_set_default_decoder_parameters(¶meters); /* JPEG 2000 compressed image data */ /* get a decoder handle */ dinfo = opj_create_decompress(is_jp2 ? CODEC_JP2 : CODEC_J2K); /* catch events using our callbacks and give a local context */ opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr); /* setup the decoder decoding parameters using the current image and user parameters */ opj_setup_decoder(dinfo, ¶meters); /* open a byte stream */ cio = opj_cio_open((opj_common_ptr)dinfo, mem, size); /* decode the stream and fill the image structure */ image = opj_decode(dinfo, cio); if (!image) { fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); opj_destroy_decompress(dinfo); opj_cio_close(cio); return NULL; } /* close the byte stream */ opj_cio_close(cio); if ((image->numcomps * image->x1 * image->y1) == 0) { fprintf(stderr, "\nError: invalid raw image parameters\n"); return NULL; } w = image->comps[0].w; h = image->comps[0].h; switch (image->numcomps) { case 1: /* Grayscale */ case 3: /* Color */ planes = 24; use_alpha = false; break; default: /* 2 or 4 - Grayscale or Color + alpha */ planes = 32; /* grayscale + alpha */ use_alpha = true; break; } i = image->numcomps; if (i > 4) i = 4; while (i) { i--; if (image->comps[i].prec > 8) use_float = true; if (image->comps[i].sgnd) signed_offsets[i] = 1 << (image->comps[i].prec - 1); /* only needed for float images but dosnt hurt to calc this */ float_divs[i] = (1 << image->comps[i].prec) - 1; } ibuf = IMB_allocImBuf(w, h, planes, use_float ? IB_rectfloat : IB_rect); if (ibuf == NULL) { if (dinfo) opj_destroy_decompress(dinfo); return NULL; } ibuf->ftype = JP2; if (is_jp2) ibuf->ftype |= JP2_JP2; else ibuf->ftype |= JP2_J2K; if (use_float) { float *rect_float = ibuf->rect_float; if (image->numcomps < 3) { r = image->comps[0].data; a = (use_alpha) ? image->comps[1].data : NULL; /* grayscale 12bits+ */ if (use_alpha) { a = image->comps[1].data; PIXEL_LOOPER_BEGIN(rect_float) { rect_float[0] = rect_float[1] = rect_float[2] = (float)(r[i] + signed_offsets[0]) / float_divs[0]; rect_float[3] = (a[i] + signed_offsets[1]) / float_divs[1]; } PIXEL_LOOPER_END; } else { PIXEL_LOOPER_BEGIN(rect_float) { rect_float[0] = rect_float[1] = rect_float[2] = (float)(r[i] + signed_offsets[0]) / float_divs[0]; rect_float[3] = 1.0f; } PIXEL_LOOPER_END; } }
/** * Loads a TIFF file. * * * \param mem: Memory containing the TIFF file. * \param size: Size of the mem buffer. * \param flags: If flags has IB_test set then the file is not actually loaded, * but all other operations take place. * * \return: A newly allocated ImBuf structure if successful, otherwise NULL. */ ImBuf *imb_loadtiff(unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]) { TIFF *image = NULL; ImBuf *ibuf = NULL, *hbuf; ImbTIFFMemFile memFile; uint32 width, height; char *format = NULL; int level; short spp; int ib_depth; /* check whether or not we have a TIFF file */ if (size < IMB_TIFF_NCB) { fprintf(stderr, "imb_loadtiff: size < IMB_TIFF_NCB\n"); return NULL; } if (imb_is_a_tiff(mem) == 0) return NULL; /* both 8 and 16 bit PNGs are default to standard byte colorspace */ colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE); image = imb_tiff_client_open(&memFile, mem, size); if (image == NULL) { printf("imb_loadtiff: could not open TIFF IO layer.\n"); return NULL; } /* allocate the image buffer */ TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width); TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height); TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp); ib_depth = (spp == 3) ? 24 : 32; ibuf = IMB_allocImBuf(width, height, ib_depth, 0); if (ibuf) { ibuf->ftype = TIF; } else { fprintf(stderr, "imb_loadtiff: could not allocate memory for TIFF " "image.\n"); TIFFClose(image); return NULL; } /* get alpha mode from file header */ if (flags & IB_alphamode_detect) { if (spp == 4) { unsigned short extra, *extraSampleTypes; TIFFGetField(image, TIFFTAG_EXTRASAMPLES, &extra, &extraSampleTypes); if (extraSampleTypes[0] == EXTRASAMPLE_ASSOCALPHA) ibuf->flags |= IB_alphamode_premul; } } /* if testing, we're done */ if (flags & IB_test) { TIFFClose(image); return ibuf; } /* detect if we are reading a tiled/mipmapped texture, in that case * we don't read pixels but leave it to the cache to load tiles */ if (flags & IB_tilecache) { format = NULL; TIFFGetField(image, TIFFTAG_PIXAR_TEXTUREFORMAT, &format); if (format && strcmp(format, "Plain Texture") == 0 && TIFFIsTiled(image)) { int numlevel = TIFFNumberOfDirectories(image); /* create empty mipmap levels in advance */ for (level = 0; level < numlevel; level++) { if (!TIFFSetDirectory(image, level)) break; if (level > 0) { width = (width > 1) ? width / 2 : 1; height = (height > 1) ? height / 2 : 1; hbuf = IMB_allocImBuf(width, height, 32, 0); hbuf->miplevel = level; hbuf->ftype = ibuf->ftype; ibuf->mipmap[level - 1] = hbuf; } else hbuf = ibuf; hbuf->flags |= IB_tilecache; TIFFGetField(image, TIFFTAG_TILEWIDTH, &hbuf->tilex); TIFFGetField(image, TIFFTAG_TILELENGTH, &hbuf->tiley); hbuf->xtiles = ceil(hbuf->x / (float)hbuf->tilex); hbuf->ytiles = ceil(hbuf->y / (float)hbuf->tiley); imb_addtilesImBuf(hbuf); ibuf->miptot++; } } } /* read pixels */ if (!(ibuf->flags & IB_tilecache) && !imb_read_tiff_pixels(ibuf, image)) { fprintf(stderr, "imb_loadtiff: Failed to read tiff image.\n"); TIFFClose(image); return NULL; } /* close the client layer interface to the in-memory file */ TIFFClose(image); /* return successfully */ return ibuf; }
struct ImBuf *imb_loadhdr(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]) { struct ImBuf *ibuf; RGBE *sline; fCOLOR fcol; float *rect_float; int found = 0; int width = 0, height = 0; int x, y; const unsigned char *ptr, *mem_eof = mem + size; char oriY[80], oriX[80]; if (imb_is_a_hdr((void *)mem)) { colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_FLOAT); /* find empty line, next line is resolution info */ for (x = 1; x < size; x++) { if ((mem[x - 1] == '\n') && (mem[x] == '\n')) { found = 1; break; } } if (found && (x < (size + 2))) { if (sscanf((char *)&mem[x + 1], "%79s %d %79s %d", (char *)&oriY, &height, (char *)&oriX, &width) != 4) { return NULL; } /* find end of this line, data right behind it */ ptr = (unsigned char *)strchr((char *)&mem[x + 1], '\n'); ptr++; if (flags & IB_test) ibuf = IMB_allocImBuf(width, height, 32, 0); else ibuf = IMB_allocImBuf(width, height, 32, (flags & IB_rect) | IB_rectfloat); if (ibuf == NULL) return NULL; ibuf->ftype = IMB_FTYPE_RADHDR; if (flags & IB_alphamode_detect) ibuf->flags |= IB_alphamode_premul; if (flags & IB_test) return ibuf; /* read in and decode the actual data */ sline = (RGBE *)MEM_mallocN(sizeof(*sline) * width, __func__); rect_float = ibuf->rect_float; for (y = 0; y < height; y++) { ptr = freadcolrs(sline, ptr, width, mem_eof); if (ptr == NULL) { printf("WARNING! HDR decode error, image may be just truncated, or completely wrong...\n"); break; } for (x = 0; x < width; x++) { /* convert to ldr */ RGBE2FLOAT(sline[x], fcol); *rect_float++ = fcol[RED]; *rect_float++ = fcol[GRN]; *rect_float++ = fcol[BLU]; *rect_float++ = 1.0f; } } MEM_freeN(sline); if (oriY[0] == '-') IMB_flipy(ibuf); if (flags & IB_rect) { IMB_rect_from_float(ibuf); } return ibuf; } //else printf("Data not found!\n"); } //else printf("Not a valid radiance HDR file!\n"); return NULL; }
static ImBuf *imb_load_jp2_stream(opj_stream_t *stream, const OPJ_CODEC_FORMAT format, int flags, char colorspace[IM_MAX_SPACE]) { if (format == OPJ_CODEC_UNKNOWN) { return NULL; } struct ImBuf *ibuf = NULL; bool use_float = false; /* for precision higher then 8 use float */ bool use_alpha = false; long signed_offsets[4] = {0, 0, 0, 0}; int float_divs[4] = {1, 1, 1, 1}; unsigned int i, i_next, w, h, planes; unsigned int y; int *r, *g, *b, *a; /* matching 'opj_image_comp.data' type */ opj_dparameters_t parameters; /* decompression parameters */ opj_image_t *image = NULL; opj_codec_t *codec = NULL; /* handle to a decompressor */ /* both 8, 12 and 16 bit JP2Ks are default to standard byte colorspace */ colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE); /* set decoding parameters to default values */ opj_set_default_decoder_parameters(¶meters); /* JPEG 2000 compressed image data */ /* get a decoder handle */ codec = opj_create_decompress(format); /* configure the event callbacks (not required) */ opj_set_error_handler(codec, error_callback, stderr); opj_set_warning_handler(codec, warning_callback, stderr); #ifdef DEBUG /* too noisy */ opj_set_info_handler(codec, info_callback, stderr); #endif /* setup the decoder decoding parameters using the current image and user parameters */ if (opj_setup_decoder(codec, ¶meters) == false) { goto finally; } if (opj_read_header(stream, codec, &image) == false) { printf("OpenJPEG error: failed to read the header\n"); goto finally; } /* decode the stream and fill the image structure */ if (opj_decode(codec, stream, image) == false) { fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n"); goto finally; } if ((image->numcomps * image->x1 * image->y1) == 0) { fprintf(stderr, "\nError: invalid raw image parameters\n"); goto finally; } w = image->comps[0].w; h = image->comps[0].h; switch (image->numcomps) { case 1: /* Grayscale */ case 3: /* Color */ planes = 24; use_alpha = false; break; default: /* 2 or 4 - Grayscale or Color + alpha */ planes = 32; /* grayscale + alpha */ use_alpha = true; break; } i = image->numcomps; if (i > 4) { i = 4; } while (i) { i--; if (image->comps[i].prec > 8) { use_float = true; } if (image->comps[i].sgnd) { signed_offsets[i] = 1 << (image->comps[i].prec - 1); } /* only needed for float images but dosnt hurt to calc this */ float_divs[i] = (1 << image->comps[i].prec) - 1; } ibuf = IMB_allocImBuf(w, h, planes, use_float ? IB_rectfloat : IB_rect); if (ibuf == NULL) { goto finally; } ibuf->ftype = IMB_FTYPE_JP2; if (1 /* is_jp2 */) { ibuf->foptions.flag |= JP2_JP2; } else { ibuf->foptions.flag |= JP2_J2K; } if (use_float) { float *rect_float = ibuf->rect_float; if (image->numcomps < 3) { r = image->comps[0].data; a = (use_alpha) ? image->comps[1].data : NULL; /* grayscale 12bits+ */ if (use_alpha) { a = image->comps[1].data; PIXEL_LOOPER_BEGIN (rect_float) { rect_float[0] = rect_float[1] = rect_float[2] = (float)(r[i] + signed_offsets[0]) / float_divs[0]; rect_float[3] = (a[i] + signed_offsets[1]) / float_divs[1]; } PIXEL_LOOPER_END; } else { PIXEL_LOOPER_BEGIN (rect_float) { rect_float[0] = rect_float[1] = rect_float[2] = (float)(r[i] + signed_offsets[0]) / float_divs[0]; rect_float[3] = 1.0f; } PIXEL_LOOPER_END; } }