1 /*****************************************************************************/
2 // Copyright 2008-2019 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE:  Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8 
9 /** \file
10  * Miscellaneous DNG opcodes.
11  */
12 
13 /*****************************************************************************/
14 
15 #ifndef __dng_misc_opcodes__
16 #define __dng_misc_opcodes__
17 
18 /*****************************************************************************/
19 
20 #include "dng_classes.h"
21 
22 #include "dng_opcodes.h"
23 
24 /*****************************************************************************/
25 
26 /// \brief Opcode to trim image to a specified rectangle.
27 
28 class dng_opcode_TrimBounds: public dng_opcode
29 	{
30 
31 	private:
32 
33 		dng_rect fBounds;
34 
35 	public:
36 
37 		/// Create opcode to trim image to the specified bounds.
38 
39 		dng_opcode_TrimBounds (const dng_rect &bounds);
40 
41 		dng_opcode_TrimBounds (dng_stream &stream);
42 
43 		virtual void PutData (dng_stream &stream) const;
44 
45 		virtual void Apply (dng_host &host,
46 							dng_negative &negative,
47 							AutoPtr<dng_image> &image);
48 
49 	};
50 
51 /*****************************************************************************/
52 
53 /// \brief A class to describe an area of an image, including a pixel subrectangle,
54 /// plane range, and row/column pitch (e.g., for mosaic images). Useful for
55 /// specifying opcodes that only apply to specific color planes or pixel types (e.g.,
56 /// only one of the two green Bayer pixels).
57 
58 class dng_area_spec
59 	{
60 
61 	public:
62 
63 		enum
64 			{
65 			kDataSize = 32
66 			};
67 
68 	private:
69 
70 		dng_rect fArea;
71 
72 		uint32 fPlane;
73 		uint32 fPlanes;
74 
75 		uint32 fRowPitch;
76 		uint32 fColPitch;
77 
78 	public:
79 
80 		/// Create an empty area.
81 
82 		dng_area_spec (const dng_rect &area = dng_rect (),
83 					   uint32 plane = 0,
84 					   uint32 planes = 1,
85 					   uint32 rowPitch = 1,
86 					   uint32 colPitch = 1)
87 
fArea(area)88 			:	fArea     (area)
89 			,	fPlane    (plane)
90 			,	fPlanes   (planes)
91 			,	fRowPitch (rowPitch)
92 			,	fColPitch (colPitch)
93 
94 			{
95 			}
96 
97 		/// The pixel area.
98 
Area()99 		const dng_rect & Area () const
100 			{
101 			return fArea;
102 			}
103 
104 		/// The first plane.
105 
Plane()106 		uint32 Plane () const
107 			{
108 			return fPlane;
109 			}
110 
111 		/// The total number of planes.
112 
Planes()113 		uint32 Planes () const
114 			{
115 			return fPlanes;
116 			}
117 
118 		/// The row pitch (i.e., stride). A pitch of 1 means all rows.
119 
RowPitch()120 		uint32 RowPitch () const
121 			{
122 			return fRowPitch;
123 			}
124 
125 		/// The column pitch (i.e., stride). A pitch of 1 means all columns.
126 
ColPitch()127 		uint32 ColPitch () const
128 			{
129 			return fColPitch;
130 			}
131 
132 		/// Read area data from the specified stream.
133 
134 		void GetData (dng_stream &stream);
135 
136 		/// Write area data to the specified stream.
137 
138 		void PutData (dng_stream &stream) const;
139 
140 		/// Compute and return pixel area overlap (i.e., intersection) between this
141 		/// area and the specified tile.
142 
143 		dng_rect Overlap (const dng_rect &tile) const;
144 
145 	};
146 
147 /*****************************************************************************/
148 
149 /// \brief An opcode to apply a 1D function (represented as a 16-bit table) to an
150 /// image area.
151 
152 class dng_opcode_MapTable: public dng_inplace_opcode
153 	{
154 
155 	private:
156 
157 		dng_area_spec fAreaSpec;
158 
159 		AutoPtr<dng_memory_block> fTable;
160 
161 		uint32 fCount;
162 
163         AutoPtr<dng_memory_block> fBlackAdjustedTable;
164 
165 	public:
166 
167 		/// Create a MapTable opcode with the specified area, table, and number of
168 		/// table entries.
169 
170 		dng_opcode_MapTable (dng_host &host,
171 							 const dng_area_spec &areaSpec,
172 							 const uint16 *table,
173 							 uint32 count = 0x10000);
174 
175 		dng_opcode_MapTable (dng_host &host,
176 							 dng_stream &stream);
177 
178 		virtual void PutData (dng_stream &stream) const;
179 
180 		virtual uint32 BufferPixelType (uint32 imagePixelType);
181 
182 		virtual dng_rect ModifiedBounds (const dng_rect &imageBounds);
183 
184         virtual void Prepare (dng_negative &negative,
185                               uint32 threadCount,
186                               const dng_point &tileSize,
187                               const dng_rect &imageBounds,
188                               uint32 imagePlanes,
189                               uint32 bufferPixelType,
190                               dng_memory_allocator &allocator);
191 
192 		virtual void ProcessArea (dng_negative &negative,
193 								  uint32 threadIndex,
194 								  dng_pixel_buffer &buffer,
195 								  const dng_rect &dstArea,
196 								  const dng_rect &imageBounds);
197 
198 	private:
199 
200 		void ReplicateLastEntry ();
201 
202 	};
203 
204 /*****************************************************************************/
205 
206 /// \brief An opcode to apply a 1D function (represented as a polynomial) to an
207 /// image area.
208 
209 class dng_opcode_MapPolynomial: public dng_inplace_opcode
210 	{
211 
212 	public:
213 
214 		enum
215 			{
216 			kMaxDegree = 8
217 			};
218 
219 	protected:
220 
221 		dng_area_spec fAreaSpec;
222 
223 		uint32 fDegree;
224 
225 		real64 fCoefficient [kMaxDegree + 1];
226 
227 		real32 fCoefficient32 [kMaxDegree + 1];
228 
229 	public:
230 
231 		/// Create a MapPolynomial opcode with the specified area, polynomial
232 		/// degree, and polynomial coefficients. The function that will be
233 		/// applied to each pixel x is:
234 		///
235 		/// f (x) = coefficient [0] + ((x	* coefficient [1]) +
236 		///							   (x^2 * coefficient [2]) +
237 		///							   (x^3 * coefficient [3]) +
238 		///							   (x^4 * coefficient [4]) ...
239 
240 		dng_opcode_MapPolynomial (const dng_area_spec &areaSpec,
241 								  uint32 degree,
242 								  const real64 *coefficient);
243 
244 		dng_opcode_MapPolynomial (dng_stream &stream);
245 
246 		virtual void PutData (dng_stream &stream) const;
247 
248 		virtual uint32 BufferPixelType (uint32 imagePixelType);
249 
250 		virtual dng_rect ModifiedBounds (const dng_rect &imageBounds);
251 
252 		virtual void ProcessArea (dng_negative &negative,
253 								  uint32 threadIndex,
254 								  dng_pixel_buffer &buffer,
255 								  const dng_rect &dstArea,
256 								  const dng_rect &imageBounds);
257 
Degree()258 		uint32 Degree () const
259 			{
260 			return fDegree;
261 			}
262 
Coefficients()263 		const real64 * Coefficients () const
264 			{
265 			return fCoefficient;
266 			}
267 
268 	protected:
269 
270 		virtual void DoProcess (dng_pixel_buffer &buffer,
271 								const dng_rect &area,
272 								const uint32 plane,
273 								const uint32 rowPitch,
274 								const uint32 colPitch,
275 								const real32 *coefficients,
276 								const uint32 degree,
277                                 uint16 blackLevel) const;
278 
279 	};
280 
281 /*****************************************************************************/
282 
283 /// \brief An opcode to apply a delta (i.e., offset) that varies per row. Within
284 /// a row, the same delta value is applied to all specified pixels.
285 
286 class dng_opcode_DeltaPerRow: public dng_inplace_opcode
287 	{
288 
289 	private:
290 
291 		dng_area_spec fAreaSpec;
292 
293 		AutoPtr<dng_memory_block> fTable;
294 
295 		real32 fScale;
296 
297 	public:
298 
299 		/// Create a DeltaPerRow opcode with the specified area and row deltas
300 		/// (specified as a table of 32-bit floats).
301 
302 		dng_opcode_DeltaPerRow (const dng_area_spec &areaSpec,
303 								AutoPtr<dng_memory_block> &table);
304 
305 		dng_opcode_DeltaPerRow (dng_host &host,
306 								dng_stream &stream);
307 
308 		virtual void PutData (dng_stream &stream) const;
309 
310 		virtual uint32 BufferPixelType (uint32 imagePixelType);
311 
312 		virtual dng_rect ModifiedBounds (const dng_rect &imageBounds);
313 
314 		virtual void ProcessArea (dng_negative &negative,
315 								  uint32 threadIndex,
316 								  dng_pixel_buffer &buffer,
317 								  const dng_rect &dstArea,
318 								  const dng_rect &imageBounds);
319 
320 	};
321 
322 /*****************************************************************************/
323 
324 /// \brief An opcode to apply a delta (i.e., offset) that varies per column.
325 /// Within a column, the same delta value is applied to all specified pixels.
326 
327 class dng_opcode_DeltaPerColumn: public dng_inplace_opcode
328 	{
329 
330 	private:
331 
332 		dng_area_spec fAreaSpec;
333 
334 		AutoPtr<dng_memory_block> fTable;
335 
336 		real32 fScale;
337 
338 	public:
339 
340 		/// Create a DeltaPerColumn opcode with the specified area and column
341 		/// deltas (specified as a table of 32-bit floats).
342 
343 		dng_opcode_DeltaPerColumn (const dng_area_spec &areaSpec,
344 								   AutoPtr<dng_memory_block> &table);
345 
346 		dng_opcode_DeltaPerColumn (dng_host &host,
347 								   dng_stream &stream);
348 
349 		virtual void PutData (dng_stream &stream) const;
350 
351 		virtual uint32 BufferPixelType (uint32 imagePixelType);
352 
353 		virtual dng_rect ModifiedBounds (const dng_rect &imageBounds);
354 
355 		virtual void ProcessArea (dng_negative &negative,
356 								  uint32 threadIndex,
357 								  dng_pixel_buffer &buffer,
358 								  const dng_rect &dstArea,
359 								  const dng_rect &imageBounds);
360 
361 	};
362 
363 /*****************************************************************************/
364 
365 /// \brief An opcode to apply a scale factor that varies per row. Within a row,
366 /// the same scale factor is applied to all specified pixels.
367 
368 class dng_opcode_ScalePerRow: public dng_inplace_opcode
369 	{
370 
371 	private:
372 
373 		dng_area_spec fAreaSpec;
374 
375 		AutoPtr<dng_memory_block> fTable;
376 
377 	public:
378 
379 		/// Create a ScalePerRow opcode with the specified area and row scale
380 		/// factors (specified as a table of 32-bit floats).
381 
382 		dng_opcode_ScalePerRow (const dng_area_spec &areaSpec,
383 								AutoPtr<dng_memory_block> &table);
384 
385 		dng_opcode_ScalePerRow (dng_host &host,
386 								dng_stream &stream);
387 
388 		virtual void PutData (dng_stream &stream) const;
389 
390 		virtual uint32 BufferPixelType (uint32 imagePixelType);
391 
392 		virtual dng_rect ModifiedBounds (const dng_rect &imageBounds);
393 
394 		virtual void ProcessArea (dng_negative &negative,
395 								  uint32 threadIndex,
396 								  dng_pixel_buffer &buffer,
397 								  const dng_rect &dstArea,
398 								  const dng_rect &imageBounds);
399 
400 	};
401 
402 /*****************************************************************************/
403 
404 /// \brief An opcode to apply a scale factor that varies per column. Within a
405 /// column, the same scale factor is applied to all specified pixels.
406 
407 class dng_opcode_ScalePerColumn: public dng_inplace_opcode
408 	{
409 
410 	private:
411 
412 		dng_area_spec fAreaSpec;
413 
414 		AutoPtr<dng_memory_block> fTable;
415 
416 	public:
417 
418 		/// Create a ScalePerColumn opcode with the specified area and column
419 		/// scale factors (specified as a table of 32-bit floats).
420 
421 		dng_opcode_ScalePerColumn (const dng_area_spec &areaSpec,
422 								   AutoPtr<dng_memory_block> &table);
423 
424 		dng_opcode_ScalePerColumn (dng_host &host,
425 								   dng_stream &stream);
426 
427 		virtual void PutData (dng_stream &stream) const;
428 
429 		virtual uint32 BufferPixelType (uint32 imagePixelType);
430 
431 		virtual dng_rect ModifiedBounds (const dng_rect &imageBounds);
432 
433 		virtual void ProcessArea (dng_negative &negative,
434 								  uint32 threadIndex,
435 								  dng_pixel_buffer &buffer,
436 								  const dng_rect &dstArea,
437 								  const dng_rect &imageBounds);
438 
439 	};
440 
441 /*****************************************************************************/
442 
443 #endif
444 
445 /*****************************************************************************/
446