1 #include <stdio.h>
2 #include "rgbe.h"
3 #include "filter.h"
4 #include "metadata.h"
5 
6 
7 
writeHDR(Image * im,fullPath * sfile)8 int writeHDR( Image *im, fullPath *sfile)
9 {
10 	rgbe_header_info rgbe_h;
11 	FILE * outfile;
12 	char filename[512];
13 	unsigned char *data;
14 	float *fdata = NULL;
15 	int i;
16 
17 
18 #ifdef __Mac__
19 	unsigned char the_pcUnixFilePath[512];// added by Kekus Digital
20 	Str255 the_cString;
21 	Boolean the_bReturnValue;
22 	CFStringRef the_FilePath;
23 	CFURLRef the_Url;//till here
24 #endif
25 
26 	if( GetFullPath (sfile, filename))
27 		return -1;
28 
29 #ifdef __Mac__
30 	CopyCStringToPascal(filename,the_cString);//Added by Kekus Digital
31 	the_FilePath = CFStringCreateWithPascalString(kCFAllocatorDefault, the_cString, kCFStringEncodingUTF8);
32 	the_Url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, the_FilePath, kCFURLHFSPathStyle, false);
33 	the_bReturnValue = CFURLGetFileSystemRepresentation(the_Url, true, the_pcUnixFilePath, 512);
34 
35 	strcpy(filename, the_pcUnixFilePath);//till here
36 #endif
37 
38 	data=malloc(im->width * im->height * 4 * 3);
39 
40 	if (im->bitsPerPixel==128) {
41 		float *srcdata;
42 		fdata=(float *)data;
43 		srcdata=(float *)*im->data;
44 		for(i=0;i<(im->width * im->height); i++) {
45 			srcdata++; // skip alpha
46 			*fdata++=*srcdata++;
47 			*fdata++=*srcdata++;
48 			*fdata++=*srcdata++;
49 		}
50 		fdata=(float *)data;
51 	}
52 	if (im->bitsPerPixel==96) {
53 		fdata=(float *)*im->data;
54 	}
55 	if ((im->bitsPerPixel==64) || (im->bitsPerPixel==48)) {
56    		double gnorm;
57 		unsigned short *srcdata;
58 		fdata=(float *)data;
59 		srcdata=(unsigned short *)*im->data;
60 
61 		gnorm = (1 / pow( 65535.0 , 2.2 ));
62 		for(i=0;i<(im->width * im->height); i++) {
63 			if (im->bitsPerPixel==64) srcdata++; // skip alpha
64 			*fdata++=(float)(pow((double)*srcdata++,2.2) * gnorm);
65 			*fdata++=(float)(pow((double)*srcdata++,2.2) * gnorm);
66 			*fdata++=(float)(pow((double)*srcdata++,2.2) * gnorm);
67 //			*fdata++=pow((double)*sdata++ * (1.0/65535.0);
68 		}
69 		fdata=(float *)data;
70 	}
71 	if ((im->bitsPerPixel==32) || (im->bitsPerPixel==24)) {
72    		double gnorm;
73 		unsigned char *srcdata;
74 		fdata=(float *)data;
75 		srcdata=(unsigned char *)*im->data;
76 		gnorm = (1 / pow( 255.0 , 2.2 ));
77 		for(i=0;i<(im->width * im->height); i++) {
78 			if (im->bitsPerPixel==32) srcdata++; // skip alpha
79 			*fdata++=(float)(pow((double)*srcdata++,2.2) * gnorm);
80 			*fdata++=(float)(pow((double)*srcdata++,2.2) * gnorm);
81 			*fdata++=(float)(pow((double)*srcdata++,2.2) * gnorm);
82 //			*fdata++=(*cdata++) * (1.0/255.0);
83 		}
84 		fdata=(float *)data;
85 	}
86 	if ((outfile = fopen(filename, "wb")) == NULL)
87 	{
88 	    PrintError("can't open %s", filename);
89       free( data );
90 	    return -1;
91 	}
92 
93 	rgbe_h.valid=-1;
94 	strcpy(rgbe_h.programtype,"RADIANCE");
95 	rgbe_h.gamma=1.0;
96 	rgbe_h.exposure=1.0;
97 
98 	RGBE_WriteHeader(outfile, im->width,im->height, &rgbe_h);
99 	RGBE_WritePixels(outfile,fdata, im->width * im->height );
100 
101 	fclose( outfile );
102 	free( data );
103 	return 0;
104 }
105 
106 
readHDR(Image * im,fullPath * sfile)107 int readHDR ( Image *im, fullPath *sfile )
108 {
109 	rgbe_header_info rgbe_h;
110 	FILE * infile;
111 	float *srcdata, *fdata;
112 	char filename[256];
113 	int i;
114 
115 #ifdef __Mac__
116 	unsigned char the_pcUnixFilePath[256];// added by Kekus Digital
117 	Str255 the_cString;
118 	Boolean the_bReturnValue;
119 	CFStringRef the_FilePath;
120 	CFURLRef the_Url;//till here
121 #endif
122 
123 	if( GetFullPath (sfile, filename))
124 		return -1;
125 
126 #ifdef __Mac__
127 	CopyCStringToPascal(filename,the_cString);//Added by Kekus Digital
128 	the_FilePath = CFStringCreateWithPascalString(kCFAllocatorDefault, the_cString, kCFStringEncodingUTF8);
129 	the_Url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, the_FilePath, kCFURLHFSPathStyle, false);
130 	the_bReturnValue = CFURLGetFileSystemRepresentation(the_Url, true, the_pcUnixFilePath, 256);
131 
132 	strcpy(filename, the_pcUnixFilePath);//till here
133 #endif
134 
135 	if ((infile = fopen(filename, "rb")) == NULL)
136 	{
137 	    PrintError("can't open %s", filename);
138 	    return -1;
139 	}
140 	SetImageDefaults( im );
141 	RGBE_ReadHeader(infile,&im->width,&im->height,&rgbe_h);
142 
143 	im->bitsPerPixel = 96;
144 	im->bytesPerLine = im->width * 4 * 4;
145 	im->dataSize = im->bytesPerLine * im->height;
146 	im->data = (unsigned char**)mymalloc( (size_t)im->dataSize );
147 	if( im->data == NULL )
148 	{
149 		PrintError("Not enough memory");
150 		fclose( infile );
151 		return -1;
152 	}
153 	RGBE_ReadPixels_RLE(infile,(float *)*im->data, im->width, im->height);
154 
155 	fdata=(float *)*im->data;
156 	srcdata=(float *)*im->data;
157 	fdata+=(im->width * im->height)*4;
158 	srcdata+=(im->width * im->height)*3;
159 	for(i=0;i<(im->width * im->height); i++) {
160 		*(--fdata)=*(--srcdata);
161 		*(--fdata)=*(--srcdata);
162 		*(--fdata)=*(--srcdata);
163 
164 		*(--fdata)=1.0; // alpha
165 	}
166 	im->bitsPerPixel = 128;
167 
168 	fclose( infile );
169 
170 	return 0;
171 }
172 
panoHDRRead(Image * im,fullPath * sfile)173 int panoHDRRead(Image *im, fullPath *sfile )
174 {
175     if (readHDR(im, sfile) == 0) {
176 	return panoMetadataUpdateFromImage(im);
177     } else
178 	return FALSE;
179 }
180