aboutsummaryrefslogtreecommitdiff
path: root/Gem/develop/include/Gem/Gem/Image.h
blob: bf61aef78a381d3802e9b56622966b63c0ab4502 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*-----------------------------------------------------------------
LOG
    GEM - Graphics Environment for Multimedia

    GemPixUtil.h
       - contains image functions for pix objects
       - part of GEM

    Copyright (c) 1997-1999 Mark Danks. mark@danks.org
    Copyright (c) Günther Geiger. geiger@epy.co.at
    Copyright (c) 2001-2011 IOhannes m zmölnig. forum::für::umläute. IEM. zmoelnig@iem.at
    For information on usage and redistribution, and for a DISCLAIMER OF ALL
    WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.

-----------------------------------------------------------------*/

#ifndef _INCLUDE__GEM_GEM_IMAGE_H_
#define _INCLUDE__GEM_GEM_IMAGE_H_

#include "Gem/GemGL.h"

#include <string.h>
#include <stdlib.h>

///////////////////////////////////////////////////////////////////////////////
// Color component defines
//
// These should be used to reference the various color channels
///////////////////////////////////////////////////////////////////////////////

/* RGBA */

#if GL_RGBA_GEM == GL_RGBA
const int chRed         = 0;
const int chGreen       = 1;
const int chBlue        = 2;
const int chAlpha       = 3;
#else
const int chAlpha       = 0;
const int chRed         = 1;
const int chGreen       = 2;
const int chBlue        = 3;
#endif


/* Gray */
const int chGray        = 0;

/* YUV422 */
const int chU           = 0;
const int chY0          = 1;
const int chV           = 2;
const int chY1          = 3;

/*-----------------------------------------------------------------
-------------------------------------------------------------------
CLASS
    imageStruct

    The basic image structure

-----------------------------------------------------------------*/
// we now have a class "imageStruct";
// since i need to compile some of the sources with an older version of Gem
// there is a  new define here:
#define IMAGE_CLASS

struct GEM_EXTERN imageStruct {
  imageStruct(void);
  imageStruct(const imageStruct&);
  virtual ~imageStruct(void);

  virtual void info(void);
  //////////
  // columns
  virtual unsigned char* allocate(size_t size);
  virtual unsigned char* allocate(void);

  // if we have allocated some space already, only re-allocate when needed.
  virtual unsigned char* reallocate(size_t size);
  virtual unsigned char* reallocate(void);

  // delete the buffer (if it is ours)
  virtual void clear(void);


  //////////
  // dimensions of the image
  GLint         xsize;
  GLint             ysize;

  //////////
  // (average) width of 1 pixel (LUMINANCE = 1, RGBA = 4, YUV = 2)
  GLint             csize;

  //////////
  // data type - always GL_UNSIGNED_BYTE (except for OS X)
  GLenum          type;

  //////////
  // the format - either GL_RGBA, GL_LUMINANCE
  // or GL_YCBCR_422_GEM (which is on mac-computers GL_YCBCR_422_APPLE)
  GLenum          format;

  //////////
  // is this owned by us (? what about underscores ?)
  int notowned;

  //////////
  // gets a pixel
  /* X,Y are the coordinates
   * C is the offset in the interleaved data (like chRed==0 for red)
   * you should use chRed instead of 0 (because it might not be 0)
   *
   * you must make sure that (0<=X<xsize) and (0<=Y<ysize)
   */
  // heck, why are X&Y swapped ?? (JMZ)
  inline unsigned char GetPixel(int Y, int X, int C) const
  {
    return(data[Y * xsize * csize + X * csize + C]);
  }

  //////////
  // sets a pixel
  /* while X and Y should be clear (coordinates),
   * C is the offset (like chRed==0 for red).
   * VAL is the value to set.
   *
   * you must make sure that (0<=X<xsize) and (0<=Y<ysize)
   */
  inline void SetPixel(int Y, int X, int C, unsigned char VAL)
  {
    data[Y * xsize * csize + X * csize + C] = VAL;
  }


  /////////
  // gets the color of a pixel
  virtual void getRGB(int X, int Y, unsigned char*r, unsigned char*g,
                      unsigned char*b, unsigned char*a=NULL) const;
  virtual void getGrey(int X, int Y, unsigned char*g) const;
  virtual void getYUV(int X, int Y, unsigned char*y, unsigned char*u,
                      unsigned char*v) const;

  /* following will set the whole image-data to either black or white
   * the size of the image-data is NOT xsize*ysize*csize but datasize
   * this is mostly slower
   * i have put the datasize into private (like pdata) (bad idea?)
   */
  virtual void setBlack(void);
  virtual void setWhite(void);

  /* certain formats are bound to certain csizes,
   * it's quite annoying to have to think again and again (ok, not much thinking)
   * so we just give the format (like GL_LUMINANCE)
   * and it will set the image format to this format
   * and set and return the correct csize (like 1)
   * if no format is given the current format is used
   */
  virtual int setCsizeByFormat(int format);
  virtual int setCsizeByFormat(void);


  /* various copy functions
   * sometimes we want to copy the whole image (including pixel-data),
   * but often it is enough to just copy the meta-data (without pixel-data)
   * into a new imageStruct
   */
  virtual void copy2Image(imageStruct *to) const;
  virtual void copy2ImageStruct(imageStruct *to)
  const; // copy the imageStruct (but not the actual data)
  /* this is a sort of better copy2Image,
   * which only copies the imageStruct-data if it is needed
   */
  virtual void refreshImage(imageStruct *to) const;


  /* inplace swapping Red and Blue channel */
  virtual void swapRedBlue(void);

  ///////////////////////////////////////////////////////////////////////////////
  // acquiring data including colour-transformations
  // should be accelerated if possible
  /* i wonder whether this is the right place to put these routines
   * they should be stored somewhere centrally
   * (because maybe a lot of objects would like them) (like [pix_rgba]...)
   * but it might be better to put them (the actual conversion routines) into
   * separate files (a separate library?)
   * orgdata points to the actual data in the given format
   * the datasize will be read from image.xsize, image.ysize
   * the dest-format will be given by image.format
   *   this is maybe not really clean (the meta-data is stored in the destination,
   *   while the source has no meta-data of its own)
   */
  virtual void convertTo  (imageStruct*to,   GLenum dest_format=0) const;
  virtual void convertFrom(const imageStruct*from, GLenum dest_format=0);

  virtual void fromRGB    (const unsigned char* orgdata);
  virtual void fromRGBA   (const unsigned char* orgdata);
  virtual void fromBGR    (const unsigned char* orgdata);
  virtual void fromBGRA   (const unsigned char* orgdata);
  virtual void fromRGB16  (const unsigned char* orgdata);
  virtual void fromABGR   (const unsigned char* orgdata);
  virtual void fromARGB   (const unsigned char* orgdata);
  virtual void fromGray   (const unsigned char* orgdata);
  virtual void fromGray   (short* orgdata);
  virtual void fromUYVY   (const unsigned char* orgdata);
  virtual void fromYUY2   (const unsigned char* orgdata); // YUYV
  virtual void fromYVYU   (const unsigned char* orgdata);
  /* planar YUV420: this is rather generic and not really YV12 only */
  virtual void fromYV12   (const unsigned char* Y, const unsigned char*U,
                           const unsigned char*V);
  /* assume that the planes are near each other: YVU */
  virtual void fromYV12   (const unsigned char* orgdata);
  /* assume that the planes are near each other: YVU */
  virtual void fromYU12   (const unsigned char* orgdata);
  /* overloading the above two in order to accept pdp YV12 packets */
  virtual void fromYV12   (const short* Y, const short*U, const short*V);
  virtual void fromYV12   (const short* orgdata);

  /* aliases */
  virtual void fromYUV422 (const unsigned char* orgdata)
  {
    fromUYVY(orgdata);
  }
  virtual void fromYUV420P(const unsigned char* orgdata)
  {
    fromYV12(orgdata);
  }
  virtual void fromYUV420P(const unsigned char*Y,const unsigned char*U,
                           const unsigned char*V)
  {
    fromYV12(Y,U,V);
  }

  // "data" points to the image.
  // the memory could(!) be reserved by this class or someone else
  // "notowned" should be set to "1", if "data" points to foreign memory
  // "data" is not freed directly, when the destructor is called
  unsigned char   *data;    // the pointer to the data
private:
  // "pdata" is the private data, and is the memory reserved by this class
  // this data is freed when the destructor is called
  unsigned char   *pdata;
  // "datasize" is the size of data reserved at "pdata"
  size_t    datasize;

public:
  //////////
  // true if the image is flipped horizontally (origin is upper-left)
  // false if the image is openGL-conformant (origin is lower-left)
  GLboolean       upsidedown;

  /* make the image orientation openGL-conformant */
  virtual void fixUpDown(void);

  imageStruct& operator=(const imageStruct&);
};

/*-----------------------------------------------------------------
-------------------------------------------------------------------
CLASS
    pixBlock

    The pix block structure

-----------------------------------------------------------------*/
struct GEM_EXTERN pixBlock {
  pixBlock();

  //////////
  // the block's image
  imageStruct     image;

  //////////
  // is this a newimage since last time?
  //  ie, has it been refreshed
  bool            newimage;

  //////////
  // keeps track of when new films are loaded
  //  useful for rectangle_textures on OSX
  bool                    newfilm;
};

///////////////////////////////////////////////////////////////////////////////
// imageStruct utility functions
//
///////////////////////////////////////////////////////////////////////////////
//////////
// copies all of the data over and mallocs memory
GEM_EXTERN extern void copy2Image(imageStruct *to, const imageStruct *from);

//////////
// assumes that it only has to refresh the data
GEM_EXTERN extern void refreshImage(imageStruct *to, const imageStruct *from);

GEM_EXTERN extern int getPixFormat(const char*);
#endif // GEMPIXUTIL_H_