cab CUPS Treiber  ---
image.c
Go to the documentation of this file.
1 /******************************************************************************
2  * cab Produkttechnik GmbH & Co KG
3  * Entwicklung Etikettendrucker
4  *
5  * cab CUPS Linux Driver, image.c
6  * - Functions for writing to a Image file
7  *
8  * Copyright (c) 2004-2020 cab Produkttechnik GmbH, Germany
9  *
10  * Licensed under Apache License v2.0. See the file "LICENSE" for more
11  * information.
12  *
13  */
14 
15 /*****************************************************************************/
16 
17 #include <string.h>
18 
19 #include "image.h"
20 #include "rastertocab.h"
21 
22 /*****************************************************************************/
23 /*---------------------------------------------------------------------------*/
34 /*---------------------------------------------------------------------------*/
35 /* Note: */
36 /* */
37 /*---------------------------------------------------------------------------*/
38 static void write_word(unsigned char *dest, unsigned short value) {
39  *dest=value>>8;
40  *(dest+1)=value&255;
41 }
42 
43 /*****************************************************************************/
44 /*---------------------------------------------------------------------------*/
56 /*---------------------------------------------------------------------------*/
57 /* Note: */
58 /* */
59 /*---------------------------------------------------------------------------*/
60 void write_img_header (FILE *oFile, unsigned int xdim, unsigned int ydim) {
61  imghead_t img;
62 
63  /* Bildkopf schreiben */
64  write_word(img.version, 1);
65  write_word(img.length, sizeof(imghead_t) / 2);
66  write_word(img.planes, 1);
67  write_word(img.pattlen, 1);
68  write_word(img.width, 1000.0 / DPMM);
69  write_word(img.height, 1000.0 / DPMM);
70  write_word(img.pixels, xdim);
71  write_word(img.lines, ydim);
72 
73  fwrite(&img, sizeof(img), 1, oFile);
74 }
75 
76 
77 
78 /*****************************************************************************/
79 /*---------------------------------------------------------------------------*/
90 /*---------------------------------------------------------------------------*/
91 /* Note: */
92 /* */
93 /*---------------------------------------------------------------------------*/
94 void write_img (pictogram_t *pp, FILE *fp) {
95  int i, bwide, wwide, vcount;
96  int zl, pattlen;
97  char *cp, *cp2;
98  short *zeile1, *zeile2;
99 
100  bwide = (pp->breite + 7) >> 3;
101  wwide = (pp->breite + 15) >> 4;
102  zl = 0;
103  vcount = 1;
104 
105  while (zl < pp->hoehe) {
106  zeile1 = pp->inh + (long)zl * (long)wwide;
107  if(zl != pp->hoehe - 1) {
108  /* Check if we can use vertical replicant count */
109  vcount = 0;
110 
111  for(;;) {
112  zeile2 = pp->inh + (long)(zl + 1) * (long)wwide;
113  if(memcmp(zeile1, zeile2, bwide))
114  break;
115  zl++;
116  if(!vcount) {
117  vcount++;
118  }
119  vcount++;
120  if(vcount == 255) {
121  break;
122  }
123  if(zl == pp->hoehe - 1) {
124  break;
125  }
126  }
127 
128  if(vcount) {
129  /* Yes, identical lines, write vrc */
130  fputc(0, fp);
131  fputc(0, fp);
132  fputc(255, fp);
133  fputc(vcount, fp);
134  }
135  }
136  /* Code one line */
137  cp = (char *)zeile1;
138  i = bwide;
139 
140  while(i) {
141  pattlen = 1;
142  i--;
143  if(*cp == '\0') {
144  /* Pruefen, ob lauter 0 */
145  cp++;
146 
147  for(;;) {
148  if(!i) {
149  break;
150  }
151  if(*cp) {
152  break;
153  }
154  cp++;
155  i--;
156  pattlen++;
157  if(pattlen == 127) {
158  break;
159  }
160  }
161  fputc(pattlen, fp);
162  }
163  else if(*cp == (char)255) {
164  /* Check if several 0xFF */
165  cp++;
166 
167  for(;;) {
168  if(!i) {
169  break;
170  }
171  if(*cp != (char)255) {
172  break;
173  }
174  cp++;
175  i--;
176  pattlen++;
177  if(pattlen == 127) {
178  break;
179  }
180  }
181 
182  pattlen += 128;
183  fputc(pattlen, fp);
184  }
185  else {
186  /* Stream of pixels */
187  cp2 = cp; /* Store start position */
188  cp++;
189 
190  for(;;) {
191  if(!i) {
192  break;
193  }
194  if(i<4) {
195  /* Don't abort at end of line */
196  pattlen += i;
197  i = 0;
198  break;
199  }
200  if(*cp == '\0' && *(cp + 1) == '\0' && *(cp + 2) == '\0') {
201  /* Found several 0x00 */
202  break;
203  }
204  if(*cp==(char)255 && *(cp+1)==(char)255 && *(cp+2)==(char)255) {
205  /* Found several 0xFF */
206  break;
207  }
208  pattlen++;
209  i--;
210  cp++;
211  if(pattlen == 252) {
212  break;
213  }
214  if(!i) {
215  break;
216  }
217  }
218 
219  /* Write pattern */
220  fputc(128, fp);
221  fputc(pattlen, fp);
222 
223  while(pattlen--) {
224  fputc(*cp2++, fp);
225  }
226  }
227  }
228  zl++;
229  }
230  /* fclose(fp); */
231  return;
232 }
unsigned char planes[2]
1 for monochrome and 4 for 16-col files
Definition: image.h:30
void write_img_header(FILE *oFile, unsigned int xdim, unsigned int ydim)
Creates and writes the image header.
Definition: image.c:60
#define DPMM
Dots per mm.
Definition: image.h:24
static void write_word(unsigned char *dest, unsigned short value)
Writes a number as a char ...
Definition: image.c:38
unsigned char pattlen[2]
length in bytes of a pattern
Definition: image.h:31
Internal struct to store basic informations for the image header.
Definition: image.h:27
unsigned char lines[2]
Image height - lines per image.
Definition: image.h:35
short * inh
pointer to bitmap
Definition: image.h:43
unsigned char version[2]
Is normally 1.
Definition: image.h:28
unsigned char width[2]
Width of a pixel in tenths of a millimetre.
Definition: image.h:32
short breite
width in pixels (16 pixel steps!)
Definition: image.h:41
Internal struct to store informations for images.
Definition: image.h:39
unsigned char length[2]
Header size.
Definition: image.h:29
unsigned char height[2]
Height of a pixel in tenths of a millimetre.
Definition: image.h:33
void write_img(pictogram_t *pp, FILE *fp)
Definition: image.c:94
unsigned char pixels[2]
Image width - pixles per line.
Definition: image.h:34
short hoehe
height in pixels
Definition: image.h:42