Example #1
0
struct ImBuf *imb_loadhdr(unsigned char *mem, size_t size, int flags)
{
	struct ImBuf* ibuf;
	RGBE* sline;
	fCOLOR fcol;
	float* rect_float;
	int found=0;
	int width=0, height=0;
	int x, y;
	unsigned char* ptr;
	char oriY[80], oriX[80];

	if (imb_is_a_hdr((void*)mem))
	{
		/* 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 = RADHDR;
			ibuf->profile = IB_PROFILE_LINEAR_RGB;

			if (flags & IB_test) return ibuf;

			/* read in and decode the actual data */
			sline = (RGBE*)MEM_mallocN(sizeof(RGBE)*width, "radhdr_read_tmpscan");
			rect_float = (float *)ibuf->rect_float;
			
			for (y=0;y<height;y++) {
				ptr = freadcolrs(sline, ptr, width);
				if (ptr==NULL) {
					printf("HDR decode error\n");
					MEM_freeN(sline);
					return ibuf;
				}
				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;
}
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;
}