Coverage Report

Created: 2025-04-22 06:20

/src/libspectre/ghostscript/contrib/lips4/gdevlips.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 1998, 1999 Norihito Ohmori.
2
3
   Ghostscript printer driver
4
   for Canon LBP and BJ printers (LIPS II+/III/IVc/IV)
5
6
   This software is distributed in the hope that it will be useful, but
7
   WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
8
   to anyone for the consequences of using it or for whether it serves any
9
   particular purpose or works at all, unless he says so in writing.  Refer
10
   to the GNU General Public License for full details.
11
12
   Everyone is granted permission to copy, modify and redistribute
13
   this software, but only under the conditions described in the GNU
14
   General Public License.  A copy of this license is supposed to have been
15
   given to you along with this software so you can know your rights and
16
   responsibilities.  It should be in a file named COPYING.  Among other
17
   things, the copyright notice and this notice must be preserved on all
18
   copies.
19
 */
20
21
/*$Id: gdevlips.c,v 1.3 2002/07/20 21:03:21 tillkamppeter Exp $ */
22
/* Common Utility for LIPS driver */
23
24
#include "gx.h"
25
#include "gdevlips.h"
26
27
typedef struct {
28
    int width;
29
    int height;
30
    int num_unit;
31
} paper_table;
32
33
static paper_table lips_paper_table[] =
34
{
35
    {842, 1190, 12},    /* A3 */
36
    {595, 842, 14},   /* A4 */
37
    {597, 842, 14},   /* A4     8.3 inch x 11.7 inch */
38
    {421, 595, 16},   /* A5 */
39
    {284, 419, 18},   /* PostCard */
40
    {729, 1032, 24},    /* JIS B4 */
41
    {516, 729, 26},   /* JIS B5 */
42
    {363, 516, 28},   /* JIS B6 */
43
    {612, 792, 30},   /* Letter */
44
    {612, 1008, 32},    /* Legal */
45
    {792, 1224, 34},    /* Ledger */
46
    {522, 756, 40},   /* Executive */
47
    {298, 666, 50},   /* envyou4 */
48
    {0, 0, 80},     /* User Size */
49
};
50
51
int
52
lips_media_selection(int width, int height)
53
0
{
54
0
    int landscape = 0;
55
0
    int tmp;
56
0
    paper_table *pt;
57
58
0
    if (width > height) {
59
0
        landscape = 1;
60
0
        tmp = width;
61
0
        width = height;
62
0
        height = tmp;
63
0
    }
64
0
    for (pt = lips_paper_table; pt->num_unit < 80; pt++)
65
0
        if (pt->width == width && pt->height == height)
66
0
            break;
67
68
0
    return pt->num_unit + landscape;
69
0
}
70
71
static int GetNumSameData(const byte * curPtr, const int maxnum);
72
static int GetNumWrongData(const byte * curPtr, const int maxnum);
73
74
/* This routine talkes from LipsIVRunLengthEncode in Yoshiharu ITO's patch  */
75
int
76
lips_packbits_encode(byte * inBuff, byte * outBuff, int Length)
77
0
{
78
0
    int size = 0;
79
80
0
    while (Length) {
81
0
        int count;
82
83
0
        if (1 < (count = GetNumSameData(inBuff,
84
0
                                        Length > 128 ? 128 : Length))) {
85
0
            Length -= count;
86
0
            size += 2;
87
88
0
            *outBuff++ = -(count - 1);
89
0
            *outBuff++ = *inBuff;
90
0
            inBuff += count;
91
0
        } else {
92
0
            count = GetNumWrongData(inBuff, Length > 128 ? 128 : Length);
93
0
            Length -= count;
94
0
            size += count + 1;
95
96
0
            *outBuff++ = count - 1;
97
0
            while (count--) {
98
0
                *outBuff++ = *inBuff++;
99
0
            }
100
0
        }
101
0
    }
102
103
0
    return (size);
104
0
}
105
106
/* LIPS III printer can no use Runlength encode mode
107
   but Mode 1-3 Format mode. */
108
/* This routine takes from LipsIIIRunLengthEncode in Yoshiharu ITO's patch  */
109
int
110
lips_mode3format_encode(byte * inBuff, byte * outBuff, int Length)
111
0
{
112
0
    int size = 0;
113
114
0
    while (Length) {
115
0
        int count;
116
117
0
        if (1 < (count = GetNumSameData(inBuff,
118
0
                                        Length > 257 ? 257 : Length))) {
119
0
            Length -= count;
120
0
            size += 3;
121
122
0
            *outBuff++ = *inBuff;
123
0
            *outBuff++ = *inBuff;
124
0
            *outBuff++ = count - 2;
125
0
            inBuff += count;
126
0
        } else {
127
0
            count = GetNumWrongData(inBuff, Length);
128
0
            Length -= count;
129
0
            size += count;
130
131
0
            while (count--) {
132
0
                *outBuff++ = *inBuff++;
133
0
            }
134
0
        }
135
0
    }
136
137
0
    return (size);
138
0
}
139
140
static int
141
GetNumSameData(const byte * curPtr, const int maxnum)
142
0
{
143
0
    int count = 1;
144
145
0
    if (1 == maxnum) {
146
0
        return (1);
147
0
    }
148
0
    while (maxnum > count && *curPtr == *(curPtr + count)) {
149
0
        count++;
150
0
    }
151
152
0
    return (count);
153
0
}
154
155
static int
156
GetNumWrongData(const byte * curPtr, const int maxnum)
157
0
{
158
0
    int count = 0;
159
160
0
    if (1 == maxnum) {
161
0
        return (1);
162
0
    }
163
0
    while (maxnum > count+1 && *(curPtr + count) != *(curPtr + count + 1)) {
164
0
        count++;
165
0
    }
166
167
0
    return (count);
168
0
}
169
170
/*
171
172
   This routine takes from gdevlips4-1.1.0.
173
174
 */
175
0
#define RLECOUNTMAX 0xff  /* 256 times */
176
int
177
lips_rle_encode(byte * inBuff, byte * outBuff, int Length)
178
0
{
179
0
    int i = 0;
180
0
    byte value;
181
0
    int count = 0;
182
0
    byte *ptr = inBuff;
183
184
0
    value = *ptr;
185
0
    ptr++;
186
187
0
    while (ptr < inBuff + Length) {
188
0
        if (*ptr == value) {
189
0
            count++;
190
0
            if (count > RLECOUNTMAX) {
191
0
                *outBuff++ = RLECOUNTMAX;
192
0
                *outBuff++ = value;
193
0
                i += 2;
194
0
                count = 0;
195
0
            }
196
0
        } else {
197
0
            *outBuff++ = count;
198
0
            *outBuff++ = value;
199
0
            i += 2;
200
0
            count = 0;
201
0
            value = *ptr;
202
0
        }
203
0
        ptr++;
204
0
    }
205
0
    *outBuff++ = count;
206
0
    *outBuff++ = value;
207
0
    i += 2;
208
209
0
    return i;
210
0
}