summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/util/render/dither/ditherWrapper.cpp
blob: c6c37a79d72611f3e47962a8cd327a957b57e686 (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
/*
  wrapper for X11 Window
  Copyright (C) 1999  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */


#include "ditherWrapper.h"

#include <iostream>

using namespace std;


/* 
   Flag for gamma correction 
   Makes images brighter/darker. 
   It's in the source but not activated (for now)
*/
int gammaCorrectFlag = 0;
double gammaCorrect = 1.0;

/* 
   Flag for chroma correction.
   reduce the color intensity..
   It's in the source but not activated (for now)
*/
int chromaCorrectFlag = 0;
double chromaCorrect = 1.0;



DitherWrapper::DitherWrapper(int bpp,unsigned int redMask,
			     unsigned int greenMask,unsigned int blueMask,
			     unsigned char pixel[256]) {

  this->bpp=bpp;
  this->redMask=redMask;
  this->greenMask=greenMask;
  this->blueMask=blueMask;


  dither8Bit=new Dither8Bit(pixel);
  dither16Bit=new Dither16Bit(redMask,greenMask,blueMask);
  dither32Bit=new Dither32Bit(redMask,greenMask,blueMask);
  ditherRGB_flipped=new DitherRGB_flipped();
  ditherRGB=new DitherRGB();
 
  
#ifdef INTEL
  lmmx=mm_support();
#else
  lmmx=false;
#endif


}


DitherWrapper::~DitherWrapper(){
  delete dither16Bit;
  delete dither8Bit;
  delete dither32Bit;
  delete ditherRGB_flipped;
  delete ditherRGB;
}





void  DitherWrapper::doDither(YUVPicture* pic,int depth,int imageMode,
			      unsigned char* dest,int offset) {


  //
  // according to the input imageType and the output area
  // handle different dither methods
  //

  int inputType=pic->getImageType();

  if ( (inputType == PICTURE_YUVMODE_CR_CB) ||
       (inputType == PICTURE_YUVMODE_CB_CR) ) {
    doDitherYUV(pic,depth,imageMode,dest,offset);
    return;
  }

  if ( (inputType == PICTURE_RGB) ||
       (inputType == PICTURE_RGB_FLIPPED) ){
    doDitherRGB(pic,depth,imageMode,dest,offset);
    return;
  }
  
  cout << "unknown inputType:"<<inputType
       << " in DitherWrapper::doDither"<<endl;
}


void DitherWrapper::doDitherRGB(YUVPicture* pic,int depth,int imageMode,
				unsigned char* dest,int offset) {
  
  int inputType=pic->getImageType();

  switch(inputType) {
  case PICTURE_RGB:
    doDitherRGB_NORMAL(pic,depth,imageMode,dest,offset);
    break;
  case  PICTURE_RGB_FLIPPED:
    doDitherRGB_FLIPPED(pic,depth,imageMode,dest,offset);
    break;
  default:
    cout << "unknown RGB type:"<<inputType<<" in DitherWrapper"<<endl;
    exit(0);
  }
}


void DitherWrapper::doDitherRGB_NORMAL(YUVPicture* pic,
				       int depth,int imageMode,
				       unsigned char* dest,int offset) {
 
  int w=pic->getWidth();
  int h=pic->getHeight();

  unsigned char* src=pic->getImagePtr();

  if (imageMode & _IMAGE_DOUBLE) {
    ditherRGB->ditherRGBImage_x2(dest,src,depth,w,h,offset);
  } else {
    ditherRGB->ditherRGBImage(dest,src,depth,w,h,offset);
  }
}

void DitherWrapper::doDitherRGB_FLIPPED(YUVPicture* pic,
					int depth,int imageMode,
					unsigned char* dest,int offset) {
 
  int w=pic->getWidth();
  int h=pic->getHeight();

  unsigned char* src=pic->getImagePtr();

  ditherRGB_flipped->flipRGBImage(dest,src,depth,w,h,offset);
}



void DitherWrapper::doDitherYUV(YUVPicture* pic,int depth,int imageMode,
				 unsigned char* dest,int offset) {

  if (imageMode & _IMAGE_DOUBLE) {
    doDither_x2(pic,depth,dest,offset);
  } else {
    doDither_std(pic,depth,dest,offset);
  }
}


void DitherWrapper::doDither_std(YUVPicture* pic,int depth,
				 unsigned char* dest,int offset){
  
  int h=pic->getHeight();
  int w=pic->getWidth();
  unsigned char* lum=pic->getLuminancePtr();
  unsigned char* cr=pic->getCrPtr();
  unsigned char* cb=pic->getCbPtr();


  switch (depth) {
  case 8:
    dither8Bit->ditherImageOrdered(lum, cr, cb,dest , h, w);
    break;
  case 16:
    if (lmmx) {
      ditherBlock(lum,cr,cb,dest,h,w,offset);
    } else {
      dither16Bit->ditherImageColor16(lum,cr,cb,dest,h,w,offset);
    }

    break;
  case 24:
  case 32:
    if (lmmx) {
      dither32_mmx(lum, cr, cb,dest ,h,w,offset);
    } else {
      dither32Bit->ditherImageColor32(lum, cr, cb,dest ,h,w,offset);
    }


    break;
  default:
    cout << "cannot dither depth:"<<depth<<endl;
  }

}
  

void DitherWrapper::doDither_x2(YUVPicture* pic,int depth,
				unsigned char* dest,int offset){

  int h=pic->getHeight();
  int w=pic->getWidth();
  unsigned char* lum=pic->getLuminancePtr();
  unsigned char* cr=pic->getCrPtr();
  unsigned char* cb=pic->getCbPtr();


  switch (depth) {
  case 8: {
    // we do dither with the 8Bit std YUV ditherer to RGB
    // and then we do the double part with the
    // RGB ditherer. Its obviously much slower but at
    // least it works. To not allocate memory twice
    // we are a bit tricky. We know that the image 
    // has space for doubls size. We but the not double size
    // image at the bottom of the dest. Maybe that
    // the last line gets overwritten
    int memPos=3*h*w;
    dither8Bit->ditherImageOrdered(lum, cr, cb,dest+memPos, h, w);
    unsigned char* src=dest+memPos;
    ditherRGB->ditherRGBImage_x2(dest,src,depth,w,h,0);
    break;
  }
  case 16:
    dither16Bit->ditherImageTwox2Color16(lum,cr,cb,dest,h,w,offset);
    break;
  case 24:
  case 32:
    if (lmmx) {
      //dither32x2_mmx(lum, cr, cb,dest ,h,w,offset);
      dither32Bit->ditherImageTwox2Color32(lum,cr,cb,dest,h,w,offset);
    } else {
      dither32Bit->ditherImageTwox2Color32(lum,cr,cb,dest,h,w,offset);
    }
    break;
  default:
    cout << "cannot dither depth:" << depth << endl;
  }
}