1 /*
2  * This software is copyrighted as noted below.  It may be freely copied,
3  * modified, and redistributed, provided that the copyright notice is
4  * preserved on all copies.
5  *
6  * There is no warranty or other guarantee of fitness for this software,
7  * it is provided solely "as is".  Bug reports or fixes may be sent
8  * to the author, who may or may not act on them as he desires.
9  *
10  * You may not include this software in a program or other software product
11  * without supplying the source, or without informing the end-user that the
12  * source is available for no extra charge.
13  *
14  * If you modify this software, you should include a notice giving the
15  * name of the person performing the modification, the date of modification,
16  * and the reason for such modification.
17  */
18 /*
19  * tobw.c - inverse a cmap/component.
20  *
21  * Author:      Raul Rivero
22  *              Mathematics Dept.
23  *              University of Oviedo
24  * Date:        Tue Mar 03 1992
25  * Copyright (c) 1992, Raul Rivero
26  *
27  */
28 
29 #include <lug.h>
30 #include <lugfnts.h>
31 
32 extern int LUGverbose;
33 
toinverse(inbitmap,outbitmap)34 toinverse(inbitmap, outbitmap)
35 bitmap_hdr *inbitmap;
36 bitmap_hdr *outbitmap;
37 {
38   int totalsize;
39   int directcolor;
40 
41   if ( inbitmap->magic != LUGUSED )
42     error( 19 );
43 
44   VPRINTF(stderr, "Converting to inverse cmap\n");
45   directcolor = ( inbitmap->depth < 24 ? 0 : 1 );
46 
47   /*
48    * Fill new header ...
49    */
50   outbitmap->magic = LUGUSED;
51   outbitmap->xsize = inbitmap->xsize;
52   outbitmap->ysize = inbitmap->ysize;
53   outbitmap->depth = inbitmap->depth;
54   outbitmap->colors = ( 1 << outbitmap->depth );
55   totalsize = outbitmap->xsize * outbitmap->ysize;
56   /* One component => r */
57   outbitmap->r = (byte *) Malloc( totalsize );
58 
59   if ( !directcolor ) {
60     /*
61      * If the image has less than 24 planes then we only
62      * need convert its palette.
63      */
64     outbitmap->cmap = (byte *) cmap_to_inverse( inbitmap );
65     /* Copy the raster information */
66     bcopy( inbitmap->r, outbitmap->r, totalsize );
67   }else {
68     /*
69      * We have a real color image.
70      */
71 
72     /* Allocate space, ... */
73     outbitmap->g = (byte *) Malloc( totalsize );
74     outbitmap->b = (byte *) Malloc( totalsize );
75 
76     /* ... copy each component ... */
77     bcopy( inbitmap->r, outbitmap->r, totalsize );
78     bcopy( inbitmap->g, outbitmap->g, totalsize );
79     bcopy( inbitmap->b, outbitmap->b, totalsize );
80 
81     /* ... and inverse it. */
82     inverse( outbitmap->r, totalsize );
83     inverse( outbitmap->g, totalsize );
84     inverse( outbitmap->b, totalsize );
85   }
86 }
87 
cmap_to_inverse(image)88 byte *cmap_to_inverse( image )
89 bitmap_hdr *image;
90 {
91   byte *buffer;
92 
93   if ( image->depth > 8 )
94     error( 11 );
95 
96   buffer = (byte *) Malloc ( 3 * image->colors );
97   bcopy( image->cmap, buffer, 3 * image->colors );
98 
99   /* Convert that buffer to its inverse */
100   inverse( buffer, 3 * image->colors );
101 
102   return buffer;
103 }
104 
inverse(buffer,size)105 inverse( buffer, size )
106 byte *buffer;
107 int size;
108 {
109   while ( size-- )
110     *buffer++ = (byte) ABS( *buffer - 255 );
111 }
112