1 /*
2    Copyright (c) 2001-2002 Andrew Bird  All rights reserved.
3    Distributed by Free Software Foundation, Inc.
4 
5 This file is part of HP2xx.
6 
7 HP2xx is distributed in the hope that it will be useful, but
8 WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
9 to anyone for the consequences of using it or for whether it serves any
10 particular purpose or works at all, unless he says so in writing.  Refer
11 to the GNU General Public License, Version 2 or later, for full details.
12 
13 Everyone is granted permission to copy, modify and redistribute
14 HP2xx, but only under the conditions described in the GNU General Public
15 License.  A copy of this license is supposed to have been
16 given to you along with HP2xx so you can know your rights and
17 responsibilities.  It should be in a file named COPYING.  Among other
18 things, the copyright notice and this notice must be preserved on all
19 copies.
20 
21 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22 */
23 
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include "hpgl.h"
28 #include "pendef.h"
29 
30 PEN pt;
31 
Pen_Width_to_tmpfile(int pen,PEN_W width)32 void Pen_Width_to_tmpfile(int pen, PEN_W width)
33 {
34 	int i;
35 	PEN_N tp;
36 	PEN_W tw;
37 
38 	tp = (PEN_N) pen;
39 	tw = width;
40 
41 	if (record_off)		/* Wrong page!  */
42 		return;
43 	if (pen < 0)
44 		return;		/* Might happen when "current pen" is still
45 				   undefined */
46 	if (tp == 0) {		/* set all pens */
47 		for (i = 1; i < NUMPENS; ++i)
48 			pt.width[i] = tw;
49 	} else {
50 		pt.width[tp] = tw;	/* set just the specified one */
51 	}
52 
53 	if (fwrite(&tp, sizeof(tp), 1, td) != 1) {
54 		PError("Pen_Width_to_tmpfile - pen");
55 		Eprintf("Error @ Cmd %ld\n", vec_cntr_w);
56 		exit(ERROR);
57 	}
58 	if (fwrite(&tw, sizeof(tw), 1, td) != 1) {
59 		PError("Pen_Width_to_tmpfile - width");
60 		Eprintf("Error @ Cmd %ld\n", vec_cntr_w);
61 		exit(ERROR);
62 	}
63 }
64 
Pen_Color_to_tmpfile(int pen,int red,int green,int blue)65 void Pen_Color_to_tmpfile(int pen, int red, int green, int blue)
66 {
67 	PEN_N tp;
68 	PEN_C r, g, b;
69 
70 	tp = (PEN_N) pen;
71 	r = (PEN_C) red;
72 	g = (PEN_C) green;
73 	b = (PEN_C) blue;
74 
75 	if (record_off)		/* Wrong page!  */
76 		return;
77 
78 	if (fwrite(&tp, sizeof(tp), 1, td) != 1) {
79 		PError("Pen_Color_to_tmpfile - pen");
80 		Eprintf("Error @ Cmd %ld\n", vec_cntr_w);
81 		exit(ERROR);
82 	}
83 	if (fwrite(&r, sizeof(r), 1, td) != 1) {
84 		PError("Pen_Color_to_tmpfile - red component");
85 		Eprintf("Error @ Cmd %ld\n", vec_cntr_w);
86 		exit(ERROR);
87 	}
88 	if (fwrite(&g, sizeof(g), 1, td) != 1) {
89 		PError("Pen_Color_to_tmpfile - green component");
90 		Eprintf("Error @ Cmd %ld\n", vec_cntr_w);
91 		exit(ERROR);
92 	}
93 	if (fwrite(&b, sizeof(b), 1, td) != 1) {
94 		PError("Pen_Color_to_tmpfile - blue component");
95 		Eprintf("Error @ Cmd %ld\n", vec_cntr_w);
96 		exit(ERROR);
97 	}
98 }
99 
load_pen_width_table(FILE * td)100 int load_pen_width_table(FILE * td)
101 {
102 	PEN_N tp;
103 	PEN_W tw;
104 	int i;
105 
106 	if (fread((void *) &tp, sizeof(tp), 1, td) != 1) {
107 		return (0);
108 	}
109 
110 	if (fread((void *) &tw, sizeof(tw), 1, td) != 1) {
111 		return (0);
112 	}
113 
114 	if (tp >= NUMPENS) {	/* don't check < 0 - unsigned */
115 		return (1);
116 	}
117 
118 	if (tp == 0) {		/* set all pens */
119 		for (i = 1; i < NUMPENS; ++i)
120 			pt.width[i] = tw;
121 	} else {
122 		pt.width[tp] = tw;	/* set just the specified one */
123 	}
124 
125 	return (1);
126 }
127 
load_pen_color_table(FILE * td)128 int load_pen_color_table(FILE * td)
129 {
130 	PEN_N tp;
131 	PEN_C r, g, b;
132 
133 	if (fread((void *) &tp, sizeof(tp), 1, td) != 1) {
134 		return (-1);
135 	}
136 
137 	if (fread((void *) &r, sizeof(r), 1, td) != 1) {
138 		return (-1);
139 	}
140 
141 	if (fread((void *) &g, sizeof(g), 1, td) != 1) {
142 		return (-1);
143 	}
144 
145 	if (fread((void *) &b, sizeof(b), 1, td) != 1) {
146 		return (-1);
147 	}
148 
149 	if (tp >= NUMPENS) {	/* don't check < 0 - unsigned */
150 		return (1);
151 	}
152 
153 	set_color_rgb(tp, (BYTE) r, (BYTE) g, (BYTE) b);
154 	return ((int) tp);
155 }
156 
set_color_rgb(PEN_N index,BYTE r,BYTE g,BYTE b)157 void set_color_rgb(PEN_N index, BYTE r, BYTE g, BYTE b)
158 {
159 	pt.clut[index][0] = r;
160 	pt.clut[index][1] = g;
161 	pt.clut[index][2] = b;
162 	pt.color[index] = index;
163 }
164