| 1428 | } |
| 1429 | |
| 1430 | static void psl_stream_dump (struct PSL_CTRL *PSL, unsigned char *buffer, int nx, int ny, int nbits, int compress, int encode, int mask) { |
| 1431 | /* Writes a stream of bytes in ascii85 or hex, performs RGB to CMYK |
| 1432 | * conversion and compression. |
| 1433 | * buffer = stream of bytes |
| 1434 | * nx, ny = image dimensions in pixels |
| 1435 | * nbits = depth of image pixels in bits |
| 1436 | * compress = no (0), rle (1), lzw (2), or deflate (3) compression |
| 1437 | * encode = ascii85 (0) or hex (1) |
| 1438 | * mask = image (0), imagemask (1), or neither (2) |
| 1439 | */ |
| 1440 | size_t nbytes, i; |
| 1441 | unsigned line_length = 0; |
| 1442 | unsigned char *buffer1 = NULL, *buffer2 = NULL; |
| 1443 | const char *kind_compress[] = {"", "/RunLengthDecode filter", "/LZWDecode filter", "/FlateDecode filter"}; |
| 1444 | const char *kind_mask[] = {"image", "imagemask"}; |
| 1445 | |
| 1446 | nx = abs (nx); |
| 1447 | nbytes = ((size_t)nbits * (size_t)nx + 7) / (size_t)8 * (size_t)ny; |
| 1448 | |
| 1449 | /* Transform RGB stream to CMYK or Gray stream */ |
| 1450 | if (PSL->internal.color_mode == PSL_CMYK && nbits == 24) |
| 1451 | buffer1 = psl_cmyk_encode (PSL, &nbytes, buffer); |
| 1452 | else if (PSL->internal.color_mode == PSL_GRAY && nbits == 24) |
| 1453 | buffer1 = psl_gray_encode (PSL, &nbytes, buffer); |
| 1454 | else |
| 1455 | buffer1 = buffer; |
| 1456 | |
| 1457 | /* Perform selected compression method */ |
| 1458 | if (compress == PSL_RLE) |
| 1459 | buffer2 = psl_rle_encode (PSL, &nbytes, buffer1); |
| 1460 | else if (compress == PSL_LZW) |
| 1461 | buffer2 = psl_lzw_encode (PSL, &nbytes, buffer1); |
| 1462 | else if (compress == PSL_DEFLATE) |
| 1463 | buffer2 = psl_deflate_encode (PSL, &nbytes, buffer1); |
| 1464 | else |
| 1465 | buffer2 = NULL; |
| 1466 | |
| 1467 | if (!buffer2) { /* If compression failed, or no compression requested */ |
| 1468 | compress = PSL_NONE; |
| 1469 | buffer2 = buffer1; |
| 1470 | } |
| 1471 | |
| 1472 | /* Output image dictionary */ |
| 1473 | if (mask < 2) { |
| 1474 | PSL_command (PSL, "/Width %d /Height %d /BitsPerComponent %d\n", nx, ny, MIN(nbits,8)); |
| 1475 | PSL_command (PSL, " /ImageMatrix [%d 0 0 %d 0 %d] /DataSource currentfile", nx, -ny, ny); |
| 1476 | if (encode == PSL_ASCII85) PSL_command (PSL, " /ASCII85Decode filter"); |
| 1477 | if (compress) PSL_command (PSL, " %s", kind_compress[compress]); |
| 1478 | PSL_command (PSL, "\n>> %s\n", kind_mask[mask]); |
| 1479 | } |
| 1480 | if (encode == PSL_ASCII85) { |
| 1481 | /* Convert 4-tuples to ASCII85 5-tuples and write buffer to file */ |
| 1482 | psl_a85_encode (PSL, buffer2, nbytes); |
| 1483 | } |
| 1484 | else { |
| 1485 | /* Regular hexadecimal encoding */ |
| 1486 | for (i = 0; i < nbytes; i++) { |
| 1487 | PSL_command (PSL, "%02X", buffer2[i]); line_length += 2; |
no test coverage detected