File: undither.c

package info (click to toggle)
xloadimage 4.1-25
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,820 kB
  • sloc: ansic: 36,084; asm: 284; makefile: 282; sh: 280
file content (70 lines) | stat: -rw-r--r-- 2,128 bytes parent folder | download | duplicates (10)
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
/* undither.c:
 *
 * this converts a bitmap image into a greyscale image.
 *
 * Eckhard R"uggeberg (erueg@cfgauss.uni-math.gwdg.de).
 */

#include "image.h"

Image *undither(oimage, verbose)
     Image *oimage;
     unsigned int  verbose;
{
  Image *nimage;			/* new image to build		*/
  byte *optr, *nptr;		/* Ptr into old/new data	*/
  int i, j, k;			/* Loop counters		*/ 
  int hi, lo;			/* 2 new Pixels constructed	*/
  register int scanned;		/* byte scanned in old data	*/
  int newX, newY;			/* new sizes			*/
  int bitskip, byteskip;		/* skips for ptr in old data	*/
  int scantimes;			/* how often to scan a byte	*/
  int oddness;			/* If newX is odd		*/

  if (!BITMAPP(oimage))
    return(oimage);
  if (verbose) {
    printf ( "  Undithering image into grayscale..." );
    fflush(stdout);
  }
  newX = (oimage->width  / 4);
  oddness = newX & 1; 
  newY = oimage->height / 4;
  nimage = newRGBImage ( newX, newY, 8 );
  if (oimage->title) {
    nimage->title = (char *) lmalloc ( strlen (oimage->title) + 13 );
    sprintf ( nimage->title, "%s (grayscaled)", oimage->title );
  }
  nimage->rgb.used = 17;
  for ( i = 0; i < 17; i++ )
    *(nimage->rgb.red + i) = *(nimage->rgb.green + i) = 
      *(nimage->rgb.blue + i) = 65535.0 * (16 - i) / 16.0;
  bitskip   = (oimage->width / 8) + (oimage->width % 8 ? 1 : 0);
  byteskip  = (bitskip << 2);
  scantimes = (newX >> 1) + oddness; 
  optr = oimage->data;
  nptr = nimage->data;
  for ( i = 0; i < newY; i++ ) {		/* for each new row	*/
    for ( j = 0; j < scantimes; j++ ) {	/* for each new colunm	*/
      lo = hi = 0;			
      for ( k = 0; k < 4; k++ ) {	/* for each of the 4	*/
	scanned = *(optr + j + k*bitskip);	/*     old rows */
	if (scanned & 0x80) hi++;
	if (scanned & 0x40) hi++;
	if (scanned & 0x20) hi++;
	if (scanned & 0x10) hi++;
	if (scanned & 0x08) lo++;
	if (scanned & 0x04) lo++;
	if (scanned & 0x02) lo++;
	if (scanned & 0x01) lo++;
      }
      *nptr++ = hi;			/* store new data	*/
      *nptr++ = lo;
    }
    if (oddness) nptr--;		/* correct at end of row*/
    optr += byteskip;
  }
  if (verbose)
    printf("done\n");
  return(nimage);
}