Coverage Report

Created: 2025-04-22 06:20

/src/libspectre/ghostscript/devices/gdevmiff.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2020 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
13
   CA 94945, U.S.A., +1(415)492-9861, for further information.
14
*/
15
16
/* MIFF (Magick Image File Format - native for ImageMagick) file format driver */
17
#include "gdevprn.h"
18
19
/* ------ The device descriptor ------ */
20
21
/*
22
 * Default X and Y resolution.
23
 */
24
#define X_DPI 72
25
#define Y_DPI 72
26
27
static dev_proc_print_page(miff24_print_page);
28
29
/* Since the print_page doesn't alter the device, this device can print in the background */
30
static const gx_device_procs miff24_procs =
31
prn_color_procs(gdev_prn_open, gdev_prn_bg_output_page, gdev_prn_close,
32
                gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb);
33
const gx_device_printer gs_miff24_device =
34
prn_device(miff24_procs, "miff24",
35
           DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
36
           X_DPI, Y_DPI,
37
           0, 0, 0, 0,    /* margins */
38
           24, miff24_print_page);
39
40
/* Print one page in 24-bit RLE direct color format. */
41
static int
42
miff24_print_page(gx_device_printer * pdev, gp_file * file)
43
0
{
44
0
    int raster = gx_device_raster((gx_device *) pdev, true);
45
0
    byte *line = gs_alloc_bytes(pdev->memory, raster, "miff line buffer");
46
0
    int y;
47
0
    int code = 0;   /* return code */
48
49
0
    if (line == 0)   /* can't allocate line buffer */
50
0
        return_error(gs_error_VMerror);
51
0
    gp_fputs("id=ImageMagick\n", file);
52
0
    gp_fputs("class=DirectClass\n", file);
53
0
    gp_fprintf(file, "columns=%d\n", pdev->width);
54
0
    gp_fputs("compression=RunlengthEncoded\n", file);
55
0
    gp_fprintf(file, "rows=%d\n", pdev->height);
56
0
    gp_fputs(":\n", file);
57
0
    for (y = 0; y < pdev->height; ++y) {
58
0
        byte *row;
59
0
        byte *end;
60
61
0
        code = gdev_prn_get_bits(pdev, y, line, &row);
62
0
        if (code < 0)
63
0
            break;
64
0
        end = row + pdev->width * 3;
65
0
        while (row < end) {
66
0
            int count = 0;
67
68
0
            while (count < 255 && row < end - 3 &&
69
0
                   row[0] == row[3] && row[1] == row[4] &&
70
0
                   row[2] == row[5]
71
0
                )
72
0
                ++count, row += 3;
73
0
            gp_fputc(row[0], file);
74
0
            gp_fputc(row[1], file);
75
0
            gp_fputc(row[2], file);
76
0
            gp_fputc(count, file);
77
0
            row += 3;
78
0
        }
79
0
    }
80
0
    gs_free_object(pdev->memory, line, "miff line buffer");
81
82
0
    return code;
83
0
}