/[rdesktop]/sourceforge.net/tags/RDESKTOP-1-3-1/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/tags/RDESKTOP-1-3-1/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 10 by matty, Tue Aug 15 10:23:24 2000 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 REPEAT(statement) { while ((count > 0) && (x < width)) { statement; count--; x++; } }
27  void hexdump(char *filename, unsigned char *data, int length);  #define MASK_UPDATE() { mixmask <<= 1; if (mixmask == 0) { mask = CVAL(input); mixmask = 1; } }
 #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); }}  
28    
29  BOOL bitmap_decompress(unsigned char *input, int size,  BOOL bitmap_decompress(unsigned char *output, int width, int height,
30                         unsigned char *output, int width)                         unsigned char *input, int size)
31  {  {
         unsigned char *savedinput = input;  
         unsigned char *start = output;  
32          unsigned char *end = input + size;          unsigned char *end = input + size;
33          unsigned char code;          unsigned char *prevline = NULL, *line = NULL;
34          unsigned char mix = 0xFF;          int opcode, count, offset, isfillormix, x = width;
35          int n, savedn;          int lastopcode = -1, insertmix = False;
36            uint8 code, colour1 = 0, colour2 = 0;
37            uint8 mixmask, mask = 0, mix = 0xff;
38    
39          while (input < end)          while (input < end)
40          {          {
41                  code = RCVAL();                  code = CVAL(input);
42                  switch (code)                  opcode = code >> 4;
43    
44                    /* Handle different opcode forms */
45                    switch (opcode)
46                    {
47                            case 0xc:
48                            case 0xd:
49                            case 0xe:
50                                    opcode -= 6;
51                                    count = code & 0xf;
52                                    offset = 16;
53                                    break;
54    
55                            case 0xf:
56                                    opcode = code & 0xf;
57                                    count = (opcode < 13) ? SVAL(input) : 1;
58                                    offset = 0;
59                                    break;
60    
61                            default:
62                                    opcode >>= 1;
63                                    count = code & 0x1f;
64                                    offset = 32;
65                                    break;
66                    }
67    
68                    /* Handle strange cases for counts */
69                    if (offset != 0)
70                  {                  {
71                  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;  
72    
73                          if (n == 0)                          if (count == 0)
74                          {                          {
75                                  fprintf(stderr, "Undefined escape 0x%X\n", code);                                  if (isfillormix)
76                                  return False;                                          count = CVAL(input) + 1;
77                                    else
78                                            count = CVAL(input) + offset;
79                          }                          }
80                            else if (isfillormix)
                         switch ((code >> 5) & 7)  
81                          {                          {
82                          case 0: // Fill                                  count <<= 3;
83                                  FILL();                          }
84                                  break;                  }
85                          case 1: // Mix  
86                                  MIX();                  /* Read preliminary data */
87                    mixmask = 0;
88                    switch (opcode)
89                    {
90                            case 0: /* Fill */
91                                    if ((lastopcode == opcode) && (x != width))
92                                            insertmix = True;
93                                    break;
94                            case 8: /* Bicolour */
95                                    colour1 = CVAL(input);
96                            case 3: /* Colour */
97                                    colour2 = CVAL(input);
98                                    break;
99                            case 6: /* SetMix/Mix */
100                            case 7: /* SetMix/FillOrMix */
101                                    mix = CVAL(input);
102                                    opcode -= 5;
103                                  break;                                  break;
104                          case 2: // FillOrMix                  }
105                                  fprintf(stderr, "FillOrMix unsupported\n");                  lastopcode = opcode;
106                                  n *= 8;  
107                                  savedn = n;                  /* Output body */
108                                  MIX();                  while (count > 0)
109                                  input += (savedn+7)/8;                  {
110                                  break;                          if (x >= width)
111                          case 3: // Color                          {
112                                  COLOR();                                  if (height <= 0)
                                 break;  
                         case 4: // Copy  
                                 COPY();  
                                 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);  
113                                          return False;                                          return False;
114                                  }  
115                                    x = 0;
116                                    height--;
117    
118                                    prevline = line;
119                                    line = output + height * width;
120                          }                          }
                 }  
         }  
121    
122          printf("Uncompressed size: %d\n", output - start);                          switch (opcode)
123  #if BITMAP_DEBUG                          {
124          {                                  case 0: /* Fill */
125                  static int bmpno = 1;                                          if (insertmix)
126                  char filename[64];                                          {
127                                                    if (prevline == NULL)
128                                                            line[x] = mix;
129                                                    else
130                                                            line[x] = prevline[x] ^ mix;
131    
132                                                    insertmix = False;
133                                                    count--;
134                                                    x++;
135                                            }
136    
137                                            if (prevline == NULL)
138                                                    REPEAT(line[x] = 0)
139                                            else
140                                                    REPEAT(line[x] = prevline[x])
141                                            break;
142    
143                  snprintf(filename, sizeof(filename)-1, "in%d.raw", bmpno);                                  case 1: /* Mix */
144                  hexdump(filename, savedinput, size);                                          if (prevline == NULL)
145                                                    REPEAT(line[x] = mix)
146                                            else
147                                                    REPEAT(line[x] = prevline[x] ^ mix)
148                                            break;
149    
150                  snprintf(filename, sizeof(filename)-1, "out%d.raw", bmpno);                                  case 2: /* Fill or Mix */
151                  hexdump(filename, start, output-start);                                          if (prevline == NULL)
152                                                REPEAT(
153                                                       MASK_UPDATE();
154    
155                                                       if (mask & mixmask)
156                                                            line[x] = mix;
157                                                       else
158                                                            line[x] = 0;
159                                                )
160                                            else
161                                                REPEAT(
162                                                       MASK_UPDATE();
163    
164                                                       if (mask & mixmask)
165                                                            line[x] = prevline[x] ^ mix;
166                                                       else
167                                                            line[x] = prevline[x];
168                                                )
169                                            break;
170    
171                  bmpno++;                                  case 3: /* Colour */
172          }                                          REPEAT(line[x] = colour2)
173  #endif                                          break;
174    
175          return True;                                  case 4: /* Copy */
176  }                                          REPEAT(line[x] = CVAL(input))
177                                            break;
178    
179                                    case 8: /* Bicolour */
180                                            REPEAT(line[x] = colour1; line[++x] = colour2)
181                                            break;
182    
183  #if BITMAP_DEBUG                                  case 13: /* White */
184  void hexdump(char *filename, unsigned char *data, int length)                                          REPEAT(line[x] = 0xff)
185  {                                          break;
         /*  
         int i;  
186    
187          for (i = 0; i < length; i++)                                  case 14: /* Black */
188          {                                          REPEAT(line[x] = 0x00)
189                  printf("%02X ", data[i]);                                          break;
190    
191                  if (i % 16 == 15)                                  default:
192                          printf("\n");                                          NOTIMP("bitmap opcode 0x%x\n", opcode);
193                                            return False;
194                            }
195                    }
196          }          }
         */  
197    
198          int fd;          return True;
   
         fd = open(filename, O_WRONLY|O_CREAT, 0600);  
         write(fd, data, length);  
         close(fd);  
199  }  }
 #endif  

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

  ViewVC Help
Powered by ViewVC 1.1.26