1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
22 #include "DNA_vec_types.h" /* for rcti */
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /** \file
29  * \ingroup imbuf
30  * \brief Contains defines and structs used throughout the imbuf module.
31  * \todo Clean up includes.
32  *
33  * Types needed for using the image buffer.
34  *
35  * Imbuf is external code, slightly adapted to live in the Blender
36  * context. It requires an external jpeg module, and the avi-module
37  * (also external code) in order to function correctly.
38  *
39  * This file contains types and some constants that go with them. Most
40  * are self-explanatory (e.g. IS_amiga tests whether the buffer
41  * contains an Amiga-format file).
42  */
43 
44 #define IMB_MIPMAP_LEVELS 20
45 #define IMB_FILENAME_SIZE 1024
46 
47 typedef struct DDSData {
48   /** DDS fourcc info */
49   unsigned int fourcc;
50   /** The number of mipmaps in the dds file */
51   unsigned int nummipmaps;
52   /** The compressed image data */
53   unsigned char *data;
54   /** The size of the compressed data */
55   unsigned int size;
56 } DDSData;
57 
58 /**
59  * \ingroup imbuf
60  * This is the abstraction of an image.  ImBuf is the basic type used for all
61  * imbuf operations.
62  *
63  * Also; add new variables to the end to save pain!
64  */
65 
66 /* Warning: Keep explicit value assignments here,
67  * this file is included in areas where not all format defines are set
68  * (e.g. intern/dds only get WITH_DDS, even if TIFF, HDR etc are also defined).
69  * See T46524. */
70 
71 /** #ImBuf.ftype flag, main image types. */
72 enum eImbTypes {
73   IMB_FTYPE_PNG = 1,
74   IMB_FTYPE_TGA = 2,
75   IMB_FTYPE_JPG = 3,
76   IMB_FTYPE_BMP = 4,
77   IMB_FTYPE_OPENEXR = 5,
78   IMB_FTYPE_IMAGIC = 6,
79 #ifdef WITH_OPENIMAGEIO
80   IMB_FTYPE_PSD = 7,
81 #endif
82 #ifdef WITH_OPENJPEG
83   IMB_FTYPE_JP2 = 8,
84 #endif
85 #ifdef WITH_HDR
86   IMB_FTYPE_RADHDR = 9,
87 #endif
88 #ifdef WITH_TIFF
89   IMB_FTYPE_TIF = 10,
90 #endif
91 #ifdef WITH_CINEON
92   IMB_FTYPE_CINEON = 11,
93   IMB_FTYPE_DPX = 12,
94 #endif
95 
96 #ifdef WITH_DDS
97   IMB_FTYPE_DDS = 13,
98 #endif
99 };
100 
101 /* ibuf->foptions flag, type specific options.
102  * Some formats include compression rations on some bits */
103 
104 #define OPENEXR_HALF (1 << 8)
105 /* careful changing this, it's used in DNA as well */
106 #define OPENEXR_COMPRESS (15)
107 
108 #ifdef WITH_CINEON
109 #  define CINEON_LOG (1 << 8)
110 #  define CINEON_16BIT (1 << 7)
111 #  define CINEON_12BIT (1 << 6)
112 #  define CINEON_10BIT (1 << 5)
113 #endif
114 
115 #ifdef WITH_OPENJPEG
116 #  define JP2_12BIT (1 << 9)
117 #  define JP2_16BIT (1 << 8)
118 #  define JP2_YCC (1 << 7)
119 #  define JP2_CINE (1 << 6)
120 #  define JP2_CINE_48FPS (1 << 5)
121 #  define JP2_JP2 (1 << 4)
122 #  define JP2_J2K (1 << 3)
123 #endif
124 
125 #define PNG_16BIT (1 << 10)
126 
127 #define RAWTGA 1
128 
129 #ifdef WITH_TIFF
130 #  define TIF_16BIT (1 << 8)
131 #  define TIF_COMPRESS_NONE (1 << 7)
132 #  define TIF_COMPRESS_DEFLATE (1 << 6)
133 #  define TIF_COMPRESS_LZW (1 << 5)
134 #  define TIF_COMPRESS_PACKBITS (1 << 4)
135 #endif
136 
137 typedef struct ImbFormatOptions {
138   short flag;
139   /** quality serves dual purpose as quality number for jpeg or compression amount for png */
140   char quality;
141 } ImbFormatOptions;
142 
143 /**
144  * \name Imbuf Component flags
145  * \brief These flags determine the components of an ImBuf struct.
146  *
147  * \{ */
148 
149 typedef enum eImBufFlags {
150   IB_rect = 1 << 0,
151   IB_test = 1 << 1,
152   IB_zbuf = 1 << 3,
153   IB_mem = 1 << 4,
154   IB_rectfloat = 1 << 5,
155   IB_zbuffloat = 1 << 6,
156   IB_multilayer = 1 << 7,
157   IB_metadata = 1 << 8,
158   IB_animdeinterlace = 1 << 9,
159   IB_tiles = 1 << 10,
160   IB_tilecache = 1 << 11,
161   /** indicates whether image on disk have premul alpha */
162   IB_alphamode_premul = 1 << 12,
163   /** if this flag is set, alpha mode would be guessed from file */
164   IB_alphamode_detect = 1 << 13,
165   /* alpha channel is unrelated to RGB and should not affect it */
166   IB_alphamode_channel_packed = 1 << 14,
167   /** ignore alpha on load and substitute it with 1.0f */
168   IB_alphamode_ignore = 1 << 15,
169   IB_thumbnail = 1 << 16,
170   IB_multiview = 1 << 17,
171   IB_halffloat = 1 << 18,
172 } eImBufFlags;
173 
174 /** \} */
175 typedef struct ImBuf {
176   struct ImBuf *next, *prev; /**< allow lists of ImBufs, for caches or flipbooks */
177 
178   /* dimensions */
179   /** Width and Height of our image buffer.
180    * Should be 'unsigned int' since most formats use this.
181    * but this is problematic with texture math in imagetexture.c
182    * avoid problems and use int. - campbell */
183   int x, y;
184 
185   /** Active amount of bits/bitplanes */
186   unsigned char planes;
187   /** Number of channels in `rect_float` (0 = 4 channel default) */
188   int channels;
189 
190   /* flags */
191   /** Controls which components should exist. */
192   int flags;
193   /** what is malloced internal, and can be freed */
194   int mall;
195 
196   /* pixels */
197 
198   /** Image pixel buffer (8bit representation):
199    * - color space defaults to `sRGB`.
200    * - alpha defaults to 'straight'.
201    */
202   unsigned int *rect;
203   /** Image pixel buffer (float representation):
204    * - color space defaults to 'linear' (`rec709`).
205    * - alpha defaults to 'premul'.
206    * \note May need gamma correction to `sRGB` when generating 8bit representations.
207    * \note Formats that support higher more than 8 but channels load as floats.
208    */
209   float *rect_float;
210 
211   /** Resolution in pixels per meter. Multiply by `0.0254` for DPI. */
212   double ppm[2];
213 
214   /* tiled pixel storage */
215   int tilex, tiley;
216   int xtiles, ytiles;
217   unsigned int **tiles;
218 
219   /* zbuffer */
220   /** z buffer data, original zbuffer */
221   int *zbuf;
222   /** z buffer data, camera coordinates */
223   float *zbuf_float;
224 
225   /* parameters used by conversion between byte and float */
226   /** random dither value, for conversion from float -> byte rect */
227   float dither;
228 
229   /* mipmapping */
230   /** MipMap levels, a series of halved images */
231   struct ImBuf *mipmap[IMB_MIPMAP_LEVELS];
232   int miptot, miplevel;
233 
234   /* externally used data */
235   /** reference index for ImBuf lists */
236   int index;
237   /** used to set imbuf to dirty and other stuff */
238   int userflags;
239   /** image metadata */
240   struct IDProperty *metadata;
241   /** temporary storage */
242   void *userdata;
243 
244   /* file information */
245   /** file type we are going to save as */
246   enum eImbTypes ftype;
247   /** file format specific flags */
248   ImbFormatOptions foptions;
249   /** filename associated with this image */
250   char name[IMB_FILENAME_SIZE];
251   /** full filename used for reading from cache */
252   char cachename[IMB_FILENAME_SIZE];
253 
254   /* memory cache limiter */
255   /** handle for cache limiter */
256   struct MEM_CacheLimiterHandle_s *c_handle;
257   /** reference counter for multiple users */
258   int refcounter;
259 
260   /* some parameters to pass along for packing images */
261   /** Compressed image only used with png and exr currently */
262   unsigned char *encodedbuffer;
263   /** Size of data written to encodedbuffer */
264   unsigned int encodedsize;
265   /** Size of encodedbuffer */
266   unsigned int encodedbuffersize;
267 
268   /* color management */
269   /** color space of byte buffer */
270   struct ColorSpace *rect_colorspace;
271   /** color space of float buffer, used by sequencer only */
272   struct ColorSpace *float_colorspace;
273   /** array of per-display display buffers dirty flags */
274   unsigned int *display_buffer_flags;
275   /** cache used by color management */
276   struct ColormanageCache *colormanage_cache;
277   int colormanage_flag;
278   rcti invalid_rect;
279 
280   /* information for compressed textures */
281   struct DDSData dds_data;
282 } ImBuf;
283 
284 /**
285  * \brief userflags: Flags used internally by blender for imagebuffers
286  */
287 
288 enum {
289   /** image needs to be saved is not the same as filename */
290   IB_BITMAPDIRTY = (1 << 1),
291   /** image mipmaps are invalid, need recreate */
292   IB_MIPMAP_INVALID = (1 << 2),
293   /** float buffer changed, needs recreation of byte rect */
294   IB_RECT_INVALID = (1 << 3),
295   /** either float or byte buffer changed, need to re-calculate display buffers */
296   IB_DISPLAY_BUFFER_INVALID = (1 << 4),
297   /** image buffer is persistent in the memory and should never be removed from the cache */
298   IB_PERSISTENT = (1 << 5),
299 };
300 
301 /**
302  * \name Imbuf preset profile tags
303  * \brief Some predefined color space profiles that 8 bit imbufs can represent
304  *
305  * \{ */
306 #define IB_PROFILE_NONE 0
307 #define IB_PROFILE_LINEAR_RGB 1
308 #define IB_PROFILE_SRGB 2
309 #define IB_PROFILE_CUSTOM 3
310 
311 /** \} */
312 
313 /* dds */
314 #ifdef WITH_DDS
315 #  ifndef DDS_MAKEFOURCC
316 #    define DDS_MAKEFOURCC(ch0, ch1, ch2, ch3) \
317       ((unsigned long)(unsigned char)(ch0) | ((unsigned long)(unsigned char)(ch1) << 8) | \
318        ((unsigned long)(unsigned char)(ch2) << 16) | ((unsigned long)(unsigned char)(ch3) << 24))
319 #  endif /* DDS_MAKEFOURCC */
320 
321 /*
322  * FOURCC codes for DX compressed-texture pixel formats
323  */
324 
325 #  define FOURCC_DDS (DDS_MAKEFOURCC('D', 'D', 'S', ' '))
326 #  define FOURCC_DXT1 (DDS_MAKEFOURCC('D', 'X', 'T', '1'))
327 #  define FOURCC_DXT2 (DDS_MAKEFOURCC('D', 'X', 'T', '2'))
328 #  define FOURCC_DXT3 (DDS_MAKEFOURCC('D', 'X', 'T', '3'))
329 #  define FOURCC_DXT4 (DDS_MAKEFOURCC('D', 'X', 'T', '4'))
330 #  define FOURCC_DXT5 (DDS_MAKEFOURCC('D', 'X', 'T', '5'))
331 
332 #endif /* DDS */
333 extern const char *imb_ext_image[];
334 extern const char *imb_ext_movie[];
335 extern const char *imb_ext_audio[];
336 
337 /* image formats that can only be loaded via filepath */
338 extern const char *imb_ext_image_filepath_only[];
339 
340 /**
341  * \name Imbuf Color Management Flag
342  * \brief Used with #ImBuf.colormanage_flag
343  *
344  * \{ */
345 
346 enum {
347   IMB_COLORMANAGE_IS_DATA = (1 << 0),
348 };
349 
350 /** \} */
351 
352 #ifdef __cplusplus
353 }
354 #endif
355