Example #1
0
bool HDRLoader::load(const char *fileName, HDRLoaderResult &res)
{
	int i;
	char str[200];
	FILE *file;

	file = fopen(fileName, "rb");
	if (!file)
		return false;

	fread(str, 10, 1, file);
	if (memcmp(str, "#?RADIANCE", 10)) {
		fclose(file);
		return false;
	}

	fseek(file, 1, SEEK_CUR);

	char cmd[200];
	i = 0;
	char c = 0, oldc;
	while(true) {
		oldc = c;
		c = fgetc(file);
		if (c == 0xa && oldc == 0xa)
			break;
		cmd[i++] = c;
	}

	char reso[200];
	i = 0;
	while(true) {
		c = fgetc(file);
		reso[i++] = c;
		if (c == 0xa)
			break;
	}

	int w, h;
	if (!sscanf(reso, "-Y %ld +X %ld", &h, &w)) {
		fclose(file);
		return false;
	}

	res.width = w;
	res.height = h;

	float *cols = new float[w * h * 3];
	res.cols = cols;

	RGBE *scanline = new RGBE[w];
	if (!scanline) {
		fclose(file);
		return false;
	}

	// convert image 
	for (int y = h - 1; y >= 0; y--) {
		if (decrunch(scanline, w, file) == false)
			break;
		workOnRGBE(scanline, w, cols);
		cols += w * 3;
	}

	delete [] scanline;
	fclose(file);

	return true;
}
bool HDRLoader::load(const char *_fileName, const bool _rawRGBE, HDRLoaderResult &_res)
{
    int  i;
    char str[200];
    FILE *file;

    file = osgDB::fopen(_fileName, "rb");
    if (!file)
        return false;

    size_t numRead = fread(str, 10, 1, file);

    if (numRead < 1)
    {
        fclose(file);
        return false;
    }

    if (memcmp(str, "#?RADIANCE", 10))
    {
        fseek(file, 0, SEEK_SET);
        numRead = fread(str, 6, 1, file);
        if (numRead < 1 || memcmp(str, "#?RGBE", 6))
        {
            fclose(file);
            return false;
        }
    }

    fseek(file, 1, SEEK_CUR);

    // char cmd[2000];
    i = 0;
    char c = 0, oldc;

    while (true)
    {
        oldc = c;
        c    = fgetc(file);
        if (c == 0xa && oldc == 0xa)
            break;

        // cmd[i++] = c;
    }

    char reso[2000];
    i = 0;

    while (true)
    {
        c         = fgetc(file);
        reso[i++] = c;
        if (c == 0xa)
            break;
    }

    int w, h;
    if (!sscanf(reso, "-Y %d +X %d", &h, &w))
    {
        fclose(file);
        return false;
    }

    _res.width  = w;
    _res.height = h;

    int   components = _rawRGBE ? 4 : 3;
    float *cols      = new float[w * h * components];
    _res.cols = cols;

    RGBE *scanline = new RGBE[w];
    if (!scanline)
    {
        fclose(file);
        return false;
    }

    // convert image
    cols += (h - 1) * w * components;

    for (int y = h - 1; y >= 0; y--)
    {
        if (decrunch(scanline, w, file) == false)
            break;

        if (_rawRGBE)
            rawRGBEData(scanline, w, cols);
        else
            workOnRGBE(scanline, w, cols);

        cols -= w * components;
    }

    delete[] scanline;
    fclose(file);

    return true;
}