1305
inline void CCITTFaxStream::addPixels(int a1, int blackPixels) {
1306
if (a1 > codingLine[a0i]) {
1308
error(getPos(), "CCITTFax row is wrong length (%d)", a1);
1312
if ((a0i & 1) ^ blackPixels) {
1315
codingLine[a0i] = a1;
1319
inline void CCITTFaxStream::addPixelsNeg(int a1, int blackPixels) {
1320
if (a1 > codingLine[a0i]) {
1322
error(getPos(), "CCITTFax row is wrong length (%d)", a1);
1326
if ((a0i & 1) ^ blackPixels) {
1329
codingLine[a0i] = a1;
1330
} else if (a1 < codingLine[a0i]) {
1332
error(getPos(), "Invalid CCITTFax code");
1336
while (a0i > 0 && a1 <= codingLine[a0i - 1]) {
1339
codingLine[a0i] = a1;
1304
1343
int CCITTFaxStream::lookChar() {
1305
1344
short code1, code2, code3;
1345
int b1i, blackPixels, i, bits;
1311
// if at eof just return EOF
1312
if (eof && codingLine[a0] >= columns) {
1316
1352
// read the next row
1318
if (codingLine[a0] >= columns) {
1353
if (outputBits == 0) {
1355
// if at eof just return EOF
1320
1362
// 2-D encoding
1321
1363
if (nextLine2D) {
1323
// a0New = current position in coding line (0 <= a0New <= columns)
1324
// codingLine[a0] = last change in coding line
1325
// (black-to-white if a0 is even,
1326
// white-to-black if a0 is odd)
1327
// refLine[b1] = next change in reference line of opposite color
1330
// 0 <= codingLine[a0] <= a0New
1331
// <= refLine[b1] <= refLine[b1+1] <= columns
1332
// 0 <= a0 <= columns+1
1334
// refLine[n] = refLine[n+1] = columns
1335
// -- for some 1 <= n <= columns+1
1337
// 0 = codingLine[0] <= codingLine[1] < codingLine[2] < ...
1338
// < codingLine[n-1] < codingLine[n] = columns
1339
// -- where 1 <= n <= columns+1
1340
1364
for (i = 0; codingLine[i] < columns; ++i) {
1341
1365
refLine[i] = codingLine[i];
1343
refLine[i] = refLine[i + 1] = columns;
1345
a0New = codingLine[a0 = 0] = 0;
1367
refLine[i++] = columns;
1368
refLine[i] = columns;
1374
// refLine[b1i-1] <= codingLine[a0i] < refLine[b1i] < refLine[b1i+1]
1376
// exception at left edge:
1377
// codingLine[a0i = 0] = refLine[b1i = 0] = 0 is possible
1378
// exception at right edge:
1379
// refLine[b1i] = refLine[b1i+1] = columns is possible
1380
while (codingLine[a0i] < columns) {
1347
1381
code1 = getTwoDimCode();
1348
1382
switch (code1) {
1349
1383
case twoDimPass:
1350
if (refLine[b1] < columns) {
1351
a0New = refLine[b1 + 1];
1384
addPixels(refLine[b1i + 1], blackPixels);
1385
if (refLine[b1i + 1] < columns) {
1355
1389
case twoDimHoriz:
1356
if ((a0 & 1) == 0) {
1393
code1 += code3 = getBlackCode();
1394
} while (code3 >= 64);
1396
code2 += code3 = getWhiteCode();
1397
} while (code3 >= 64);
1359
1400
code1 += code3 = getWhiteCode();
1360
1401
} while (code3 >= 64);
1362
1403
code2 += code3 = getBlackCode();
1363
1404
} while (code3 >= 64);
1367
code1 += code3 = getBlackCode();
1368
} while (code3 >= 64);
1370
code2 += code3 = getWhiteCode();
1371
} while (code3 >= 64);
1373
if (code1 > 0 || code2 > 0) {
1374
if (a0New + code1 <= columns) {
1375
codingLine[a0 + 1] = a0New + code1;
1377
codingLine[a0 + 1] = columns;
1380
if (codingLine[a0] + code2 <= columns) {
1381
codingLine[a0 + 1] = codingLine[a0] + code2;
1383
codingLine[a0 + 1] = columns;
1386
a0New = codingLine[a0];
1387
while (refLine[b1] <= a0New && refLine[b1] < columns) {
1406
addPixels(codingLine[a0i] + code1, blackPixels);
1407
if (codingLine[a0i] < columns) {
1408
addPixels(codingLine[a0i] + code2, blackPixels ^ 1);
1410
while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
1415
addPixels(refLine[b1i] + 3, blackPixels);
1417
if (codingLine[a0i] < columns) {
1419
while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
1425
addPixels(refLine[b1i] + 2, blackPixels);
1427
if (codingLine[a0i] < columns) {
1429
while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
1435
addPixels(refLine[b1i] + 1, blackPixels);
1437
if (codingLine[a0i] < columns) {
1439
while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
1392
1444
case twoDimVert0:
1393
if (refLine[b1] < columns) {
1394
a0New = codingLine[++a0] = refLine[b1];
1396
while (refLine[b1] <= a0New && refLine[b1] < columns) {
1400
a0New = codingLine[++a0] = columns;
1404
if (refLine[b1] + 1 < columns) {
1405
a0New = codingLine[++a0] = refLine[b1] + 1;
1407
while (refLine[b1] <= a0New && refLine[b1] < columns) {
1411
a0New = codingLine[++a0] = columns;
1445
addPixels(refLine[b1i], blackPixels);
1447
if (codingLine[a0i] < columns) {
1449
while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
1455
addPixelsNeg(refLine[b1i] - 3, blackPixels);
1457
if (codingLine[a0i] < columns) {
1463
while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
1469
addPixelsNeg(refLine[b1i] - 2, blackPixels);
1471
if (codingLine[a0i] < columns) {
1477
while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
1414
1482
case twoDimVertL1:
1415
if (refLine[b1] - 1 > a0New || (a0 == 0 && refLine[b1] == 1)) {
1416
a0New = codingLine[++a0] = refLine[b1] - 1;
1418
while (refLine[b1] <= a0New && refLine[b1] < columns) {
1424
if (refLine[b1] + 2 < columns) {
1425
a0New = codingLine[++a0] = refLine[b1] + 2;
1427
while (refLine[b1] <= a0New && refLine[b1] < columns) {
1431
a0New = codingLine[++a0] = columns;
1435
if (refLine[b1] - 2 > a0New || (a0 == 0 && refLine[b1] == 2)) {
1436
a0New = codingLine[++a0] = refLine[b1] - 2;
1438
while (refLine[b1] <= a0New && refLine[b1] < columns) {
1444
if (refLine[b1] + 3 < columns) {
1445
a0New = codingLine[++a0] = refLine[b1] + 3;
1447
while (refLine[b1] <= a0New && refLine[b1] < columns) {
1451
a0New = codingLine[++a0] = columns;
1455
if (refLine[b1] - 3 > a0New || (a0 == 0 && refLine[b1] == 3)) {
1456
a0New = codingLine[++a0] = refLine[b1] - 3;
1458
while (refLine[b1] <= a0New && refLine[b1] < columns) {
1483
addPixelsNeg(refLine[b1i] - 1, blackPixels);
1485
if (codingLine[a0i] < columns) {
1491
while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
1497
addPixels(columns, 0);
1465
codingLine[a0 = 0] = columns;
1468
1501
error(getPos(), "Bad 2D code %04x in CCITTFax stream", code1);
1502
addPixels(columns, 0);
1472
} while (codingLine[a0] < columns);
1474
1508
// 1-D encoding
1476
codingLine[a0 = 0] = 0;
1513
while (codingLine[a0i] < columns) {
1480
code1 += code3 = getWhiteCode();
1481
} while (code3 >= 64);
1482
codingLine[a0+1] = codingLine[a0] + code1;
1484
if (codingLine[a0] >= columns) {
1489
code2 += code3 = getBlackCode();
1490
} while (code3 >= 64);
1491
codingLine[a0+1] = codingLine[a0] + code2;
1493
if (codingLine[a0] >= columns) {
1499
if (codingLine[a0] != columns) {
1500
error(getPos(), "CCITTFax row is wrong length (%d)", codingLine[a0]);
1501
// force the row to be the correct length
1502
while (codingLine[a0] > columns) {
1505
codingLine[++a0] = columns;
1517
code1 += code3 = getBlackCode();
1518
} while (code3 >= 64);
1521
code1 += code3 = getWhiteCode();
1522
} while (code3 >= 64);
1524
addPixels(codingLine[a0i] + code1, blackPixels);
1509
1529
// byte-align the row