/[rdesktop]/sourceforge.net/trunk/rdesktop/bitmap.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /sourceforge.net/trunk/rdesktop/bitmap.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

sourceforge.net/branches/RDESKTOP/rdesktop/bitmap.c revision 3 by matty, Wed May 10 07:36:34 2000 UTC sourceforge.net/trunk/rdesktop/bitmap.c revision 28 by matty, Wed Jun 20 13:54:48 2001 UTC
# Line 18  Line 18 
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */  */
20    
21  #include "includes.h"  #include "rdesktop.h"
 #include <fcntl.h>  
22    
23  #define BITMAP_DEBUG 1  #define CVAL(p)   (*(p++))
24    #define SVAL(p)   ((*((p++) + 1) << 8) | CVAL(p))
25    
26  #if BITMAP_DEBUG  #define UNROLL8(exp) { exp exp exp exp exp exp exp exp }
 void hexdump(char *filename, unsigned char *data, int length);  
 #endif  
   
 #define RCVAL()   (*(input++))  
 #define RSVAL()   ((*((input++) + 1) << 8) | RCVAL())  
 #define SCVAL(v)  {*(output++) = (v);}  
   
 #define FILL()    {while (n-- > 0) { if (output - start < width) { SCVAL(0) } else { SCVAL(*(output-width)); }}}  
 #define MIX()     {while (n-- > 0) { if (output - start < width) { SCVAL(mix) } else { SCVAL(*(output-width) ^ mix); }}}  
 #define COPY()    {while (n-- > 0) { SCVAL(RCVAL()); }}  
 #define COLOR()   {int color = RCVAL(); \  
                    while (n-- > 0) { SCVAL(color); }}  
 #define BICOLOR() {int color1 = RCVAL(); int color2 = RCVAL(); \  
                    while (n-- > 0) { SCVAL(color1); SCVAL(color2); }}  
 #define SETMIX_MIX() {mix = RCVAL(); MIX();}  
 #define COPY_PACKED() {n++; n/=2; while (n-- > 0) \  
                       {unsigned char c = RCVAL(); SCVAL((c & 0xF0) >> 4); \  
                                                   SCVAL(c & 0x0F); }}  
27    
28  BOOL bitmap_decompress(unsigned char *input, int size,  #define REPEAT(statement) \
29                         unsigned char *output, int width)  { \
30            while((count & ~0x7) && ((x+8) < width)) \
31                    UNROLL8( statement; count--; x++; ); \
32            \
33            while((count > 0) && (x < width)) { statement; count--; x++; } \
34    }
35    
36    #define MASK_UPDATE() \
37    { \
38            mixmask <<= 1; \
39            if (mixmask == 0) \
40            { \
41                    mask = fom_mask ? fom_mask : CVAL(input); \
42                    mixmask = 1; \
43            } \
44    }
45    
46    BOOL
47    bitmap_decompress(unsigned char *output, int width, int height,
48                      unsigned char *input, int size)
49  {  {
         unsigned char *savedinput = input;  
         unsigned char *start = output;  
50          unsigned char *end = input + size;          unsigned char *end = input + size;
51          unsigned char code;          unsigned char *prevline = NULL, *line = NULL;
52          unsigned char mix = 0xFF;          int opcode, count, offset, isfillormix, x = width;
53          int n, savedn;          int lastopcode = -1, insertmix = False, bicolour = False;
54            uint8 code, colour1 = 0, colour2 = 0;
55            uint8 mixmask, mask = 0, mix = 0xff;
56            int fom_mask = 0;
57    
58          while (input < end)          while (input < end)
59          {          {
60                  code = RCVAL();                  fom_mask = 0;
61                  switch (code)                  code = CVAL(input);
62                    opcode = code >> 4;
63    
64                    /* Handle different opcode forms */
65                    switch (opcode)
66                    {
67                            case 0xc:
68                            case 0xd:
69                            case 0xe:
70                                    opcode -= 6;
71                                    count = code & 0xf;
72                                    offset = 16;
73                                    break;
74    
75                            case 0xf:
76                                    opcode = code & 0xf;
77                                    if (opcode < 9)
78                                            count = SVAL(input);
79                                    else
80                                            count = (opcode < 0xb) ? 8 : 1;
81                                    offset = 0;
82                                    break;
83    
84                            default:
85                                    opcode >>= 1;
86                                    count = code & 0x1f;
87                                    offset = 32;
88                                    break;
89                    }
90    
91                    /* Handle strange cases for counts */
92                    if (offset != 0)
93                  {                  {
94                  case 0x00: // Fill                          isfillormix = ((opcode == 2) || (opcode == 7));
                         n = RCVAL() + 32;  
                         FILL();  
                         break;  
                 case 0xF0: // Fill  
                         n = RSVAL();  
                         FILL();  
                         break;  
                 case 0x20: // Mix  
                         n = RCVAL() + 32;  
                         MIX();  
                         break;  
                 case 0xF1: // Mix  
                         n = RSVAL();  
                         MIX();  
                         break;  
                 case 0x40: // FillOrMix  
                         fprintf(stderr, "FillOrMix unsupported\n");  
                         savedn = n = RCVAL() + 1;  
                         MIX();  
                         input += (savedn+7)/8;  
                         break;  
                 case 0xF2:  
                         fprintf(stderr, "FillOrMix unsupported\n");  
                         savedn = n = RSVAL();  
                         MIX();  
                         input += (savedn+7)/8;  
                         break;  
                 case 0x60: // Color  
                         n = RCVAL() + 32;  
                         COLOR();  
                         break;  
                 case 0xF3:  
                         n = RSVAL();  
                         fprintf(stderr, "Color %d\n", n);  
                         COLOR();  
                         break;  
                 case 0x80: // Copy  
                         n = RCVAL() + 32;  
                         COPY();  
                         break;  
                 case 0xF4:  
                         n = RSVAL();  
                         COPY();  
                         break;  
                 case 0xA0: // Copy Packed  
                         fprintf(stderr, "CopyPacked 1\n");  
                         n = RCVAL() + 32;  
                         COPY_PACKED();  
                         break;  
                 case 0xF5:  
                         fprintf(stderr, "CopyPacked 2\n");  
                         n = RSVAL();  
                         COPY_PACKED();  
                         break;  
                 case 0xC0: // SetMix_Mix  
                         fprintf(stderr, "SetMix_Mix 1\n");  
                         n = RCVAL() + 16;  
                         SETMIX_MIX();  
                         break;  
                 case 0xF6:  
                         fprintf(stderr, "SetMix_Mix 2\n");  
                         n = RSVAL();  
                         SETMIX_MIX();  
                         break;  
                 case 0xD0: // SetMix_FillOrMix  
                         fprintf(stderr, "SetMix_FillOrMix unsupported\n");  
                         savedn = n = RCVAL() + 1;  
                         SETMIX_MIX();  
                         input += (savedn+7)/8;  
                         break;  
                 case 0xF7:  
                         fprintf(stderr, "SetMix_FillOrMix unsupported\n");  
                         savedn = n = RSVAL();  
                         SETMIX_MIX();  
                         input += (savedn+7)/8;  
                         break;  
                 case 0xE0: // Bicolor  
                         fprintf(stderr, "Bicolor 1\n");  
                         n = RCVAL() + 16;  
                         BICOLOR();  
                         break;  
                 case 0xF8:  
                         fprintf(stderr, "Bicolor 2\n");  
                         n = RSVAL();  
                         BICOLOR();  
                         break;  
                 case 0xF9: // FillOrMix_1  
                         fprintf(stderr, "FillOrMix_1 unsupported\n");  
                         return False;  
                 case 0xFA: // FillOrMix_2  
                         fprintf(stderr, "FillOrMix_2 unsupported\n");  
                         return False;  
                 case 0xFD: // White  
                         SCVAL(0xFF);  
                         break;  
                 case 0xFE: // Black  
                         SCVAL(0);  
                         break;  
                 default:  
                         n = code & 31;  
95    
96                          if (n == 0)                          if (count == 0)
97                          {                          {
98                                  fprintf(stderr, "Undefined escape 0x%X\n", code);                                  if (isfillormix)
99                                  return False;                                          count = CVAL(input) + 1;
100                                    else
101                                            count = CVAL(input) + offset;
102                          }                          }
103                            else if (isfillormix)
                         switch ((code >> 5) & 7)  
104                          {                          {
105                          case 0: // Fill                                  count <<= 3;
106                                  FILL();                          }
107                                  break;                  }
108                          case 1: // Mix  
109                                  MIX();                  /* Read preliminary data */
110                    switch (opcode)
111                    {
112                            case 0: /* Fill */
113                                    if ((lastopcode == opcode)
114                                        && !((x == width) && (prevline == NULL)))
115                                            insertmix = True;
116                                    break;
117                            case 8: /* Bicolour */
118                                    colour1 = CVAL(input);
119                            case 3: /* Colour */
120                                    colour2 = CVAL(input);
121                                    break;
122                            case 6: /* SetMix/Mix */
123                            case 7: /* SetMix/FillOrMix */
124                                    mix = CVAL(input);
125                                    opcode -= 5;
126                                    break;
127                            case 9: /* FillOrMix_1 */
128                                    mask = 0x03;
129                                    opcode = 0x02;
130                                    fom_mask = 3;
131                                    break;
132                            case 0x0a:      /* FillOrMix_2 */
133                                    mask = 0x05;
134                                    opcode = 0x02;
135                                    fom_mask = 5;
136                                  break;                                  break;
137                          case 2: // FillOrMix  
138                                  fprintf(stderr, "FillOrMix unsupported\n");                  }
139                                  n *= 8;  
140                                  savedn = n;                  lastopcode = opcode;
141                                  MIX();                  mixmask = 0;
142                                  input += (savedn+7)/8;  
143                                  break;                  /* Output body */
144                          case 3: // Color                  while (count > 0)
145                                  COLOR();                  {
146                                  break;                          if (x >= width)
147                          case 4: // Copy                          {
148                                  COPY();                                  if (height <= 0)
                                 break;  
                         case 5: // Copy Packed  
                                 fprintf(stderr, "CopyPacked 3\n");  
                                 COPY_PACKED();  
                                 break;  
                         case 6:  
                                 n = code & 15;  
   
                                 switch ((code >> 4) & 15)  
                                 {  
                                 case 0xC:  
                                         fprintf(stderr, "SetMix_Mix 3\n");  
                                         SETMIX_MIX();  
                                         break;  
                                 case 0xD:  
                                         fprintf(stderr, "SetMix_FillOrMix unsupported\n");  
                                         n *= 8;  
                                         savedn = n;  
                                         SETMIX_MIX();  
                                         input += (savedn+7)/8;  
                                         break;  
                                 case 0xE:  
                                         fprintf(stderr, "Bicolor 3\n");  
                                         BICOLOR();  
                                         break;  
                                 default:  
                                         fprintf(stderr, "Undefined escape 0x%X\n", code);  
149                                          return False;                                          return False;
150                                  }  
151                                    x = 0;
152                                    height--;
153    
154                                    prevline = line;
155                                    line = output + height * width;
156                          }                          }
                 }  
         }  
157    
158          printf("Uncompressed size: %d\n", output - start);                          switch (opcode)
159  #if BITMAP_DEBUG                          {
160          {                                  case 0: /* Fill */
161                  static int bmpno = 1;                                          if (insertmix)
162                  char filename[64];                                          {
163                                                    if (prevline == NULL)
164                                                            line[x] = mix;
165                                                    else
166                                                            line[x] =
167                                                                    prevline[x] ^
168                                                                    mix;
169    
170                                                    insertmix = False;
171                                                    count--;
172                                                    x++;
173                                            }
174    
175                                            if (prevline == NULL)
176                                            {
177                                                    REPEAT(line[x] = 0);
178                                            }
179                                            else
180                                            {
181                                                    REPEAT(line[x] = prevline[x]);
182                                            }
183                                            break;
184    
185                  snprintf(filename, sizeof(filename)-1, "in%d.raw", bmpno);                                  case 1: /* Mix */
186                  hexdump(filename, savedinput, size);                                          if (prevline == NULL)
187                                            {
188                                                    REPEAT(line[x] = mix);
189                                            }
190                                            else
191                                            {
192                                                    REPEAT(line[x] =
193                                                           prevline[x] ^ mix);
194                                            }
195                                            break;
196    
197                  snprintf(filename, sizeof(filename)-1, "out%d.raw", bmpno);                                  case 2: /* Fill or Mix */
198                  hexdump(filename, start, output-start);                                          if (prevline == NULL)
199                                            {
200                                                    REPEAT(MASK_UPDATE();
201                                                           if (mask & mixmask)
202                                                           line[x] = mix;
203                                                           else
204                                                           line[x] = 0;);
205                                            }
206                                            else
207                                            {
208                                                    REPEAT(MASK_UPDATE();
209                                                           if (mask & mixmask)
210                                                           line[x] =
211                                                           prevline[x] ^ mix;
212                                                           else
213                                                           line[x] =
214                                                           prevline[x];);
215                                            }
216                                            break;
217    
218                  bmpno++;                                  case 3: /* Colour */
219          }                                          REPEAT(line[x] = colour2);
220  #endif                                          break;
221    
222          return True;                                  case 4: /* Copy */
223  }                                          REPEAT(line[x] = CVAL(input));
224                                            break;
225    
226                                    case 8: /* Bicolour */
227                                            REPEAT(if (bicolour)
228                                                   {
229                                                   line[x] = colour2;
230                                                   bicolour = False;}
231                                                   else
232                                                   {
233                                                   line[x] = colour1;
234                                                   bicolour = True; count++;}
235                                            );
236                                            break;
237    
238  #if BITMAP_DEBUG                                  case 0xd:       /* White */
239  void hexdump(char *filename, unsigned char *data, int length)                                          REPEAT(line[x] = 0xff);
240  {                                          break;
         /*  
         int i;  
241    
242          for (i = 0; i < length; i++)                                  case 0xe:       /* Black */
243          {                                          REPEAT(line[x] = 0x00);
244                  printf("%02X ", data[i]);                                          break;
245    
246                  if (i % 16 == 15)                                  default:
247                          printf("\n");                                          NOTIMP("bitmap opcode 0x%x\n",
248                                                   opcode);
249                                            return False;
250                            }
251                    }
252          }          }
         */  
   
         int fd;  
253    
254          fd = open(filename, O_WRONLY|O_CREAT, 0600);          return True;
         write(fd, data, length);  
         close(fd);  
255  }  }
 #endif  

Legend:
Removed from v.3  
changed lines
  Added in v.28

  ViewVC Help
Powered by ViewVC 1.1.26