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