summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/AVStreams/mpeg/source/mpeg_client/mono.cpp
blob: 9549a90950b1a155ba047347244a6bf98e74a2cf (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
/* $Id$ */

/*
 * Author:	Yoichiro Ueno (ueno@cs.titech.ac.jp)
 *
 * Copyright (C) 1991, 1992, Yoichiro Ueno.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose is hereby granted by the Author without
 * fee, provided that the above copyright notice appear in all copies and
 * that both the copyright notice and this permission notice appear in
 * supporting documentation, and that the name of the Author not be used
 * in advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The Author makes no
 * representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 *
 * THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include "ace/OS.h"
#include <string.h>
#include <stdlib.h>
#include "video.h"
#include "proto.h"
#include "dither.h"

ACE_RCSID(mpeg_client, mono, "$Id$")

/*
 *--------------------------------------------------------------
 *
 * MonoDitherImage --
 *
 *	Dithers image into monochrome.
 *	Dither algorithm is based on dither.c in xli.1.11.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	None.
 *
 *--------------------------------------------------------------
 */
#define MaxGrey		65280
#define Threshold	(MaxGrey/2)
#define MinGrey		0

#if ultrix && mips
#	define SEED_BIT 0x01
#	define OPP_SEED_BIT 0x80
#	define SHIFT_SEED_BIT(b) (b <<= 1)
#	define OPP_SHIFT_SEED_BIT(b) (b >>= 1)
#else
#	define SEED_BIT 0x80
#	define OPP_SEED_BIT 0x01
#	define SHIFT_SEED_BIT(b) (b >>= 1)
#	define OPP_SHIFT_SEED_BIT(b) (b <<= 1)
#endif

static int	*curr = NULL;
static int	*next = NULL;

#if 0
void
MonoDitherImage(register unsigned char *lum, unsigned char *cr,
		unsigned char *cb, unsigned char *out,
		int h, int w)
{
  int bit_r2l;
  register unsigned int bit;
  register unsigned int data;
  int i;
  register int j;
  int *swap;
  register int out_err;
  register int next1;
  register int next2;

  if(curr == NULL) {
    curr = (int *)ACE_OS::malloc(sizeof(int) * (w + 2));
    curr += 1;
  }
  if(next == NULL) {
    next = (int *)ACE_OS::malloc(sizeof(int) * (w + 2));
    next += 1;
  }

  bzero ((char *)curr, w * sizeof(*curr));

  bit_r2l = SEED_BIT << (w - 1 & 7);
  for(i = 0; i < h; i ++) {
    if(i & 0x01) {				/* Right to Left */
      bit = bit_r2l;
      data = 0;
      out_err = curr[w-1];
      next1 = 0;
      next2 = 0;
      for (j=(w-1); j>=0; j--)
      {
	out_err = (out_err >> 4) + (lum[j] << 8);
	if(out_err > Threshold) {
	  data |= bit;
	  out_err -= MaxGrey;
	}
	else
	  out_err -= MinGrey;

	next[j+1] = next1 +     (out_err * 3);
	next1     = next2 +     (out_err * 5);
	next2     =             (out_err * 1);
	out_err   = curr[j-1] + (out_err * 7);

	OPP_SHIFT_SEED_BIT(bit);
#if ultrix && mips
	if(bit == 0)
#else
	if(bit > 0x80)
#endif
	{
	  out[j >> 3] = data;
	  bit = OPP_SEED_BIT;
	  data = 0;
	}
      }
      next[0] = next1;
    }
    else {					/* Left to Right */
      bit = SEED_BIT;
      data = 0;
      out_err = curr[0];
      next1 = 0;
      next2 = 0;
      for (j=0; j<w; j++)
      {
	out_err = (out_err >> 4) + (lum[j] << 8);
	if(out_err > Threshold) {
	  data |= bit;
	  out_err = out_err - MaxGrey;
	}
	else
	  out_err = out_err - MinGrey;

	next[j-1] = next1 +     (out_err * 3);
	next1     = next2 +     (out_err * 5);
	next2     =             (out_err * 1);
	out_err   = curr[j+1] + (out_err * 7);

	SHIFT_SEED_BIT(bit);
#if ultrix && mips
	if(bit > 0x80)
#else
	if(bit == 0)
#endif
	{
	  out[j >> 3] = data;
	  bit = SEED_BIT;
	  data = 0;
	}
      }
      next[w-1] = next1;
    }
    
    lum += w;
    out += w >> 3;
    swap = curr;
    curr = next;
    next = swap;
  }
}
#endif    

void
MonoDitherImage(register unsigned char *lum, unsigned char *cr,
		unsigned char *cb, unsigned char *out,
		int h, int w)
{
  register unsigned int bit;
  register unsigned int data;
  register int j;
  int i;
  static unsigned char d[4][4] = {{0, 8, 2, 10},
			  {12, 4, 14, 6},
			  {3, 11, 1, 9},
			  {15, 7, 13, 5}};
  
  for(i = 0; i < h; i ++) {
    bit = SEED_BIT;
    data = 0;
    for (j=0; j<w; j++) {
      if((lum[j] >> 4) > d[i % 4][j % 4]) {
	data |= bit;
      }
      SHIFT_SEED_BIT(bit);
#if ultrix && mips
      if(bit > 0x80)
#else
      if(bit == 0)
#endif
      {
	out[j >> 3] = data;
	bit = SEED_BIT;
	data = 0;
      }
    }
    lum += w;
    out += (w + 7) >> 3;
  }
}


/*
 *--------------------------------------------------------------
 *
 * MonoThresholdImage --
 *
 *	convert image into monochrome with threshold.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	None.
 *
 *--------------------------------------------------------------
 */
void
MonoThresholdImage(unsigned char *lum, unsigned char *cr,
		   unsigned char *cb, unsigned char *out,
		   int h, int w)
{
  unsigned char	bit;
  unsigned char	data;

  bit = SEED_BIT;
  data = 0;
  for (w*=h; w>0; w--) {
    if(*lum++>128)
      data |= bit;

    SHIFT_SEED_BIT(bit);
    if(bit == 0) {
      *out ++ = data;
      bit = SEED_BIT;
      data = 0;
    }
  }
}