1.1 diff -r 0e75b61c7aa4 -r f2c7acb4a258 PTdecode/src/main.cpp 1.2 --- a/PTdecode/src/main.cpp Fri Sep 25 10:51:24 2009 +0100 1.3 +++ b/PTdecode/src/main.cpp Tue Mar 18 01:27:15 2014 +0000 1.4 @@ -2,9 +2,10 @@ 1.5 * ptdecode: P-touch PT-2450DX output decoder 1.6 ****************************************************************************/ 1.7 1.8 +#include <cctype> 1.9 #include <cstdio> 1.10 #include <exception> 1.11 -#include "CImg.h" 1.12 +#include <CImg.h> 1.13 1.14 using namespace std; 1.15 using namespace cimg_library; 1.16 @@ -13,7 +14,7 @@ 1.17 const unsigned int PT_HEAD_WIDTH = 1024; 1.18 1.19 // If defined, makes "blank row" blocks visible 1.20 -//#define MAKE_BLANK_ROWS_VISIBLE 1.21 +#define MAKE_BLANK_ROWS_VISIBLE 1.22 1.23 // custom exception class for file read errors 1.24 class EReadError : public exception { 1.25 @@ -39,154 +40,6 @@ 1.26 } 1.27 } 1.28 1.29 -// Handler for graphics transfer mode 1 1.30 -void runGraphicsXferMode1() 1.31 -{ 1.32 - bool exit = false; 1.33 - unsigned int cm = -1; 1.34 - unsigned long xpos = 0; 1.35 - unsigned long ypos = 0; 1.36 - unsigned long ydim = 128; 1.37 - CImg<unsigned char> img(0, 0, 0, 0, (unsigned char)0); 1.38 - 1.39 - while (!exit) { 1.40 - unsigned char ch = getNext(); 1.41 - unsigned int len = 0; 1.42 - unsigned int rowpos = 0; 1.43 - unsigned char row[PT_HEAD_WIDTH / 8]; // stores uncompressed row data 1.44 - 1.45 - switch (ch) { 1.46 - case 'M': // Set compression mode 1.47 - ch = getNext(); 1.48 - cm = ch; 1.49 - printf("Set compression mode: 0x%02X", ch); 1.50 - switch (cm) { 1.51 - case 0x02: 1.52 - printf(" (TIFF/Packbits)\n"); 1.53 - break; 1.54 - default: 1.55 - printf(" *** Unknown, assuming uncompressed ***\n"); 1.56 - cm = 1; 1.57 - break; 1.58 - } 1.59 - break; 1.60 - 1.61 - case 'Z': // Blank raster line 1.62 - // Increment x-position and resize the image 1.63 - img.resize(xpos+1, ydim, 1, 1, 0, 0); 1.64 - 1.65 - // Blank the new row 1.66 - if (img.dimy() > 0) { 1.67 -// printf("Clear row: x=%lu\n", xpos); 1.68 - for (int i=0; i<img.dimy(); i++) { 1.69 -#ifdef MAKE_BLANK_ROWS_VISIBLE 1.70 - img(xpos, i) = 128; 1.71 -#else 1.72 - img(xpos, i) = 255; 1.73 -#endif 1.74 - } 1.75 - } 1.76 - 1.77 - xpos++; 1.78 - break; 1.79 - 1.80 - case 'G': // Graphics data row 1.81 - // decode the length 1.82 - ch = getNext(); 1.83 - len = (((int)getNext()) << 8) + ch; 1.84 - 1.85 - // Is gfx payload compressed or uncompressed? 1.86 - if (cm == 1) { 1.87 - // Uncompressed. Read straight into the row buffer. 1.88 - while (len > 0) { 1.89 - row[rowpos++] = getNext(); len--; 1.90 - } 1.91 - } else { 1.92 - // Decompress the gfx data 1.93 - rowpos = 0; 1.94 - while (len > 0) { 1.95 - // get the prefix byte 1.96 - ch = getNext(); len--; 1.97 - 1.98 - // Is this a "run" (a single byte replicated) or a "copy"? 1.99 - int runlen; 1.100 - if (ch & 0x80) { 1.101 - // MSB set, it's a run 1.102 - runlen = 257 - ((int)ch); 1.103 - 1.104 - // Get the byte to replicate, and replicate it into the o/p buffer 1.105 - ch = getNext(); len--; 1.106 - while (runlen-- > 0) { 1.107 - row[rowpos++] = ch; 1.108 - } 1.109 - } else { 1.110 - // MSB clear, it's a copy 1.111 - runlen = ((int)ch) + 1; 1.112 - 1.113 - // Copy N bytes from the input stream to the output 1.114 - while (runlen-- > 0) { 1.115 - row[rowpos++] = getNext(); 1.116 - len--; 1.117 - } 1.118 - } 1.119 - } 1.120 - } 1.121 - 1.122 - // Row decode complete. row contains the image data, and rowpos 1.123 - // contains its length in bytes. Now shuffle it into CImg... 1.124 - 1.125 - // If image height is less than size of image row, then make the 1.126 - // image taller. 1.127 - if (((unsigned int)img.dimy()) < (rowpos * 8)) { 1.128 - ydim = rowpos * 8; 1.129 - } else { 1.130 - ydim = img.dimy(); 1.131 - } 1.132 - 1.133 - // Perform the Y resize if necessary, but also make Xdim=Xdim+1 1.134 - img.resize(xpos+1, ydim, 1, 1, 0, 0); 1.135 - 1.136 - img(xpos, ydim/2) = 128; 1.137 - 1.138 - // Now copy the image data... 1.139 - ypos = 0; 1.140 - for (unsigned int byte=0; byte<rowpos; byte++) { 1.141 - for (unsigned int bit=0; bit<8; bit++) { 1.142 - if (row[byte] & (0x80>>bit)) { 1.143 - img(xpos, ypos) = 0; 1.144 - } else { 1.145 - img(xpos, ypos) = 255; 1.146 - } 1.147 - 1.148 - // Increment y-position 1.149 - ypos++; 1.150 - } 1.151 - } 1.152 - 1.153 - // An entire row has been decoded. Increment x-position. 1.154 - xpos++; 1.155 - break; 1.156 - 1.157 - case 0x0c: // FF 1.158 - printf("Formfeed: Print without label feed (job completed, more labels follow)\n"); 1.159 - exit = true; 1.160 - break; 1.161 - 1.162 - case 0x1a: // Ctrl-Z 1.163 - printf("Ctrl-Z: Print with label feed (job completed, no further labels)\n"); 1.164 - exit = true; 1.165 - break; 1.166 - 1.167 - default: // Something else 1.168 - printf("** Unrecognised command prefix in gfx mode: 0x%02x\n", ch); 1.169 - break; 1.170 - } 1.171 - } 1.172 - 1.173 - // Display the contents of the image 1.174 - img.display(); 1.175 -} 1.176 - 1.177 // Parse an ESC i command 1.178 void parse_esc_i() 1.179 { 1.180 @@ -194,6 +47,11 @@ 1.181 unsigned int tmpI; 1.182 1.183 switch (ch) { 1.184 + case 'A': // ESC i A: QL-specific command 1.185 + ch = getNext(); 1.186 + printf("*** QL-SPECIFIC: ESC i A 0x%02X\n", ch); 1.187 + break; 1.188 + 1.189 case 'B': // ESC i B: Specify baud rate 1.190 tmpI = getNext(); 1.191 ch = getNext(); 1.192 @@ -221,7 +79,7 @@ 1.193 tmpI = getNext(); 1.194 ch = getNext(); 1.195 tmpI += ((int)ch)*256; 1.196 - printf("Set margin:\t%d dots", tmpI); 1.197 + printf("Set margin:\t%d dots\n", tmpI); 1.198 break; 1.199 1.200 case 'K': // ESC i K: Set expanded mode 1.201 @@ -240,14 +98,13 @@ 1.202 printf("Set graphics transfer mode 0x%02X: ", ch); 1.203 if (ch == 1) { 1.204 printf("Raster graphics mode\n"); 1.205 - runGraphicsXferMode1(); 1.206 } else { 1.207 printf("\n\tUnrecognised graphics transfer mode: remainder of data may be garbage.\n"); 1.208 } 1.209 break; 1.210 1.211 default: 1.212 - printf("Unrecognised cmnd: ESC i 0x%02X\n", ch); 1.213 + printf("Unrecognised cmnd: ESC i 0x%02X [%c]\n", ch, ch); 1.214 break; 1.215 } 1.216 } 1.217 @@ -274,6 +131,16 @@ 1.218 1.219 int main(int argc, char **argv) 1.220 { 1.221 + unsigned int cm = -1; 1.222 + unsigned long xpos = 0; 1.223 + unsigned long ypos = 0; 1.224 + unsigned long ydim = 128; 1.225 + CImg<unsigned char> img(0, 0, 0, 0, (unsigned char)0); 1.226 + unsigned int len = 0; 1.227 + unsigned int rowpos = 0; 1.228 + unsigned char row[PT_HEAD_WIDTH / 8]; // stores uncompressed row data 1.229 + 1.230 + 1.231 // check params 1.232 if (argc != 2) { 1.233 // wrong! 1.234 @@ -298,17 +165,137 @@ 1.235 case 0x00: // NULL 1.236 printf("Null\n"); 1.237 break; 1.238 + 1.239 case 0x0c: // FF 1.240 + img.display("P-Touch Data [no feed]", false); 1.241 printf("Formfeed: Print without feed\n"); 1.242 + xpos = 0; 1.243 break; 1.244 + 1.245 case 0x1a: // Ctrl-Z 1.246 + img.display("P-Touch Data [label feed]", false); 1.247 printf("Ctrl-Z: Print with label feed\n"); 1.248 + xpos = 0; 1.249 break; 1.250 + 1.251 case 0x1b: // ESC 1.252 parse_esc(); 1.253 break; 1.254 + 1.255 + case 'M': // Set compression mode 1.256 + ch = getNext(); 1.257 + cm = ch; 1.258 + printf("Set compression mode: 0x%02x", ch); 1.259 + switch (cm) { 1.260 + case 0x02: 1.261 + printf(" (TIFF/PackBits)\n"); 1.262 + break; 1.263 + default: 1.264 + printf(" *** Unknown, assuming uncompressed ***\n"); 1.265 + cm = 1; 1.266 + break; 1.267 + } 1.268 + break; 1.269 + 1.270 + case 'Z': // blank raster line 1.271 + // Increment x-position and resize the image 1.272 + img.resize(xpos+1, ydim, 1, 1, 0, 0); 1.273 + 1.274 + // Blank the new row 1.275 + if (img.height() > 0) { 1.276 + // printf("clear row: x=%lu\n", xpos); 1.277 + for (int i=0; i<img.height(); i++) { 1.278 +#ifdef MAKE_BLANK_ROWS_VISIBLE 1.279 + img(xpos, i) = 128; 1.280 +#else 1.281 + img(xpos, i) = 255; 1.282 +#endif 1.283 + } 1.284 + } 1.285 + 1.286 + xpos++; 1.287 + break; 1.288 + 1.289 + case 'G': // Graphics data row 1.290 + // decode the length 1.291 + ch = getNext(); 1.292 + len = (((int)getNext()) << 8) + ch; 1.293 + 1.294 + // Is gfx payload compressed or uncompressed? 1.295 + if (cm == 1) { 1.296 + // Uncompressed. Read straight into the row buffer. 1.297 + while (len > 0) { 1.298 + row[rowpos++] = getNext(); len--; 1.299 + } 1.300 + } else { 1.301 + // Decompress the gfx data 1.302 + rowpos = 0; 1.303 + while (len > 0) { 1.304 + // get the prefix byte 1.305 + ch = getNext(); len--; 1.306 + 1.307 + // Is this a "run" (a single byte replicated) or a "copy"? 1.308 + int runlen; 1.309 + if (ch & 0x80) { 1.310 + // MSB set, it's a run 1.311 + runlen = 257 - ((int)ch); 1.312 + 1.313 + // Get the byte to replicate, and replicate it into the o/p buffer 1.314 + ch = getNext(); len--; 1.315 + while (runlen-- > 0) { 1.316 + row[rowpos++] = ch; 1.317 + } 1.318 + } else { 1.319 + // MSB clear, it's a copy 1.320 + runlen = ((int)ch) + 1; 1.321 + 1.322 + // Copy N bytes from the input stream to the output 1.323 + while (runlen-- > 0) { 1.324 + row[rowpos++] = getNext(); 1.325 + len--; 1.326 + } 1.327 + } 1.328 + } 1.329 + } 1.330 + 1.331 + // Row decode complete. row contains the image data, and rowpos 1.332 + // contains its length in bytes. Now shuffle it into CImg... 1.333 + 1.334 + // If image height is less than size of image row, then make the 1.335 + // image taller. 1.336 + if (((unsigned int)img.height()) < (rowpos * 8)) { 1.337 + ydim = rowpos * 8; 1.338 + } else { 1.339 + ydim = img.height(); 1.340 + } 1.341 + 1.342 + // Perform the Y resize if necessary, but also make Xdim=Xdim+1 1.343 + img.resize(xpos+1, ydim, 1, 1, 0, 0); 1.344 + 1.345 + img(xpos, ydim/2) = 128; 1.346 + 1.347 + // Now copy the image data... 1.348 + ypos = 0; 1.349 + for (unsigned int byte=0; byte<rowpos; byte++) { 1.350 + for (unsigned int bit=0; bit<8; bit++) { 1.351 + if (row[byte] & (0x80>>bit)) { 1.352 + img(xpos, ypos) = 0; 1.353 + } else { 1.354 + img(xpos, ypos) = 255; 1.355 + } 1.356 + 1.357 + // Increment y-position 1.358 + ypos++; 1.359 + } 1.360 + } 1.361 + 1.362 + // An entire row has been decoded. Increment x-position. 1.363 + xpos++; 1.364 + break; 1.365 + 1.366 + 1.367 default: 1.368 - printf("Unrecognised cmnd: 0x%02X\n", ch); 1.369 + printf("Unrecognised cmnd: 0x%02X [%c]\n", ch, ch); 1.370 break; 1.371 } 1.372 }