GemPixUtil.h 8.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*-----------------------------------------------------------------
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) Gnther Geiger. geiger@epy.co.at
    Copyright (c) 2001-2002 IOhannes m zmoelnig. forum::fr::umlute. IEM. zmoelnig@iem.kug.ac.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_GEMPIXUTIL_H_
#define INCLUDE_GEMPIXUTIL_H_

// I hate Microsoft...I shouldn't have to do this!
#ifdef _WINDOWS
#include <windows.h>
#endif

tigital's avatar
tigital committed
25
26
27
28
29
#ifdef MACOSX
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <QuickTime/QuickTime.h>
#else
30
#include <GL/gl.h>
tigital's avatar
tigital committed
31
#include "config.h"
32
#endif // MACOSX
33
34
35
36
37
38
39
40
41
42

// packed pixel defines for textures
#ifndef GL_EXT_packed_pixels
#define GL_UNSIGNED_BYTE_3_3_2_EXT          0x8032
#define GL_UNSIGNED_SHORT_4_4_4_4_EXT       0x8033
#define GL_UNSIGNED_SHORT_5_5_5_1_EXT       0x8034
#define GL_UNSIGNED_INT_8_8_8_8_EXT         0x8035
#define GL_UNSIGNED_INT_10_10_10_2_EXT      0x8036
#endif

zmoelnig's avatar
zmoelnig committed
43
44
45
46
47
48
49
#ifdef GL_YCBCR_422_APPLE
#define GL_YCBCR_422_GEM GL_YCBCR_422_APPLE
#else
// maybe, GL_YCBCR_422_APPLE is 0x85B9 (i really don't know exactly...)
#define GL_YCBCR_422_GEM 0x85B9
#endif

50
51
52
53
54
55
56
57
58
59
#include "Base/GemFuncUtil.h"

/*-----------------------------------------------------------------
-------------------------------------------------------------------
CLASS
    imageStruct
    
    The basic image structure

-----------------------------------------------------------------*/
zmoelnig's avatar
zmoelnig committed
60
61
62
63
64
// 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

65
66
struct GEM_EXTERN imageStruct
{
67
  imageStruct() : xsize (0), ysize(0),csize(0),notowned(0),data(0),datasize(0),pdata(0) {}
68
69

  ~imageStruct() { clear(); }
70
  void info();
71
72
  //////////
  // columns
73
74
75
76
77
78
79
80
  unsigned char* allocate(int size) {
    if (pdata) delete [] pdata;
#if 1
    pdata = new unsigned char[size];
    data = (unsigned char*) ((((unsigned int)pdata)+31)& (~31));
#else
    data = pdata =  new unsigned char [size];
#endif
81
    datasize=size;
82
83
    return data; 
  }
84
85
86
87
88
89
  // if we have allocated some space already, only re-allocate when needed.
  unsigned char* reallocate(int size) {
    if (size>datasize)
      return allocate(size);
    return data;
  }
90
91
92
93
94
95
  void clear() {
    if (!notowned && pdata) {
      delete [] pdata;
      data = pdata = 0;      
    }
    xsize = ysize = csize = 0;
96
    datasize=0;
97
  }
98
99
  
  GLint           xsize;
100

101
102
103
  //////////
  // rows
  GLint   	    ysize;
104

105
106
107
  //////////
  // color (LUMINANCE = 1, RGBA = 4)
  GLint   	    csize;
108

109
110
111
112
  //////////
  // the format - either GL_RGBA, GL_BGRA_EXT, or GL_LUMINANCE
  // or GL_YCBCR_422_GEM (which is on mac-computers GL_YCBCR_422_APPLE)
  GLenum          format;
113

114
115
116
117
118
  //////////
  // data type - always UNSIGNED_BYTE (except for OS X)
  GLenum          type;
  
  
119
  int notowned;
120
121
122
  //////////
  // the actual image data
  
123
124
125
126
127
128
129
130
131
  //////////
  // gets a pixel
  inline unsigned char GetPixel(int Y, int X, int C)
  { return(data[Y * xsize * csize + X * csize + C]); }
  
  //////////
  // sets a pixel
  inline void SetPixel(int Y, int X, int C, unsigned char VAL)
  { data[Y * xsize * csize + X * csize + C] = VAL; }
132
  
133
134
  void copy2Image(imageStruct *to);
  void refreshImage(imageStruct *to);
135
  void copy2ImageStruct(imageStruct *to); // copy the imageStruct (but not the actual data)
136
137
138
139
  
  
  unsigned char   *data;
  int             datasize;
140
  private:
141
  unsigned char   *pdata;
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
};

/*-----------------------------------------------------------------
-------------------------------------------------------------------
CLASS
    pixBlock
    
    The pix block structure

-----------------------------------------------------------------*/
struct GEM_EXTERN pixBlock
{
    //////////
    // the block's image
    imageStruct     image;

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

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

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


///////////////////////////////////////////////////////////////////////////////
// Color component defines
//
// These should be used to reference the various color channels
///////////////////////////////////////////////////////////////////////////////
tigital's avatar
tigital committed
182
#ifdef MACOSX				//tigital
183
184
185
186
const int chRed		= 1;
const int chGreen	= 2;
const int chBlue	= 3;
const int chAlpha	= 0;
tigital's avatar
tigital committed
187
#else
188
189
190
191
const int chRed		= 0;
const int chGreen	= 1;
const int chBlue	= 2;
const int chAlpha	= 3;
tigital's avatar
tigital committed
192
#endif
193
194
195
196
197
198
199
200

const int chGray	= 0;

///////////////////////////////////////////////////////////////////////////////
// Pixel access functions
//
///////////////////////////////////////////////////////////////////////////////

201
202
203
204

//
// Accelerated Pixel Manipulations
//
tigital's avatar
tigital committed
205
#ifndef MACOSX
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

int detect_mmx( void );


#if HAVE_MMX


#define MMXDONE asm volatile ("emms  \n\t")

#define MMXSTART   \
asm volatile ( \
        "push %ebx \n\t"                    \
        "mov       %esp, %ebx \n\t"    \
        "sub          $4, %ebx \n\t"  \
        "and        $-32, %ebx \n\t"  \
        "mov     %esp, (%ebx) \n\t"  \
        "mov       %ebx, %esp \n\t"  \
        "pop %ebx")

#define ADD8(a,b) \
asm volatile (\
   "movq    (%1), %%mm1 \n\t"       \
   "paddusb (%0), %%mm1 \n\t"       \
   "movq    %%mm1, (%0) \n\t"       \
   : : "r" (a), "r" (b))

#define SUB8(a,b) \
asm volatile (\
       "movq    (%0), %%mm1 \n\t"   \
       "psubusb (%1), %%mm1 \n\t"   \
       "movq    %%mm1, (%0) \n\t"   \
       : : "r" (a), "r" (b))

#define ABSDIFF8(a,b) \
asm volatile (\
       "movq    (%0), %%mm1 \n\t"   \
       "movq    (%1), %%mm2 \n\t"   \
       "psubusb (%1), %%mm1 \n\t"   \
       "psubusb (%0), %%mm2 \n\t"   \
       "por    %%mm2, %%mm1 \n\t"   \
       "movq    %%mm1, (%0) \n\t"   \
	      : : "r" (a), "r" (b))


#define MULT4_b_s(a,b) \
asm volatile (\
       "pxor      %%mm0, %%mm0 \n\t" \
       "movq    (%0), %%mm1 \n\t"    \
       "movq    (%1), %%mm2 \n\t"    \
       "punpcklbw %%mm0, %%mm1 \n\t" \
       "pmullw    %%mm2, %%mm1 \n\t" \
       "packuswb  %%mm0, %%mm1 \n\t" \
       "movq    %%mm1, (%0) \n\t"   \
       : : "r" (a), "r" (b))



#else // non MMX versions

#define MMXSTART
#define MMXDONE

#define ADD8(a,b) \
 ((unsigned char*)(a))[0] = CLAMP_HIGH((int)((unsigned char*)(a))[0] + ((unsigned char*)(b))[0]);\
 ((unsigned char*)(a))[1] = CLAMP_HIGH((int)((unsigned char*)(a))[1] + ((unsigned char*)(b))[1]);\
 ((unsigned char*)(a))[2] = CLAMP_HIGH((int)((unsigned char*)(a))[2] + ((unsigned char*)(b))[2]);\
 ((unsigned char*)(a))[3] = CLAMP_HIGH((int)((unsigned char*)(a))[3] + ((unsigned char*)(b))[3]);\
 ((unsigned char*)(a))[4] = CLAMP_HIGH((int)((unsigned char*)(a))[4] + ((unsigned char*)(b))[4]);\
 ((unsigned char*)(a))[5] = CLAMP_HIGH((int)((unsigned char*)(a))[5] + ((unsigned char*)(b))[5]);\
 ((unsigned char*)(a))[6] = CLAMP_HIGH((int)((unsigned char*)(a))[6] + ((unsigned char*)(b))[6]);\
 ((unsigned char*)(a))[7] = CLAMP_HIGH((int)((unsigned char*)(a))[7] + ((unsigned char*)(b))[7]);


#define SUB8(a,b) \
 ((unsigned char*)(a))[0] = CLAMP_LOW((int)((unsigned char*)(a))[0] - ((unsigned char*)(b))[0]);\
 ((unsigned char*)(a))[1] = CLAMP_LOW((int)((unsigned char*)(a))[1] - ((unsigned char*)(b))[1]);\
 ((unsigned char*)(a))[2] = CLAMP_LOW((int)((unsigned char*)(a))[2] - ((unsigned char*)(b))[2]);\
 ((unsigned char*)(a))[3] = CLAMP_LOW((int)((unsigned char*)(a))[3] - ((unsigned char*)(b))[3]);\
 ((unsigned char*)(a))[4] = CLAMP_LOW((int)((unsigned char*)(a))[4] - ((unsigned char*)(b))[4]);\
 ((unsigned char*)(a))[5] = CLAMP_LOW((int)((unsigned char*)(a))[5] - ((unsigned char*)(b))[5]);\
 ((unsigned char*)(a))[6] = CLAMP_LOW((int)((unsigned char*)(a))[6] - ((unsigned char*)(b))[6]);\
 ((unsigned char*)(a))[7] = CLAMP_LOW((int)((unsigned char*)(a))[7] - ((unsigned char*)(b))[7]);

#endif // MMX

tigital's avatar
tigital committed
291
#endif //MACOSX
292

293
294
295


#endif