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