aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/programs/Xserver/hw/nxagent/Pixels.c
blob: eb8daf8f3d04ddb95367ef8fa4d4d641ad6dd246 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**************************************************************************/
/*                                                                        */
/* Copyright (c) 2001, 2009 NoMachine, http://www.nomachine.com/.         */
/*                                                                        */
/* NXAGENT, NX protocol compression and NX extensions to this software    */
/* are copyright of NoMachine. Redistribution and use of the present      */
/* software is allowed according to terms specified in the file LICENSE   */
/* which comes in the source distribution.                                */
/*                                                                        */
/* Check http://www.nomachine.com/licensing.html for applicability.       */
/*                                                                        */
/* NX and NoMachine are trademarks of Medialogic S.p.A.                   */
/*                                                                        */
/* All rights reserved.                                                   */
/*                                                                        */
/**************************************************************************/

#include <stdio.h>
#include <stdlib.h>

#include "Xmd.h"
#include "Xlib.h"

#define PANIC
#define WARNING
#undef  TEST
#undef  DEBUG

#define PIXEL_ELEMENTS  256
#define PIXEL_THRESHOLD 8
#define PIXEL_STEP      7

unsigned int Get16(const char *buffer, int order);
unsigned int Get24(const char *buffer, int order);
unsigned int Get32(const char *buffer, int order);

void Put16(unsigned int value, char *buffer, int order);
void Put24(unsigned int value, char *buffer, int order);
void Put32(unsigned int value, char *buffer, int order);

static int nxagentComparePixels(const void *p1, const void *p2)
{
  int pixel1 = *((int *) p1);
  int pixel2 = *((int *) p2);

  return (pixel1 < pixel2 ? -1 : (pixel1 == pixel2 ? 0 : 1));
}

int nxagentUniquePixels(XImage *image)
{
  int i = 0;

  int pixels[PIXEL_ELEMENTS];

  int elements = PIXEL_ELEMENTS;
  int unique   = 0;

  int total;
  int ratio;
  int step;

  int last = -1;

  const char *next = image -> data;

  #ifdef TEST
  fprintf(stderr, "nxagentUniquePixels: Image geometry [%d,%d] depth [%d] bits per pixel [%d].\n",
              image -> width, image -> height, image -> depth, image -> bits_per_pixel);
  #endif

  /*
   * Take at most 256 pixels from the image.
   */

  total = image -> width * image -> height;
  
  step = total / elements;

  if (step < PIXEL_STEP)
  {
    step = PIXEL_STEP;
  }

  #ifdef TEST
  fprintf(stderr, "nxagentUniquePixels: Step is [%d] with [%d] pixels and [%d] elements.\n",
              step, total, elements);
  #endif

  /*
   * Shift at the left after each scanline.
   */
   
  if (image -> bytes_per_line % step == 0)
  {
    step++;

    #ifdef TEST
    fprintf(stderr, "nxagentUniquePixels: Increasing step to [%d] with [%d] bytes per line.\n",
                step, image -> bytes_per_line);
    #endif
  }

  elements = total / step;

  if (elements > PIXEL_ELEMENTS)
  {
    elements = PIXEL_ELEMENTS;
  }

  #ifdef TEST
  fprintf(stderr, "nxagentUniquePixels: Step is now [%d] with [%d] elements.\n",
              step, elements);
  #endif

  if (elements < PIXEL_THRESHOLD)
  {
    #ifdef TEST
    fprintf(stderr, "nxagentUniquePixels: Assuming ratio [100] with only [%d] elements.\n",
                elements);
    #endif

    return 100;
  }

  #ifdef TEST
  fprintf(stderr, "nxagentUniquePixels: Scanning [%d] pixels out of [%d] with step [%d].\n",
              elements, total, step);
  #endif

  /*
   * Take one pixel every n from the image and
   * add it to the array.
   */

  switch (image -> bits_per_pixel)
  {
    case 32:
    {
      for (i = 0; i < elements; i++)
      {
        pixels[i] = Get32(next, image -> byte_order);

        next += (4 * step);

        #ifdef DEBUG
        fprintf(stderr, "nxagentUniquePixels: pixels[%d][0x%08x].\n",
                    i, pixels[i]);
        #endif
      }

      break;
    }
    case 24:
    {
      for (i = 0; i < elements; i++)
      {
        pixels[i] = Get24(next, image -> byte_order);

        next += (3 * step);

        #ifdef DEBUG
        fprintf(stderr, "nxagentUniquePixels: pixels[%d][0x%08x].\n",
                    i, pixels[i]);
        #endif
      }

      break;
    }
    case 16:
    case 15:
    {
      /*
       * Note that the padding bytes at the end
       * of the scanline are included in the set.
       * This is not a big problem. What we want
       * to find out is just how compressible is
       * the image data.
       */

      for (i = 0; i < elements; i++)
      {
        pixels[i] = Get16(next, image -> byte_order);

        next += (2 * step);

        #ifdef DEBUG
        fprintf(stderr, "nxagentUniquePixels: pixels[%d][0x%08x].\n",
                    i, pixels[i]);
        #endif
      }

      break;
    }
    default:
    {
      #ifdef PANIC
      fprintf(stderr, "nxagentUniquePixels: PANIC! Assuming ratio [100] with [%d] bits per pixel.\n",
                  image -> bits_per_pixel);
      #endif

      return 100;
    }
  }

  #ifdef TEST
  fprintf(stderr, "nxagentUniquePixels: Sorting [%d] elements in the list.\n", i);
  #endif

  qsort(pixels, elements, sizeof(int), nxagentComparePixels);

  for (i = 0; i < elements; i++)
  {
    if (last != pixels[i])
    {
      unique++;

      last = pixels[i];
    }

    #ifdef DEBUG
    fprintf(stderr, "nxagentUniquePixels: pixels[%d][0x%08x].\n",
                i, pixels[i]);
    #endif
  }

  ratio = unique * 100 / elements;

  #ifdef TEST
  fprintf(stderr, "nxagentUniquePixels: Found [%d] unique pixels out of [%d] with ratio [%d%%].\n",
              unique, elements, ratio);
  #endif

  return ratio;
}

unsigned int Get16(const char *buffer, int order)
{
  unsigned int result;

  if (order == MSBFirst)
  {
    result = *buffer;

    result <<= 8;

    result += buffer[1];
  }
  else
  {
    result = buffer[1];

    result <<= 8;

    result += *buffer;
  }

  return result;
}

unsigned int Get24(const char *buffer, int order)
{
  int i;

  const char *next = (order == MSBFirst ? buffer : buffer + 2);

  unsigned int result = 0;

  for (i = 0; i < 3; i++)
  {
    result <<= 8;

    result += *next;

    if (order == MSBFirst)
    {
      next++;
    }
    else
    {
      next--;
    }
  }

  return result;
}

unsigned int Get32(const char *buffer, int order)
{
  int i;

  const char *next = (order == MSBFirst ? buffer : buffer + 3);

  unsigned int result = 0;

  for (i = 0; i < 4; i++)
  {
    result <<= 8;

    result += *next;

    if (order == MSBFirst)
    {
      next++;
    }
    else
    {
      next--;
    }
  }

  return result;
}

void Put16(unsigned int value, char *buffer, int order)
{
  if (order == MSBFirst)
  {
    buffer[1] = (unsigned char) (value & 0xff);

    value >>= 8;

    *buffer = (unsigned char) value;
  }
  else
  {
    *buffer = (unsigned char) (value & 0xff);

    value >>= 8;

    buffer[1] = (unsigned char) value;
  }
}

void Put24(unsigned int value, char *buffer, int order)
{
  int i;

  if (order == MSBFirst)
  {
    buffer += 2;

    for (i = 3; i > 0; i--)
    {
      *buffer-- = (unsigned char) (value & 0xff);

      value >>= 8;
    }
  }
  else
  {
    for (i = 3; i > 0; i--)
    {
      *buffer++ = (unsigned char) (value & 0xff);

      value >>= 8;
    }
  }
}

void Put32(unsigned int value, char *buffer, int order)
{
  int i;

  if (order == MSBFirst)
  {
    buffer += 3;

    for (i = 4; i > 0; i--)
    {
      *buffer-- = (unsigned char) (value & 0xff);

      value >>= 8;
    }
  }
  else
  {
    for (i = 4; i > 0; i--)
    {
      *buffer++ = (unsigned char) (value & 0xff);

      value >>= 8;
    }
  }
}