Codebase list splix / run/f9930058-0a62-4e31-b355-fba291a69d22/main src / algo0x0d.cpp
run/f9930058-0a62-4e31-b355-fba291a69d22/main

Tree @run/f9930058-0a62-4e31-b355-fba291a69d22/main (Download .tar.gz)

algo0x0d.cpp @run/f9930058-0a62-4e31-b355-fba291a69d22/mainraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
 *         algo0x0d.cpp   SpliX is Copyright 2006-2008 by Aurélien Croc
 *                        This file is a SpliX derivative work
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the
 *  Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * $Id: algo0x0d.cpp 301 2012-02-29 14:11:09Z tillkamppeter $
 *
 * --
 * This code is written by Leonardo Hamada
 */
#include "algo0x0d.h"
#include <new>
#include <string.h>
#include "errlog.h"
#include "request.h"
#include "printer.h"
#include "bandplane.h"



/*
 * Constructor.
 */
Algo0x0D::Algo0x0D()
{
}

Algo0x0D::~Algo0x0D()
{
}



/*
 * Method for two-byte packet encoding.
 */
inline void Algo0x0D::writeTwoBytesPacket( unsigned char * output,
                            unsigned long & outputSize,
                            long int accumulatedHorizontalOffsetValue,
                            long int accumulatedVerticalOffsetValue,
                            unsigned long accumulatedRunCount )
{
    /* Encodes the run count and vertical displacement in the first byte. */
    output[ outputSize++ ] =
        ( unsigned char )( ( accumulatedVerticalOffsetValue << 6 ) |
	                     accumulatedRunCount );

    /* Encodes the offset value in the second byte. This is signed. */
    output[ outputSize++ ] =
        ( unsigned char )( 0xFF & accumulatedHorizontalOffsetValue );
}



/*
 * Method for four-byte packet encoding.
 * Encodes the pen offset value as a 14-bit signed value in the first two bytes.
 */
inline void Algo0x0D::writeFourBytesPacket( unsigned char * output,
                            unsigned long & outputSize,
                            long int accumulatedHorizontalOffsetValue,
                            long int accumulatedVerticalOffsetValue,
                            unsigned long accumulatedRunCount )
{
    /* Encodes the upper 6-bit value of the offset value. */
    output[ outputSize++ ] = 0x80 |
    ( unsigned char )( ( 0x00003F00 & accumulatedHorizontalOffsetValue ) >> 8 );

    /* Encode the lower 8-bit value of the offset value. */
    output[ outputSize++ ] =
            ( unsigned char )( 0x000000FF & accumulatedHorizontalOffsetValue );

    /* Encode the run count as a unsigned 12-bit value in the third byte. */
    output[ outputSize++ ] = 0x80 |
            ( unsigned char )( ( accumulatedVerticalOffsetValue << 4 ) |
                               ( accumulatedRunCount >> 8 ) );
    /* Encode the last byte of run count. */
    output[ outputSize++ ] =
            ( unsigned char )( 0x000000FF & accumulatedRunCount );
}



/*
 * Method for six-byte packet encoding.
 * Encodes the offset value as a 24-bit unsigned value in the second,
 * third and fourth bytes.
 */
inline void Algo0x0D::writeSixBytesPacket( unsigned char * output,
                            unsigned long & outputSize,
                            long int accumulatedCombinedOffsetValue,
                            unsigned long accumulatedRunCount )
{
    /* Write the packet header constant in the first byte. */
    output[ outputSize++ ] = 0xC0;

    /* Encodes the upper 8-bit value of the 24-bit offset value. */
    output[ outputSize++ ] =
                ( unsigned char )( ( 0x00FF0000 & 
                                     accumulatedCombinedOffsetValue ) >> 16 );

    /* Encodes the middle 8-bit value of the 24-bit offset value. */
    output[ outputSize++ ] =
                ( unsigned char )( ( 0x0000FF00 &
                                     accumulatedCombinedOffsetValue ) >> 8 );

    /* Encodes the remaining 8-bit value of the 24-bit offset value. */
    output[ outputSize++ ] =
                ( unsigned char )( 0x000000FF &
                                   accumulatedCombinedOffsetValue );

    /* Encodes the run counts as unsigned 14-bit value in the fifth and
       sixth byte. */

    /* Encodes the run counts in upper 6-bits. */
    output[ outputSize++ ] = 0xC0 |
                ( unsigned char )( accumulatedRunCount >> 8 );

    /* Encodes the run count lower 8-bits value. */
    output[ outputSize++ ] =
                ( unsigned char )( 0x000000FF & accumulatedRunCount );
}



/*
 *  Modifications: Jun-29-2008, Jul-08-2008, Oct-03-2009, Oct-04-2009.
 */
inline void Algo0x0D::selectPacketSize(
                            unsigned char * output,
                            unsigned long & outputSize,
                            unsigned long preAccumulatedHorizontalOffsetValue,
                            unsigned long accumulatedHorizontalOffsetValue,
                            unsigned long currentHorizontalPenPosition,
                            unsigned long accumulatedRunCount,
                            unsigned long consecutiveBlankScanLines,
                            unsigned long currentVerticalPenPosition,
                            const unsigned long wrapWidth )
{
    /* Set the initial vertical offset value. */
    long int verticalOffsetValue = consecutiveBlankScanLines;

    /* Set the initial horizontal offset value. */
    long int horizontalOffsetValue = accumulatedHorizontalOffsetValue;

    /* Verify if this is the first formed packet of the scan-line
       and that it is not the first top-most scan-line of the given band.
       Can be verified because on every beginning of a scan-line work, the 
       pre-accumulated horizontal offset value is zero. */
    if ( ( 0 == preAccumulatedHorizontalOffsetValue ) &&
                                ( 0 < currentVerticalPenPosition ) ) {
        /* Evaluate pixel distance between previous and current pen position
           to find the relative horizontal offset value. */
        horizontalOffsetValue -= currentHorizontalPenPosition;

        /* Adjust by +1, when any of the previous scan-lines is not blank.
           Must account for three cases:
             A - When the band begins with any number of blank scan-lines.
             B - When the current non-blank scan-line follow a blank scan-line,
                 but there are non-blank scan-lines previously.
	     C - When the previous scan-line was not blank.
           Notice that when any of the previous scan-lines is non-blank,
           the blank scan-line counter is always less than and not equal to
           the value of the vertical pen position. */
        if ( consecutiveBlankScanLines < currentVerticalPenPosition ) {
            verticalOffsetValue++;
        }

    } else {

        /* Process a sequential packet for current scan-line.
           The pre-accumulated offset value must be added, this was the 
           previous packet's run count value. */
        horizontalOffsetValue += preAccumulatedHorizontalOffsetValue;

    }

    /* Choosing the packet size. */
    if ( ( 127 >= horizontalOffsetValue )
                                && ( -128 <= horizontalOffsetValue ) 
                                && ( 63 >= accumulatedRunCount )
                                && ( 1 >= verticalOffsetValue ) ) {

	/* Issue an encoded 2-byte packet. */
        writeTwoBytesPacket( output, outputSize,
                                horizontalOffsetValue,
                                verticalOffsetValue,
                                accumulatedRunCount );

    } else if ( ( 8191 >= horizontalOffsetValue )
                                && ( -8192 <= horizontalOffsetValue )
                                && ( 4095 >= accumulatedRunCount )
                                && ( 3 >= verticalOffsetValue ) ) {

	/* Issue an encoded 4-byte packet. */
        writeFourBytesPacket( output, outputSize,
                                horizontalOffsetValue,
                                verticalOffsetValue,
                                accumulatedRunCount );

    } else {

        /* Issue an encoded 6-byte packet. */ 
        writeSixBytesPacket( output, outputSize, 
			        wrapWidth * verticalOffsetValue
                                + horizontalOffsetValue,
                                accumulatedRunCount );

    }

    /* Finished one packet encoding. */
}



/*
 * Main algorithm 0xd encoder.
 */
BandPlane * Algo0x0D::compress(const Request & request, unsigned char *data,
        unsigned long width, unsigned long height)
{
    /* Basic parameters validation. */
    if ( !data || !height || !width ) {
        ERRORMSG(_("Invalid given data for compression: 0xd"));
        return NULL;
    }

    /* We will interpret the band heigth of 128 pixels as 600 DPI printing
     request. Likewise, height of 64 pixels as 300 DPI printing. */
    if ( ! ( 128 == height || 64 == height ) ) {
        ERRORMSG(_("Invalid band height for compression: 0xd"));
        return NULL;
    }

    /* Set the hardware wrapping width for six-byte type packet format. */
    const unsigned long wrapWidth = ( 64 == height ) ? 0x09A0 : 0x1360;

    /* These are the limits that an encoded scan-line is the allowed to produce
     until encoding is given up. 250 bytes for 300 DPI, 122 bytes for 600 DPI.*/
    const unsigned long maxEncodedBytesPerScanLine = ( 64 == height ) ?
        250 : 122;

    /* Estimate a output buffer size limit equal to 256 bytes times the bitmap
     height for 300 DPI printing, and 128 bytes times the bitmap height for
     600 DPI printing. */
    const unsigned long maximumBufferSize = ( 64 == height ) ?
        256 * height + 4: 128 * height + 4;

    /* Keep track of the size of encoded data. */
    unsigned long outputSize = 0;

    /* Encoded data size of current scan-line. */
    unsigned long encodedScanLineSize = 0;

    /* Create the output buffer for work. */
    unsigned char * output = NULL;

    try {
        output = new unsigned char[ maximumBufferSize ];
    } catch( std::bad_alloc & ) {
        ERRORMSG(_("Could not allocate work buffer for compression: 0xd"));
        return NULL;
    }

    /* Mask to track the bits in raw data-byte that is being processed. */
    unsigned char bitMask = 0x80;

    /* Accumulation of the number of (black) pixel runs. */
    unsigned long accumulatedRunCount = 0;

    /* Accumulation of horizontal offset value in pixels. */
    unsigned long accumulatedHorizontalOffsetValue = 0;

    /* Index for the raw data byte in the current scanline. */
    unsigned long rowByteIndex = 0;

    /* Current absolute horizontal pen position. */
    unsigned long currentHorizontalPenPosition = 0;

    /* Run counts are accounted for as an offset after each packet encodings. */
    unsigned long preAccumulatedHorizontalOffsetValue = 0;

    /* Current absolute vertical pen position. */
    unsigned long currentVerticalPenPosition = 0;

    /* Number of row-bytes in the bitmap. */
    const unsigned long rowBytes = ( width + 7 ) / 8;

    /* This is the working and therefore the effective printing width.
    Crop the working width if bitmap is longer than wrap width. */
    const unsigned long workWidth = ( width > wrapWidth ) ? wrapWidth : width;

    /* Working width row-bytes. How many bytes need to store a working width
     number of pixels. */
    const unsigned long workWidthRowBytes = ( workWidth + 7 ) / 8;

    /* Number of pixels left to be processed in the scanline. */
    unsigned long pixelsLeftInScanline = workWidth;

    /* Number of acumulated consecutive blank scanline. */
    unsigned long consecutiveBlankScanLines = 0;

    /* Main encoding loop. */
    while ( currentVerticalPenPosition < height ) {

        /* Scan for offset value. */
        while ( rowByteIndex < workWidthRowBytes ) {

            /* Check current byte data against the mask for a unset bit. */
            if ( ( 0 == ( bitMask & data[ rowByteIndex ] ) ) &&
                                                ( pixelsLeftInScanline > 0 ) ) {
                /* Account for a blank pixel. */
                accumulatedHorizontalOffsetValue++;

                /* Make a discount for previous processed pixel. */
                pixelsLeftInScanline--;
            } else {
                /* Exit current loop for scanning of offset values. */
                break;
            }

            /* Rotating the bit mask. */
            bitMask >>= 1;

            /* Reset the mask if needed. */
            if ( 0 == bitMask ) {
                bitMask = 0x80;

                /* Advance the row byte index. */
                rowByteIndex++;
            }
        }

        /* Now, scan for run count. */
        while ( rowByteIndex < workWidthRowBytes ) {

            /* Check byte against the mask for an one-bit (set) value. */
            if ( ( 0 != ( bitMask & data[ rowByteIndex ] ) ) &&
                                            ( pixelsLeftInScanline > 0 ) ) {
                /* Account for a black pixel. */
                accumulatedRunCount++;

                /* Make a discount for previous processed pixel. */
                pixelsLeftInScanline--;
            } else {
                /* Exit current loop for scanning of run count. */
                break;
            }

            /* Rotating the bit mask. */
            bitMask >>= 1;

            /* Reset the mask if needed. */
            if ( 0 == bitMask ) {
                bitMask = 0x80;

                /* Advance the row byte index. */
                rowByteIndex++;
            }
        }

        /* We have an offset value and a run count pair. */
        /* Verify if it's a blank scanline before proceeding. */
        if ( ( accumulatedHorizontalOffsetValue == workWidth ) &&
                                            ( 0 == accumulatedRunCount ) ) {
            /* We encountered a blank scanline, so account for it. */
            consecutiveBlankScanLines++;
        } else if ( 0 < accumulatedRunCount ) {

            /* We have pixels to encode, proceed. */
            unsigned long previousOutputSize = outputSize;

           if ( outputSize + 6 + 4  <= maximumBufferSize ) {
                selectPacketSize( output, outputSize,
                        preAccumulatedHorizontalOffsetValue,
                        accumulatedHorizontalOffsetValue,
                        currentHorizontalPenPosition,
                        accumulatedRunCount, consecutiveBlankScanLines,
                        currentVerticalPenPosition, wrapWidth );
            } else {
                /* Here we failed. Unlikely. */
                ERRORMSG(_("Out of buffer space: 0xd"));
                delete [] output;
                return NULL;
            }

            encodedScanLineSize += ( outputSize - previousOutputSize );

            if ( maxEncodedBytesPerScanLine < encodedScanLineSize ) {
                /* We did not fail, but gave up because data is unsuited for
                 encoding by this algorithm. */
                delete [] output;
                return NULL;
            }

            /* After encoding, reset the blank scan-line counter. */
            consecutiveBlankScanLines = 0;

            /* Update the pen position. This is one way of doing it. */
            currentHorizontalPenPosition =
                        workWidth - pixelsLeftInScanline - accumulatedRunCount;

            /* Must pre-accumulate the offset value. */
            preAccumulatedHorizontalOffsetValue = accumulatedRunCount;
        }

        if ( 0 == pixelsLeftInScanline ) {
            /* Advance the vertical pen position. */
            if ( ++currentVerticalPenPosition < height ) {
		/* No more pixels left in this scan-line, so go to the next one. */
		data = & data[ rowBytes ];
	    }

            /* Re-initialize the encoded scan-line data size tracker to zero. */
            encodedScanLineSize = 0;

            /* Reset control variables to initial values. */
            rowByteIndex = 0;

            /* Reset the bit mask. */
            bitMask = 0x80;

            /* Re-init the working width. */
            pixelsLeftInScanline = workWidth;

            /* Reset the pre-accumulated offset value at each scan-line done. */
            preAccumulatedHorizontalOffsetValue = 0;
        }

        /* Always reset the run count and the offset value in every pass. */
        accumulatedRunCount = 0;
        accumulatedHorizontalOffsetValue = 0;
    }

    /* Zero value byte padding for data size alignment to 4-byte boundary. */
    unsigned long zerosPad = 4 - ( outputSize % 4 );

    /* Pad anyway even if already aligned. */
    if ( outputSize + zerosPad <= maximumBufferSize ) {
        while ( zerosPad-- ) {
            output[ outputSize++ ] = 0;
        }
    } else {
        /* Here we failed. Unlikely. */
        ERRORMSG(_("No buffer during padding: 0xd"));
        delete [] output;
        return NULL;
    }

    /* Prepare to return data encoded by algorithm 0xd. */
    BandPlane * plane = new BandPlane();
    
    plane->setData( output, outputSize );
    plane->setEndian( BandPlane::Dependant );
    plane->setCompression( 0xd );

    /* Finished this band encoding. */
    DEBUGMSG(_("Finished band encoding: type=0xd, size=%lu"), outputSize);

    return plane;
}