1/*
2 * Copyright 2007-2008, Axel D��rfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef RAW_H
6#define RAW_H
7
8
9#include "ReadHelper.h"
10
11
12struct jhead;
13struct tiff_tag;
14
15
16struct image_meta_info {
17	char	manufacturer[64];
18	char	model[128];
19	char	software[64];
20	float	flash_used;
21	float	iso_speed;
22	float	shutter;
23	float	aperture;
24	float	focal_length;
25	double	pixel_aspect;
26	uint32	raw_width;
27	uint32	raw_height;
28	int		flip;
29	uint32	dng_version;
30	uint32	shot_order;
31	int32	black;
32	int32	maximum;
33	float	camera_multipliers[4];
34	float	pre_multipliers[4];
35	float	rgb_camera[3][4];	/* RGB from camera color */
36	time_t	timestamp;
37};
38
39struct image_data_info {
40	uint32	width;
41	uint32	height;
42	uint32	output_width;
43	uint32	output_height;
44	uint32	bits_per_sample;
45	uint32	compression;
46	uint32	photometric_interpretation;
47	uint32	flip;
48	uint32	samples;
49	uint32	bytes;
50	off_t	data_offset;
51	bool	is_raw;
52};
53
54#define COMPRESSION_NONE		1
55#define COMPRESSION_OLD_JPEG	6		// Old JPEG (before 6.0)
56#define COMPRESSION_PACKBITS	32773	// Macintosh RLE
57
58
59typedef void (*monitor_hook)(const char* message, float percentage, void* data);
60
61class DCRaw {
62	public:
63		DCRaw(BPositionIO& stream);
64		~DCRaw();
65
66		status_t Identify();
67		status_t ReadImageAt(uint32 index, uint8*& outputBuffer,
68			size_t& bufferSize);
69
70		void GetMetaInfo(image_meta_info& metaInfo) const;
71		uint32 CountImages() const;
72		status_t ImageAt(uint32 index, image_data_info& info) const;
73
74		status_t GetEXIFTag(off_t& offset, size_t& length,
75			bool& bigEndian) const;
76		void SetProgressMonitor(monitor_hook hook, void* data);
77
78		void SetHalfSize(bool half);
79
80	private:
81		int32 _AllocateImage();
82		image_data_info& _Raw();
83		image_data_info& _Thumb();
84		void _CorrectIndex(uint32& index) const;
85		uint16& _Bayer(int32 column, int32 row);
86		int32 _FilterCoefficient(int32 column, int32 row);
87		int32 _FlipIndex(uint32 row, uint32 col, uint32 flip);
88		bool _SupportsCompression(image_data_info& info) const;
89		bool _IsCanon() const;
90		bool _IsKodak() const;
91		bool _IsNikon() const;
92		bool _IsOlympus() const;
93		bool _IsPentax() const;
94		bool _IsSamsung() const;
95
96		// image manipulation and conversion
97		void _ScaleColors();
98		void _WaveletDenoise();
99		void _PreInterpolate();
100		void _CameraToCIELab(ushort cam[4], float lab[3]);
101		void _CameraXYZCoefficients(double cam_xyz[4][3]);
102		void _AdobeCoefficients(const char *manufacturer, const char *model);
103		void _BorderInterpolate(uint32 border);
104		void _AHDInterpolate();
105		void _PseudoInverse(double (*in)[3], double (*out)[3], uint32 size);
106		void _ConvertToRGB();
107		void _GammaLookUpTable(uchar* lut);
108
109		void _ParseThumbTag(off_t baseOffset, uint32 offsetTag, uint32 lengthTag);
110		void _ParseManufacturerTag(off_t baseOffset);
111		void _ParseEXIF(off_t baseOffset);
112		void _ParseLinearTable(uint32 length);
113		void _FixupValues();
114
115		// Lossless JPEG
116		void _InitDecoder();
117		uchar *_MakeDecoder(const uchar* source, int level);
118		void _InitDecodeBits();
119		uint32 _GetDecodeBits(uint32 numBits);
120		status_t _LosslessJPEGInit(struct jhead* jh, bool infoOnly);
121		int _LosslessJPEGDiff(struct decode *dindex);
122		void _LosslessJPEGRow(struct jhead *jh, int jrow);
123
124		// RAW Loader
125		void _LoadRAWUnpacked(const image_data_info& image);
126		void _LoadRAWPacked12(const image_data_info& info);
127		void _MakeCanonDecoder(uint32 table);
128		bool _CanonHasLowBits();
129		void _LoadRAWCanonCompressed(const image_data_info& info);
130		void _LoadRAWLosslessJPEG(const image_data_info& image);
131		void _LoadRAW(const image_data_info& info);
132
133		// Image writers
134		void _WriteJPEG(image_data_info& image, uint8* outputBuffer);
135		void _WriteRGB32(image_data_info& image, uint8* outputBuffer);
136
137		// TIFF
138		time_t _ParseTIFFTimestamp(bool reversed);
139		void _ParseTIFFTag(off_t baseOffset, tiff_tag& tag, off_t& offset);
140		status_t _ParseTIFFImageFileDirectory(off_t baseOffset, uint32 offset);
141		status_t _ParseTIFFImageFileDirectory(off_t baseOffset);
142		status_t _ParseTIFF(off_t baseOffset);
143
144		TReadHelper	fRead;
145		image_meta_info fMeta;
146		image_data_info* fImages;
147		uint32		fNumImages;
148
149		int32		fRawIndex;
150		int32		fThumbIndex;
151		uint32		fDNGVersion;
152		bool		fIsTIFF;
153
154		uint16		(*fImageData)[4];
155			// output image data
156		float		fThreshold;
157		int32		fShrink;
158		bool		fHalfSize;
159		bool		fUseCameraWhiteBalance;
160		bool		fUseAutoWhiteBalance;
161		bool		fRawColor;
162		bool		fUseGamma;
163		float		fBrightness;
164		int32		fOutputColor;
165		int32		fHighlight;
166		int32		fDocumentMode;
167		uint32		fOutputWidth;
168		uint32		fOutputHeight;
169		uint32		fInputWidth;
170		uint32		fInputHeight;
171		int32		fTopMargin;
172		int32		fLeftMargin;
173		uint32		fUniqueID;
174		uint32		fColors;
175		uint16		fWhite[8][8];
176		float		fUserMultipliers[4];
177		uint16*		fCurve;
178		uint16		fCR2Slice[3];
179		uint32*		fOutputProfile;
180		uint32		fOutputBitsPerSample;
181		int32		(*fHistogram)[4];
182		float*		cbrt;
183		float		xyz_cam[3][4];
184			// TODO: find better names for these...
185
186		// Lossless JPEG
187		decode*		fDecodeBuffer;
188		decode*		fSecondDecode;
189		decode*		fFreeDecode;
190		int			fDecodeLeaf;
191		uint32		fDecodeBits;
192		uint32		fDecodeBitsRead;
193		bool		fDecodeBitsReset;
194		bool		fDecodeBitsZeroAfterMax;
195
196		uint32		fFilters;
197		uint32		fEXIFFilters;
198		off_t		fEXIFOffset;
199		uint32		fEXIFLength;
200
201		off_t		fCurveOffset;
202
203		monitor_hook fProgressMonitor;
204		void*		fProgressData;
205};
206
207#endif	// RAW_H
208