Update PTdecode to handle output from other Ptouch drivers default tip

Tue, 18 Mar 2014 01:27:15 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Tue, 18 Mar 2014 01:27:15 +0000
changeset 23
f2c7acb4a258
parent 22
0e75b61c7aa4

Update PTdecode to handle output from other Ptouch drivers

PTdecode/Makefile file | annotate | diff | revisions
PTdecode/src/main.cpp file | annotate | diff | revisions
     1.1 --- a/PTdecode/Makefile	Fri Sep 25 10:51:24 2009 +0100
     1.2 +++ b/PTdecode/Makefile	Tue Mar 18 01:27:15 2014 +0000
     1.3 @@ -117,7 +117,7 @@
     1.4  LIBPATH		=
     1.5  # include paths -- where to search for #include files (in addition to the
     1.6  # standard paths
     1.7 -INCPATH		=	CImg-1.3.0
     1.8 +INCPATH		=
     1.9  # garbage files that should be deleted on a 'make clean' or 'make tidy'
    1.10  GARBAGE		=
    1.11  
     2.1 --- a/PTdecode/src/main.cpp	Fri Sep 25 10:51:24 2009 +0100
     2.2 +++ b/PTdecode/src/main.cpp	Tue Mar 18 01:27:15 2014 +0000
     2.3 @@ -2,9 +2,10 @@
     2.4   * ptdecode: P-touch PT-2450DX output decoder
     2.5   ****************************************************************************/
     2.6  
     2.7 +#include <cctype>
     2.8  #include <cstdio>
     2.9  #include <exception>
    2.10 -#include "CImg.h"
    2.11 +#include <CImg.h>
    2.12  
    2.13  using namespace std;
    2.14  using namespace cimg_library;
    2.15 @@ -13,7 +14,7 @@
    2.16  const unsigned int PT_HEAD_WIDTH = 1024;
    2.17  
    2.18  // If defined, makes "blank row" blocks visible
    2.19 -//#define MAKE_BLANK_ROWS_VISIBLE
    2.20 +#define MAKE_BLANK_ROWS_VISIBLE
    2.21  
    2.22  // custom exception class for file read errors
    2.23  class EReadError : public exception {
    2.24 @@ -39,154 +40,6 @@
    2.25  	}
    2.26  }
    2.27  
    2.28 -// Handler for graphics transfer mode 1
    2.29 -void runGraphicsXferMode1()
    2.30 -{
    2.31 -	bool exit = false;
    2.32 -	unsigned int cm = -1;
    2.33 -	unsigned long xpos = 0;
    2.34 -	unsigned long ypos = 0;
    2.35 -	unsigned long ydim = 128;
    2.36 -	CImg<unsigned char> img(0, 0, 0, 0, (unsigned char)0);
    2.37 -
    2.38 -	while (!exit) {
    2.39 -		unsigned char ch = getNext();
    2.40 -		unsigned int len = 0;
    2.41 -		unsigned int rowpos = 0;
    2.42 -		unsigned char row[PT_HEAD_WIDTH / 8];	// stores uncompressed row data
    2.43 -
    2.44 -		switch (ch) {
    2.45 -			case 'M':			// Set compression mode
    2.46 -				ch = getNext();
    2.47 -				cm = ch;
    2.48 -				printf("Set compression mode: 0x%02X", ch);
    2.49 -				switch (cm) {
    2.50 -					case 0x02:
    2.51 -						printf(" (TIFF/Packbits)\n");
    2.52 -						break;
    2.53 -					default:
    2.54 -						printf(" *** Unknown, assuming uncompressed ***\n");
    2.55 -						cm = 1;
    2.56 -						break;
    2.57 -				}
    2.58 -				break;
    2.59 -
    2.60 -			case 'Z':			// Blank raster line
    2.61 -				// Increment x-position and resize the image
    2.62 -				img.resize(xpos+1, ydim, 1, 1, 0, 0);
    2.63 -
    2.64 -				// Blank the new row
    2.65 -				if (img.dimy() > 0) {
    2.66 -//					printf("Clear row: x=%lu\n", xpos);
    2.67 -					for (int i=0; i<img.dimy(); i++) {
    2.68 -#ifdef MAKE_BLANK_ROWS_VISIBLE
    2.69 -						img(xpos, i) = 128;
    2.70 -#else
    2.71 -						img(xpos, i) = 255;
    2.72 -#endif
    2.73 -					}
    2.74 -				}
    2.75 -
    2.76 -				xpos++;
    2.77 -				break;
    2.78 -
    2.79 -			case 'G':			// Graphics data row
    2.80 -				// decode the length
    2.81 -				ch = getNext();
    2.82 -				len = (((int)getNext()) << 8) + ch;
    2.83 -
    2.84 -				// Is gfx payload compressed or uncompressed?
    2.85 -				if (cm == 1) {
    2.86 -					// Uncompressed. Read straight into the row buffer.
    2.87 -					while (len > 0) {
    2.88 -						row[rowpos++] = getNext(); len--;
    2.89 -					}
    2.90 -				} else {
    2.91 -					// Decompress the gfx data
    2.92 -					rowpos = 0;
    2.93 -					while (len > 0) {
    2.94 -						// get the prefix byte
    2.95 -						ch = getNext(); len--;
    2.96 -
    2.97 -						// Is this a "run" (a single byte replicated) or a "copy"?
    2.98 -						int runlen;
    2.99 -						if (ch & 0x80) {
   2.100 -							// MSB set, it's a run
   2.101 -							runlen = 257 - ((int)ch);
   2.102 -
   2.103 -							// Get the byte to replicate, and replicate it into the o/p buffer
   2.104 -							ch = getNext(); len--;
   2.105 -							while (runlen-- > 0) {
   2.106 -								row[rowpos++] = ch;
   2.107 -							}
   2.108 -						} else {
   2.109 -							// MSB clear, it's a copy
   2.110 -							runlen = ((int)ch) + 1;
   2.111 -
   2.112 -							// Copy N bytes from the input stream to the output
   2.113 -							while (runlen-- > 0) {
   2.114 -								row[rowpos++] = getNext();
   2.115 -								len--;
   2.116 -							}
   2.117 -						}
   2.118 -					}
   2.119 -				}
   2.120 -
   2.121 -				// Row decode complete. row contains the image data, and rowpos
   2.122 -				// contains its length in bytes. Now shuffle it into CImg...
   2.123 -
   2.124 -				// If image height is less than size of image row, then make the
   2.125 -				// image taller.
   2.126 -				if (((unsigned int)img.dimy()) < (rowpos * 8)) {
   2.127 -					ydim = rowpos * 8;
   2.128 -				} else {
   2.129 -					ydim = img.dimy();
   2.130 -				}
   2.131 -
   2.132 -				// Perform the Y resize if necessary, but also make Xdim=Xdim+1
   2.133 -				img.resize(xpos+1, ydim, 1, 1, 0, 0);
   2.134 -
   2.135 -				img(xpos, ydim/2) = 128;
   2.136 -
   2.137 -				// Now copy the image data...
   2.138 -				ypos = 0;
   2.139 -				for (unsigned int byte=0; byte<rowpos; byte++) {
   2.140 -					for (unsigned int bit=0; bit<8; bit++) {
   2.141 -						if (row[byte] & (0x80>>bit)) {
   2.142 -							img(xpos, ypos) = 0;
   2.143 -						} else {
   2.144 -							img(xpos, ypos) = 255;
   2.145 -						}
   2.146 -
   2.147 -						// Increment y-position
   2.148 -						ypos++;
   2.149 -					}
   2.150 -				}
   2.151 -
   2.152 -				// An entire row has been decoded. Increment x-position.
   2.153 -				xpos++;
   2.154 -				break;
   2.155 -
   2.156 -			case 0x0c:		// FF
   2.157 -				printf("Formfeed: Print without label feed (job completed, more labels follow)\n");
   2.158 -				exit = true;
   2.159 -				break;
   2.160 -
   2.161 -			case 0x1a:		// Ctrl-Z
   2.162 -				printf("Ctrl-Z:   Print with label feed    (job completed, no further labels)\n");
   2.163 -				exit = true;
   2.164 -				break;
   2.165 -
   2.166 -			default:			// Something else
   2.167 -				printf("** Unrecognised command prefix in gfx mode: 0x%02x\n", ch);
   2.168 -				break;
   2.169 -		}
   2.170 -	}
   2.171 -
   2.172 -	// Display the contents of the image
   2.173 -	img.display();
   2.174 -}
   2.175 -
   2.176  // Parse an ESC i command
   2.177  void parse_esc_i()
   2.178  {
   2.179 @@ -194,6 +47,11 @@
   2.180  	unsigned int tmpI;
   2.181  
   2.182  	switch (ch) {
   2.183 +		case 'A':				// ESC i A: QL-specific command
   2.184 +			ch = getNext();
   2.185 +			printf("*** QL-SPECIFIC: ESC i A 0x%02X\n", ch);
   2.186 +			break;
   2.187 +
   2.188  		case 'B':				// ESC i B: Specify baud rate
   2.189  			tmpI = getNext();
   2.190  			ch = getNext();
   2.191 @@ -221,7 +79,7 @@
   2.192  			tmpI = getNext();
   2.193  			ch = getNext();
   2.194  			tmpI += ((int)ch)*256;
   2.195 -			printf("Set margin:\t%d dots", tmpI);
   2.196 +			printf("Set margin:\t%d dots\n", tmpI);
   2.197  			break;
   2.198  
   2.199  		case 'K':				// ESC i K: Set expanded mode
   2.200 @@ -240,14 +98,13 @@
   2.201  			printf("Set graphics transfer mode 0x%02X: ", ch);
   2.202  			if (ch == 1) {
   2.203  				printf("Raster graphics mode\n");
   2.204 -				runGraphicsXferMode1();
   2.205  			} else {
   2.206  				printf("\n\tUnrecognised graphics transfer mode: remainder of data may be garbage.\n");
   2.207  			}
   2.208  			break;
   2.209  
   2.210  		default:
   2.211 -			printf("Unrecognised cmnd: ESC i 0x%02X\n", ch);
   2.212 +			printf("Unrecognised cmnd: ESC i 0x%02X [%c]\n", ch, ch);
   2.213  			break;
   2.214  	}
   2.215  }
   2.216 @@ -274,6 +131,16 @@
   2.217  
   2.218  int main(int argc, char **argv)
   2.219  {
   2.220 +	unsigned int cm = -1;
   2.221 +	unsigned long xpos = 0;
   2.222 +	unsigned long ypos = 0;
   2.223 +	unsigned long ydim = 128;
   2.224 +	CImg<unsigned char> img(0, 0, 0, 0, (unsigned char)0);
   2.225 +	unsigned int len = 0;
   2.226 +	unsigned int rowpos = 0;
   2.227 +	unsigned char row[PT_HEAD_WIDTH / 8];	// stores uncompressed row data
   2.228 +
   2.229 +
   2.230  	// check params
   2.231  	if (argc != 2) {
   2.232  		// wrong!
   2.233 @@ -298,17 +165,137 @@
   2.234  				case 0x00:		// NULL
   2.235  					printf("Null\n");
   2.236  					break;
   2.237 +
   2.238  				case 0x0c:		// FF
   2.239 +					img.display("P-Touch Data [no feed]", false);
   2.240  					printf("Formfeed: Print without feed\n");
   2.241 +					xpos = 0;
   2.242  					break;
   2.243 +
   2.244  				case 0x1a:		// Ctrl-Z
   2.245 +					img.display("P-Touch Data [label feed]", false);
   2.246  					printf("Ctrl-Z:   Print with label feed\n");
   2.247 +					xpos = 0;
   2.248  					break;
   2.249 +
   2.250  				case 0x1b:		// ESC
   2.251  					parse_esc();
   2.252  					break;
   2.253 +
   2.254 +				case 'M':			// Set compression mode
   2.255 +					ch = getNext();
   2.256 +					cm = ch;
   2.257 +					printf("Set compression mode: 0x%02x", ch);
   2.258 +					switch (cm) {
   2.259 +						case 0x02:
   2.260 +							printf(" (TIFF/PackBits)\n");
   2.261 +							break;
   2.262 +						default:
   2.263 +							printf(" *** Unknown, assuming uncompressed ***\n");
   2.264 +							cm = 1;
   2.265 +							break;
   2.266 +					}
   2.267 +					break;
   2.268 +
   2.269 +				case 'Z':			// blank raster line
   2.270 +					// Increment x-position and resize the image
   2.271 +					img.resize(xpos+1, ydim, 1, 1, 0, 0);
   2.272 +
   2.273 +					// Blank the new row
   2.274 +					if (img.height() > 0) {
   2.275 +	//					printf("clear row: x=%lu\n", xpos);
   2.276 +						for (int i=0; i<img.height(); i++) {
   2.277 +#ifdef MAKE_BLANK_ROWS_VISIBLE
   2.278 +							img(xpos, i) = 128;
   2.279 +#else
   2.280 +							img(xpos, i) = 255;
   2.281 +#endif
   2.282 +						}
   2.283 +					}
   2.284 +
   2.285 +					xpos++;
   2.286 +					break;
   2.287 +
   2.288 +				case 'G':			// Graphics data row
   2.289 +					// decode the length
   2.290 +					ch = getNext();
   2.291 +					len = (((int)getNext()) << 8) + ch;
   2.292 +
   2.293 +					// Is gfx payload compressed or uncompressed?
   2.294 +					if (cm == 1) {
   2.295 +						// Uncompressed. Read straight into the row buffer.
   2.296 +						while (len > 0) {
   2.297 +							row[rowpos++] = getNext(); len--;
   2.298 +						}
   2.299 +					} else {
   2.300 +						// Decompress the gfx data
   2.301 +						rowpos = 0;
   2.302 +						while (len > 0) {
   2.303 +							// get the prefix byte
   2.304 +							ch = getNext(); len--;
   2.305 +
   2.306 +							// Is this a "run" (a single byte replicated) or a "copy"?
   2.307 +							int runlen;
   2.308 +							if (ch & 0x80) {
   2.309 +								// MSB set, it's a run
   2.310 +								runlen = 257 - ((int)ch);
   2.311 +
   2.312 +								// Get the byte to replicate, and replicate it into the o/p buffer
   2.313 +								ch = getNext(); len--;
   2.314 +								while (runlen-- > 0) {
   2.315 +									row[rowpos++] = ch;
   2.316 +								}
   2.317 +							} else {
   2.318 +								// MSB clear, it's a copy
   2.319 +								runlen = ((int)ch) + 1;
   2.320 +
   2.321 +								// Copy N bytes from the input stream to the output
   2.322 +								while (runlen-- > 0) {
   2.323 +									row[rowpos++] = getNext();
   2.324 +									len--;
   2.325 +								}
   2.326 +							}
   2.327 +						}
   2.328 +					}
   2.329 +
   2.330 +					// Row decode complete. row contains the image data, and rowpos
   2.331 +					// contains its length in bytes. Now shuffle it into CImg...
   2.332 +
   2.333 +					// If image height is less than size of image row, then make the
   2.334 +					// image taller.
   2.335 +					if (((unsigned int)img.height()) < (rowpos * 8)) {
   2.336 +						ydim = rowpos * 8;
   2.337 +					} else {
   2.338 +						ydim = img.height();
   2.339 +					}
   2.340 +
   2.341 +					// Perform the Y resize if necessary, but also make Xdim=Xdim+1
   2.342 +					img.resize(xpos+1, ydim, 1, 1, 0, 0);
   2.343 +
   2.344 +					img(xpos, ydim/2) = 128;
   2.345 +
   2.346 +					// Now copy the image data...
   2.347 +					ypos = 0;
   2.348 +					for (unsigned int byte=0; byte<rowpos; byte++) {
   2.349 +						for (unsigned int bit=0; bit<8; bit++) {
   2.350 +							if (row[byte] & (0x80>>bit)) {
   2.351 +								img(xpos, ypos) = 0;
   2.352 +							} else {
   2.353 +								img(xpos, ypos) = 255;
   2.354 +							}
   2.355 +
   2.356 +							// Increment y-position
   2.357 +							ypos++;
   2.358 +						}
   2.359 +					}
   2.360 +
   2.361 +					// An entire row has been decoded. Increment x-position.
   2.362 +					xpos++;
   2.363 +					break;
   2.364 +
   2.365 +
   2.366  				default:
   2.367 -					printf("Unrecognised cmnd: 0x%02X\n", ch);
   2.368 +					printf("Unrecognised cmnd: 0x%02X [%c]\n", ch, ch);
   2.369  					break;
   2.370  			}
   2.371  		}