/[projet1]/public/pc/tools/osdk/main/pictconv/sources/oric_converter_colored.cpp
Defence Force logotype

Contents of /public/pc/tools/osdk/main/pictconv/sources/oric_converter_colored.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1260 - (show annotations)
Sat Feb 14 20:26:49 2015 UTC (4 years, 7 months ago) by dbug
File size: 10193 byte(s)
PictConv 0.21
- Added support for 32bit picture, to handle things like masking/opacity in the picture formats
- The -a1 mode will generate bytes with null value for blocks of 6 pixels with transparent alpha values (only active in monochrome or AIC conversion modes)
- Added the -f7 conversion mode for Oric pictures using the AIC coloring method.

1
2
3 #include <assert.h>
4 #include <stdio.h>
5 #include <fcntl.h>
6 #include <iostream>
7 #include <string.h>
8
9 #ifndef _WIN32
10 #include <unistd.h>
11 #endif
12
13 #include <sys/types.h>
14 #include <sys/stat.h>
15
16 #include "FreeImage.h"
17
18 #include "defines.h"
19 #include "getpixel.h"
20 #include "hires.h"
21 #include "oric_converter.h"
22 #include "dithering.h"
23
24 #include "common.h"
25
26 #include "image.h"
27
28
29 char *PtrSpaces;
30 char BufferSpaces[]=" ";
31
32 int gcur_line=0;
33 int gmax_count=0;
34
35 bool FlagDebug=false;
36
37
38 BlocOf6 Bloc6Buffer[40]; // For 240 pixel wide pictures
39
40
41 /*
42
43 Les cas possibles:
44 - 6 pixels unis.
45 * Ils peuvent être d'une de ces couleurs:
46 Utilisation de la couleur courante du papier
47 Utilisation de la couleur courante de l'encre
48 Utilisation de la couleur courante du papier, avec inversion vidéo
49 Utilisation de la couleur courante de l'encre, avec inversion vidéo
50
51 * On peur y faire:
52 Changement de papier de la même couleur
53 Changement de papier d'une couleur inversée de la couleur, en mettant la vidéo inverse
54
55
56
57 - 6 pixels, utilisant deux couleurs.
58
59
60
61
62 */
63
64
65
66
67 bool RecurseLine(unsigned char count,BlocOf6 *ptr_bloc6,unsigned char *ptr_hires,ORIC_COLOR cur_paper,ORIC_COLOR cur_ink)
68 {
69 PtrSpaces-=2;
70
71 /*
72 if (gcur_line==138)
73 {
74 printf("\r\n Count:%d Paper:%d Ink:%d",count,cur_paper,cur_ink);
75 }
76 */
77 /*
78 if ((time(0)-gLastTimer)>2)
79 {
80 //
81 // End of recursion with error
82 //
83 if (!FlagDebug)
84 {
85 return false;
86 }
87 }
88 */
89
90 if (count<gmax_count)
91 {
92 gmax_count=count;
93 //printf("\r\n MaxCount:%d",gmax_count);
94 /*
95 if ((gcur_line==138) && (gmax_count==34))
96 {
97 __asm {int 3 }
98 }
99 */
100 }
101
102 if (!count)
103 {
104 //
105 // End of recursion
106 //
107 return true;
108 }
109
110 unsigned char color_count=ptr_bloc6->color_count;
111 ORIC_COLOR c0=ptr_bloc6->colors[0];
112 ORIC_COLOR c1=ptr_bloc6->colors[1];
113 unsigned char v =ptr_bloc6->value;
114
115
116 count--;
117 ptr_bloc6++;
118 ptr_hires++;
119
120 if (color_count==1)
121 {
122 // ========================================
123 // The current bloc of pixels is using only
124 // one color. It's the right opportunity to
125 // change either the PAPER or the INK color.
126 // ========================================
127
128 if (c0==cur_paper)
129 {
130 //
131 // The 6 pixels are using the current paper color.
132 //
133
134 // Use current paper color
135 if (FlagDebug) printf("\r\n %sUse paper color (%d)",PtrSpaces,cur_paper);
136 if (RecurseLine(count,ptr_bloc6,ptr_hires,cur_paper,cur_ink))
137 {
138 ptr_hires[-1]=64;
139 return true;
140 }
141
142 // Try each of the 8 possible ink colors
143 for (ORIC_COLOR color=ORIC_COLOR_BLACK;color<_ORIC_COLOR_LAST_;color++)
144 {
145 if (FlagDebug) printf("\r\n %sUse paper color (%d) while changing ink color to (%d)",PtrSpaces,cur_paper,color);
146 if (RecurseLine(count,ptr_bloc6,ptr_hires,cur_paper,color))
147 {
148 ptr_hires[-1]=(unsigned char)color;
149 return true;
150 }
151 }
152 }
153
154 // Use current ink color
155 if (c0==cur_ink)
156 {
157 if (FlagDebug) printf("\r\n %sUse ink color (%d)",PtrSpaces,cur_ink);
158 if (RecurseLine(count,ptr_bloc6,ptr_hires,cur_paper,cur_ink))
159 {
160 ptr_hires[-1]=64|63;
161 return true;
162 }
163 }
164
165
166 if (c0==(7-cur_paper))
167 {
168 //
169 // The 6 pixels are using the current inverted paper color.
170 //
171
172 // Use current paper color
173 if (FlagDebug) printf("\r\n %sUse inverted paper color (%d => %d)",PtrSpaces,cur_paper,7-cur_paper);
174 if (RecurseLine(count,ptr_bloc6,ptr_hires,cur_paper,cur_ink))
175 {
176 ptr_hires[-1]=128|64;
177 return true;
178 }
179
180 // Try each of the 8 possible ink colors
181 for (ORIC_COLOR color=ORIC_COLOR_BLACK;color<_ORIC_COLOR_LAST_;color++)
182 {
183 if (FlagDebug) printf("\r\n %sUse inverted paper color (%d => %d) while changing ink color to (%d)",PtrSpaces,cur_paper,7-cur_paper,color);
184 if (RecurseLine(count,ptr_bloc6,ptr_hires,cur_paper,color))
185 {
186 ptr_hires[-1]=(unsigned char)(128|color);
187 return true;
188 }
189 }
190 }
191
192 // Use current inverted ink color
193 if (c0==(7-cur_ink))
194 {
195 if (FlagDebug) printf("\r\n %sUse inverted ink color (%d => %d)",PtrSpaces,cur_ink,7-cur_ink);
196 if (RecurseLine(count,ptr_bloc6,ptr_hires,cur_paper,cur_ink))
197 {
198 ptr_hires[-1]=128|64|63;
199 return true;
200 }
201 }
202
203 // Change paper color
204 if (FlagDebug) printf("\r\n %sChange paper color to (%d)",PtrSpaces,c0);
205 if (RecurseLine(count,ptr_bloc6,ptr_hires,c0,cur_ink))
206 {
207 ptr_hires[-1]=(unsigned char)(16+c0);
208 return true;
209 }
210
211
212 // Change paper color, using inverse video
213 if (FlagDebug) printf("\r\n %sChange paper color to (%d) using inversion (%d)",PtrSpaces,7-c0,c0);
214 if (RecurseLine(count,ptr_bloc6,ptr_hires,ORIC_COLOR_WHITE-c0,cur_ink))
215 {
216 ptr_hires[-1]=(unsigned char)(128|16+(7-c0));
217 return true;
218 }
219 }
220 else
221 {
222 // ========================================
223 // The current bloc of pixels is using two
224 // different colors. It's totaly impossible
225 // to use attributes changes on this one.
226 // ========================================
227
228
229 // Try simple pixels.
230 if ((c0==cur_paper) && (c1==cur_ink))
231 {
232 if (FlagDebug) printf("\r\n %sUse current colors (%d,%d)",PtrSpaces,cur_paper,cur_ink);
233 if (RecurseLine(count,ptr_bloc6,ptr_hires,cur_paper,cur_ink))
234 {
235 ptr_hires[-1]=64|v;
236 return true;
237 }
238 }
239
240 // Try simple pixels, but invert the bitmask.
241 if ((c0==cur_ink) && (c1==cur_paper))
242 {
243 if (FlagDebug) printf("\r\n %sUse current colors (%d,%d)",PtrSpaces,cur_paper,cur_ink);
244 if (RecurseLine(count,ptr_bloc6,ptr_hires,cur_paper,cur_ink))
245 {
246 ptr_hires[-1]=64|(v^63);
247 return true;
248 }
249 }
250
251 // Try simple video inverted pixels.
252 if ((c0==(7-cur_paper)) && (c1==(7-cur_ink)))
253 {
254 if (FlagDebug) printf("\r\n %sUse current inverted colors (%d,%d) => (%d,%d)",PtrSpaces,cur_paper,cur_ink,7-cur_paper,7-cur_ink);
255 if (RecurseLine(count,ptr_bloc6,ptr_hires,cur_paper,cur_ink))
256 {
257 ptr_hires[-1]=128|64|v;
258 return true;
259 }
260 }
261
262 // Try simple video inverted pixels, but invert the bitmask.
263 if ((c0==(7-cur_ink)) && (c1==(7-cur_paper)))
264 {
265 if (FlagDebug) printf("\r\n %sUse current inverted colors (%d,%d) => (%d,%d)",PtrSpaces,cur_paper,cur_ink,7-cur_paper,7-cur_ink);
266 if (RecurseLine(count,ptr_bloc6,ptr_hires,cur_paper,cur_ink))
267 {
268 ptr_hires[-1]=128|64|(v^63);
269 return true;
270 }
271 }
272 }
273
274 //printf("\r\n ==== back track !!! ====");
275 //getch();
276 PtrSpaces+=2;
277 return false;
278 }
279
280
281 void OricPictureConverter::convert_colored(const ImageContainer& sourcePicture)
282 {
283 ImageContainer convertedPicture(sourcePicture);
284
285 //
286 // Phase 1: Create a buffer with infos
287 //
288 bool flag=false;
289 unsigned char *ptr_hires=m_Buffer.m_buffer;
290
291 bool error_in_picture=false;
292
293 for (unsigned y=0;y<m_Buffer.m_buffer_height;y++)
294 {
295 if (m_flag_debug)
296 {
297 printf("\r\nLine %d ",y);
298 }
299
300 BlocOf6 *ptr_bloc6=Bloc6Buffer;
301 bool error_in_line=false;
302
303 //
304 // Create a buffer for the scanline
305 //
306 int x=0;
307 for (int col=0;col<m_Buffer.m_buffer_cols;col++)
308 {
309 bool error_in_bloc=false;
310
311 // Init that bloc
312 ptr_bloc6->Clear();
313
314 for (int bit=0;bit<6;bit++)
315 {
316
317 // Get the original pixel color
318 BYTE *ptr_byte=FreeImage_GetBitsRowCol(convertedPicture.GetBitmap(),x,y);
319 ORIC_COLOR color=ORIC_COLOR_BLACK;
320 if ((*ptr_byte++)>128) color|=ORIC_COLOR_BLUE;
321 if ((*ptr_byte++)>128) color|=ORIC_COLOR_GREEN;
322 if ((*ptr_byte++)>128) color|=ORIC_COLOR_RED;
323
324 error_in_bloc|=ptr_bloc6->AddColor(color);
325
326 x++;
327 }
328
329 if (error_in_bloc)
330 {
331 if (!error_in_line)
332 {
333 printf("\r\nLine %d ",y);
334 }
335 printf("[%d colors bloc %d] ",ptr_bloc6->color_count,col);
336 error_in_line=true;
337 }
338 ptr_bloc6++;
339 }
340 if (error_in_line)
341 {
342 error_in_picture=true;
343 }
344
345 //
346 // Convert to monochrome
347 //
348 //gLastTimer=time(0);
349 ptr_bloc6=Bloc6Buffer;
350
351 unsigned char val;
352
353 PtrSpaces=BufferSpaces+strlen(BufferSpaces);
354 gcur_line=y;
355 gmax_count=m_Buffer.m_buffer_cols;
356 #ifdef _DEBUG
357 if (y==138)
358 {
359 //__asm int 3;
360 }
361 #endif
362 if (RecurseLine((unsigned char)m_Buffer.m_buffer_cols,ptr_bloc6,ptr_hires,ORIC_COLOR_BLACK,ORIC_COLOR_WHITE))
363 //if (RecurseLine(7,ptr_bloc6,ptr_hires,0,7))
364 {
365 //
366 // Recursion work.
367 // Nothing to do.
368 //
369 if (FlagDebug)
370 {
371 printf("\r\n ======= found =====");
372 printf("\r\n Dump: ");
373 for (int col=0;col<7;col++)
374 {
375 val=ptr_hires[col];
376 printf("[%d,%d,%d] ",(val>>7)&1,(val>>6)&1,(val&63));
377 }
378
379 //getch();
380 }
381 ptr_bloc6+=m_Buffer.m_buffer_cols;
382 ptr_hires+=m_Buffer.m_buffer_cols;
383 }
384 else
385 {
386 if (!error_in_line)
387 {
388 printf("\r\nLine %d ",y);
389 }
390 printf(" max:%d ",gmax_count);
391
392 //
393 // Unable to perfom conversion
394 // Use standard monochrom algorithm
395 //
396 x=0;
397 for (int col=0;col<m_Buffer.m_buffer_cols;col++)
398 {
399 if (flag)
400 {
401 if (ptr_bloc6->color_count>2)
402 {
403 val=127;
404 }
405 else
406 {
407 val=64;
408 }
409 }
410 else
411 {
412 val=64+ptr_bloc6->value;
413 }
414 *ptr_hires++=val;
415 ptr_bloc6++;
416 }
417 }
418 }
419 }
420

  ViewVC Help
Powered by ViewVC 1.1.26