1 /**************************************************************************/
2 /*                                                                        */
3 /* Copyright (c) 2001, 2010 NoMachine, http://www.nomachine.com/.         */
4 /*                                                                        */
5 /* NXCOMP, NX protocol compression and NX extensions to this software     */
6 /* are copyright of NoMachine. Redistribution and use of the present      */
7 /* software is allowed according to terms specified in the file LICENSE   */
8 /* which comes in the source distribution.                                */
9 /*                                                                        */
10 /* Check http://www.nomachine.com/licensing.html for applicability.       */
11 /*                                                                        */
12 /* NX and NoMachine are trademarks of Medialogic S.p.A.                   */
13 /*                                                                        */
14 /* All rights reserved.                                                   */
15 /*                                                                        */
16 /**************************************************************************/
17 
18 #include "Misc.h"
19 #include "Rgb.h"
20 
21 #define PANIC
22 #define WARNING
23 #undef  TEST
24 #undef  DEBUG
25 
UnpackRgb(T_geometry * geometry,unsigned char method,unsigned char * src_data,int src_size,int dst_bpp,int dst_width,int dst_height,unsigned char * dst_data,int dst_size)26 int UnpackRgb(T_geometry *geometry, unsigned char method, unsigned char *src_data,
27                   int src_size, int dst_bpp, int dst_width, int dst_height,
28                       unsigned char *dst_data, int dst_size)
29 {
30   if (*src_data == 0)
31   {
32     if (dst_size != src_size - 1)
33     {
34       #ifdef TEST
35       *logofs << "UnpackRgb: PANIC! Invalid destination size "
36               << dst_size << " with source " << src_size
37               << ".\n" << logofs_flush;
38       #endif
39 
40       return -1;
41     }
42 
43     #ifdef TEST
44     *logofs << "UnpackRgb: Expanding " << src_size - 1
45             << " bytes of plain RGB data.\n" << logofs_flush;
46     #endif
47 
48     memcpy(dst_data, src_data + 1, src_size - 1);
49 
50     return 1;
51   }
52 
53   unsigned int check_size = dst_size;
54 
55   int result = ZDecompress(&unpackStream, dst_data, &check_size,
56                                src_data + 1, src_size - 1);
57 
58   if (result != Z_OK)
59   {
60     #ifdef PANIC
61     *logofs << "UnpackRgb: PANIC! Failure decompressing RGB data. "
62             << "Error is '" << zError(result) << "'.\n"
63             << logofs_flush;
64     #endif
65 
66     cerr << "Error" << ": Failure decompressing RGB data. "
67          << "Error is '" << zError(result) << "'.\n";
68 
69     return -1;
70   }
71   else if (check_size != (unsigned int) dst_size)
72   {
73     #ifdef PANIC
74     *logofs << "UnpackRgb: PANIC! Size mismatch in RGB data. "
75             << "Resulting size is " << check_size << " with "
76             << "expected size " << dst_size << ".\n"
77             << logofs_flush;
78     #endif
79 
80     cerr << "Error" << ": Size mismatch in RGB data. "
81          << "Resulting size is " << check_size << " with "
82          << "expected size " << dst_size << ".\n";
83 
84     return -1;
85   }
86 
87   #ifdef TEST
88   *logofs << "UnpackRgb: Decompressed " << src_size - 1
89           << " bytes to " << dst_size << " bytes of RGB data.\n"
90           << logofs_flush;
91   #endif
92 
93   return 1;
94 }
95