/[public]/psiconv/trunk/lib/psiconv/generate_image.c
ViewVC logotype

Diff of /psiconv/trunk/lib/psiconv/generate_image.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 171 Revision 175
1/* 1/*
2
2 generate_image.c - Part of psiconv, a PSION 5 file formats converter 3 generate_image.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl> 4 Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl>
4 5
5 This program is free software; you can redistribute it and/or modify 6 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 7 it under the terms of the GNU General Public License as published by
20#include "config.h" 21#include "config.h"
21#include "compat.h" 22#include "compat.h"
22 23
23#include "generate_routines.h" 24#include "generate_routines.h"
24#include "error.h" 25#include "error.h"
26#include "list.h"
25 27
26#ifdef DMALLOC 28#ifdef DMALLOC
27#include <dmalloc.h> 29#include <dmalloc.h>
28#endif 30#endif
29 31
46 const psiconv_pixel_floats_t palet); 48 const psiconv_pixel_floats_t palet);
47static int psiconv_pixel_data_to_bytes(const psiconv_config config, 49static int psiconv_pixel_data_to_bytes(const psiconv_config config,
48 psiconv_pixel_bytes *bytes, int xsize, 50 psiconv_pixel_bytes *bytes, int xsize,
49 int ysize, const psiconv_pixel_ints pixels, 51 int ysize, const psiconv_pixel_ints pixels,
50 int colordepth); 52 int colordepth);
53static int psiconv_encode_rle8(const psiconv_config config,
54 const psiconv_pixel_bytes plain_bytes,
55 psiconv_pixel_bytes *encoded_bytes);
56static int psiconv_encode_rle16(const psiconv_config config,
57 const psiconv_pixel_bytes plain_bytes,
58 psiconv_pixel_bytes *encoded_bytes);
59static int psiconv_encode_rle24(const psiconv_config config,
60 const psiconv_pixel_bytes plain_bytes,
61 psiconv_pixel_bytes *encoded_bytes);
51 62
52#define PALET_GREY_2_LEN 4 63#define PALET_GREY_2_LEN 4
53float palet_grey_2_rgb[PALET_GREY_2_LEN] = {0.0/3, 1.0/3, 2.0/3, 3.0/3}; 64float palet_grey_2_rgb[PALET_GREY_2_LEN] = {0.0/3, 1.0/3, 2.0/3, 3.0/3};
54#define PALET_GREY_4_LEN 16 65#define PALET_GREY_4_LEN 16
55float palet_grey_4_rgb[PALET_GREY_4_LEN] = 66float palet_grey_4_rgb[PALET_GREY_4_LEN] =
84 }; 95 };
85 96
86 97
87int psiconv_write_paint_data_section(const psiconv_config config, 98int psiconv_write_paint_data_section(const psiconv_config config,
88 psiconv_buffer buf, 99 psiconv_buffer buf,
89 const psiconv_paint_data_section value) 100 const psiconv_paint_data_section value,
101 int is_clipart)
90{ 102{
91 int res,colordepth,i; 103 int res,colordepth,i;
92 psiconv_pixel_ints ints; 104 psiconv_pixel_ints ints;
93 psiconv_pixel_floats_t floats,palet; 105 psiconv_pixel_floats_t floats,palet;
94 psiconv_list bytes; 106 psiconv_list bytes,bytes_rle;
95 psiconv_u8 *byteptr; 107 psiconv_u8 *byteptr,encoding;
96 108
97 if (!value) { 109 if (!value) {
98 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null paint data section"); 110 psiconv_warn(config,0,psiconv_buffer_length(buf),"Null paint data section");
99 res = -PSICONV_E_GENERATE; 111 res = -PSICONV_E_GENERATE;
100 goto ERROR1; 112 goto ERROR1;
120 132
121 if ((res = psiconv_pixel_data_to_bytes(config,&bytes,value->xsize,value->ysize, 133 if ((res = psiconv_pixel_data_to_bytes(config,&bytes,value->xsize,value->ysize,
122 ints,config->colordepth))) 134 ints,config->colordepth)))
123 goto ERROR2; 135 goto ERROR2;
124 136
125 if ((res = psiconv_write_u32(config,buf,0x28+psiconv_list_length(bytes)))) 137
138 encoding = 0x00;
139 if ((res = psiconv_encode_rle8(config,bytes,&bytes_rle)))
140 goto ERROR3;
141 if (psiconv_list_length(bytes_rle) < psiconv_list_length(bytes)) {
142 encoding = 0x01;
143 psiconv_list_free(bytes);
144 bytes = bytes_rle;
145 } else {
146 bytes_rle = NULL;
147 }
148
149 if ((res = psiconv_write_u32(config,buf,
150 0x28+psiconv_list_length(bytes))))
126 goto ERROR3; 151 goto ERROR3;
127 if ((res = psiconv_write_u32(config,buf,0x28))) 152 if ((res = psiconv_write_u32(config,buf,0x28)))
128 goto ERROR3; 153 goto ERROR3;
129 if ((res = psiconv_write_u32(config,buf,value->xsize))) 154 if ((res = psiconv_write_u32(config,buf,value->xsize)))
130 goto ERROR3; 155 goto ERROR3;
141 goto ERROR3; 166 goto ERROR3;
142 if ((res = psiconv_write_u32(config,buf,(config->color?1:0)))) 167 if ((res = psiconv_write_u32(config,buf,(config->color?1:0))))
143 goto ERROR3; 168 goto ERROR3;
144 if ((res = psiconv_write_u32(config,buf,0))) 169 if ((res = psiconv_write_u32(config,buf,0)))
145 goto ERROR3; 170 goto ERROR3;
146 /* Encoding: no RLE for now */
147 if ((res = psiconv_write_u32(config,buf,0))) 171 if ((res = psiconv_write_u32(config,buf,encoding)))
172 goto ERROR3;
173 if (is_clipart) {
174 if ((res = psiconv_write_u32(config,buf,0xffffffff)))
148 goto ERROR3; 175 goto ERROR3;
176 if ((res = psiconv_write_u32(config,buf,0x00000044)))
177 goto ERROR3;
178 }
149 for (i = 0; i < psiconv_list_length(bytes); i++) { 179 for (i = 0; i < psiconv_list_length(bytes); i++) {
150 if (!(byteptr = psiconv_list_get(bytes,i))) 180 if (!(byteptr = psiconv_list_get(bytes,i)))
151 goto ERROR3; 181 goto ERROR3;
152 if ((res = psiconv_write_u8(config,buf,*byteptr))) 182 if ((res = psiconv_write_u8(config,buf,*byteptr)))
153 goto ERROR3; 183 goto ERROR3;
300 psiconv_list_free(*bytes); 330 psiconv_list_free(*bytes);
301ERROR1: 331ERROR1:
302 return res; 332 return res;
303} 333}
304 334
335int psiconv_encode_rle8(const psiconv_config config,
336 const psiconv_pixel_bytes plain_bytes,
337 psiconv_pixel_bytes *encoded_bytes)
338{
339 int res,i,j,len;
340 psiconv_u8 *entry,*next;
341 psiconv_u8 temp;
342
343 if (!(*encoded_bytes = psiconv_list_new(sizeof(*entry)))) {
344 res = -PSICONV_E_NOMEM;
345 goto ERROR1;
346 }
347
348 for (i = 0; i < psiconv_list_length(plain_bytes);) {
349 if (!(entry = psiconv_list_get(plain_bytes,i))) {
350 res = -PSICONV_E_NOMEM;
351 goto ERROR2;
352 }
353 if (!(next = psiconv_list_get(plain_bytes,i+1))) {
354 res = -PSICONV_E_NOMEM;
355 goto ERROR2;
356 }
357 if (i == psiconv_list_length(plain_bytes) - 2) {
358 temp = 0xfe;
359 if ((res = psiconv_list_add(*encoded_bytes,&temp)))
360 goto ERROR2;
361 if ((res = psiconv_list_add(*encoded_bytes,entry)))
362 goto ERROR2;
363 if ((res = psiconv_list_add(*encoded_bytes,next)))
364 goto ERROR2;
365 i +=2;
366 } else if (*next == *entry) {
367 len = 0;
368 while ((*next == *entry) &&
369 (i+len + 1 < psiconv_list_length(plain_bytes)) &&
370 len < 0x80) {
371 len ++;
372 if (!(next = psiconv_list_get(plain_bytes,i+len))) {
373 res = -PSICONV_E_NOMEM;
374 goto ERROR2;
375 }
376 }
377 temp = len - 1;
378 if ((res = psiconv_list_add(*encoded_bytes,&temp)))
379 goto ERROR2;
380 if ((res = psiconv_list_add(*encoded_bytes,entry)))
381 goto ERROR2;
382 i += len;
383 } else {
384 len = 1;
385 while ((*next != *entry) &&
386 (i+len+1 < psiconv_list_length(plain_bytes)) &&
387 len < 0x80) {
388 len ++;
389 entry = next;
390 if (!(next = psiconv_list_get(plain_bytes,i+len))) {
391 res = -PSICONV_E_NOMEM;
392 goto ERROR2;
393 }
394 }
395 len --;
396 temp = 0x100 - len;
397 if ((res = psiconv_list_add(*encoded_bytes,&temp)))
398 goto ERROR2;
399 for (j = 0; j < len; j++) {
400 if (!(next = psiconv_list_get(plain_bytes,i+j))) {
401 res = -PSICONV_E_NOMEM;
402 goto ERROR2;
403 }
404 if ((res = psiconv_list_add(*encoded_bytes,next)))
405 goto ERROR2;
406 }
407 i += len;
408 }
409 }
410 return 0;
411
412ERROR2:
413 psiconv_list_free(*encoded_bytes);
414ERROR1:
415 return res;
416}
417
418int psiconv_encode_rle16(const psiconv_config config,
419 const psiconv_pixel_bytes plain_bytes,
420 psiconv_pixel_bytes *encoded_bytes)
421{
422 int res,i,j,len;
423 psiconv_u8 *entry1,*entry2,*next1,*next2;
424 psiconv_u8 temp;
425
426 if (!(*encoded_bytes = psiconv_list_new(sizeof(*entry1)))) {
427 res = -PSICONV_E_NOMEM;
428 goto ERROR1;
429 }
430
431 for (i = 0; i < psiconv_list_length(plain_bytes);) {
432 if (!(entry1 = psiconv_list_get(plain_bytes,i))) {
433 res = -PSICONV_E_NOMEM;
434 goto ERROR2;
435 }
436 if (!(entry2 = psiconv_list_get(plain_bytes,i+1))) {
437 res = -PSICONV_E_NOMEM;
438 goto ERROR2;
439 }
440 if (!(next1 = psiconv_list_get(plain_bytes,i+2))) {
441 res = -PSICONV_E_NOMEM;
442 goto ERROR2;
443 }
444 if (!(next2 = psiconv_list_get(plain_bytes,i+3))) {
445 res = -PSICONV_E_NOMEM;
446 goto ERROR2;
447 }
448 if (i == psiconv_list_length(plain_bytes) - 4) {
449 temp = 0xfe;
450 if ((res = psiconv_list_add(*encoded_bytes,&temp)))
451 goto ERROR2;
452 if ((res = psiconv_list_add(*encoded_bytes,entry1)))
453 goto ERROR2;
454 if ((res = psiconv_list_add(*encoded_bytes,entry2)))
455 goto ERROR2;
456 if ((res = psiconv_list_add(*encoded_bytes,next1)))
457 goto ERROR2;
458 if ((res = psiconv_list_add(*encoded_bytes,next2)))
459 goto ERROR2;
460 i +=4;
461 } else if ((*next1 == *entry1) && (*next2 == *entry2)) {
462 len = 0;
463 while (((*next1 == *entry1) && (*next2 == *entry2)) &&
464 (i+2*len + 4 < psiconv_list_length(plain_bytes)) &&
465 len < 0x80) {
466 len ++;
467 if (!(next1 = psiconv_list_get(plain_bytes,i+len*2))) {
468 res = -PSICONV_E_NOMEM;
469 goto ERROR2;
470 }
471 if (!(next2 = psiconv_list_get(plain_bytes,i+len*2+1))) {
472 res = -PSICONV_E_NOMEM;
473 goto ERROR2;
474 }
475 }
476 temp = len - 1;
477 if ((res = psiconv_list_add(*encoded_bytes,&temp)))
478 goto ERROR2;
479 if ((res = psiconv_list_add(*encoded_bytes,entry1)))
480 goto ERROR2;
481 if ((res = psiconv_list_add(*encoded_bytes,entry2)))
482 goto ERROR2;
483 i += len*2;
484 } else {
485 len = 1;
486 while (((*next1 != *entry1) || (*next2 != *entry2))&&
487 (i+len*2+4 < psiconv_list_length(plain_bytes)) &&
488 len < 0x80) {
489 len ++;
490 entry1 = next1;
491 entry2 = next2;
492 if (!(next1 = psiconv_list_get(plain_bytes,i+len*2))) {
493 res = -PSICONV_E_NOMEM;
494 goto ERROR2;
495 }
496 if (!(next2 = psiconv_list_get(plain_bytes,i+len*2+1))) {
497 res = -PSICONV_E_NOMEM;
498 goto ERROR2;
499 }
500 }
501 len --;
502 temp = 0x100 - len;
503 if ((res = psiconv_list_add(*encoded_bytes,&temp)))
504 goto ERROR2;
505 for (j = 0; j < len; j++) {
506 if (!(next1 = psiconv_list_get(plain_bytes,i+j*2))) {
507 res = -PSICONV_E_NOMEM;
508 goto ERROR2;
509 }
510 if (!(next2 = psiconv_list_get(plain_bytes,i+j*2+1))) {
511 res = -PSICONV_E_NOMEM;
512 goto ERROR2;
513 }
514 if ((res = psiconv_list_add(*encoded_bytes,next1)))
515 goto ERROR2;
516 if ((res = psiconv_list_add(*encoded_bytes,next2)))
517 goto ERROR2;
518 }
519 i += len*2;
520 }
521 }
522 return 0;
523
524ERROR2:
525 psiconv_list_free(*encoded_bytes);
526ERROR1:
527 return res;
528}
529
530int psiconv_encode_rle24(const psiconv_config config,
531 const psiconv_pixel_bytes plain_bytes,
532 psiconv_pixel_bytes *encoded_bytes)
533{
534 int res,i,j,len;
535 psiconv_u8 *entry1,*entry2,*entry3,*next1,*next2,*next3;
536 psiconv_u8 temp;
537
538 if (!(*encoded_bytes = psiconv_list_new(sizeof(*entry1)))) {
539 res = -PSICONV_E_NOMEM;
540 goto ERROR1;
541 }
542
543 for (i = 0; i < psiconv_list_length(plain_bytes);) {
544 if (!(entry1 = psiconv_list_get(plain_bytes,i))) {
545 res = -PSICONV_E_NOMEM;
546 goto ERROR2;
547 }
548 if (!(entry2 = psiconv_list_get(plain_bytes,i+1))) {
549 res = -PSICONV_E_NOMEM;
550 goto ERROR2;
551 }
552 if (!(entry3 = psiconv_list_get(plain_bytes,i+2))) {
553 res = -PSICONV_E_NOMEM;
554 goto ERROR2;
555 }
556 if (!(next1 = psiconv_list_get(plain_bytes,i+3))) {
557 res = -PSICONV_E_NOMEM;
558 goto ERROR2;
559 }
560 if (!(next2 = psiconv_list_get(plain_bytes,i+4))) {
561 res = -PSICONV_E_NOMEM;
562 goto ERROR2;
563 }
564 if (!(next3 = psiconv_list_get(plain_bytes,i+5))) {
565 res = -PSICONV_E_NOMEM;
566 goto ERROR2;
567 }
568 if (i == psiconv_list_length(plain_bytes) - 6) {
569 temp = 0xfe;
570 if ((res = psiconv_list_add(*encoded_bytes,&temp)))
571 goto ERROR2;
572 if ((res = psiconv_list_add(*encoded_bytes,entry1)))
573 goto ERROR2;
574 if ((res = psiconv_list_add(*encoded_bytes,entry2)))
575 goto ERROR2;
576 if ((res = psiconv_list_add(*encoded_bytes,entry3)))
577 goto ERROR2;
578 if ((res = psiconv_list_add(*encoded_bytes,next1)))
579 goto ERROR2;
580 if ((res = psiconv_list_add(*encoded_bytes,next2)))
581 goto ERROR2;
582 if ((res = psiconv_list_add(*encoded_bytes,next3)))
583 goto ERROR2;
584 i +=4;
585 } else if ((*next1 == *entry1) && (*next2 == *entry2) &&
586 (*next3 == *entry3)) {
587 len = 0;
588 while (((*next1 == *entry1) && (*next2 == *entry2) &&
589 (*next3 == *entry3)) &&
590 (i+3*len + 6 < psiconv_list_length(plain_bytes)) &&
591 len < 0x80) {
592 len ++;
593 if (!(next1 = psiconv_list_get(plain_bytes,i+len*3))) {
594 res = -PSICONV_E_NOMEM;
595 goto ERROR2;
596 }
597 if (!(next2 = psiconv_list_get(plain_bytes,i+len*3+1))) {
598 res = -PSICONV_E_NOMEM;
599 goto ERROR2;
600 }
601 if (!(next3 = psiconv_list_get(plain_bytes,i+len*3+2))) {
602 res = -PSICONV_E_NOMEM;
603 goto ERROR2;
604 }
605 }
606 temp = len - 1;
607 if ((res = psiconv_list_add(*encoded_bytes,&temp)))
608 goto ERROR2;
609 if ((res = psiconv_list_add(*encoded_bytes,entry1)))
610 goto ERROR2;
611 if ((res = psiconv_list_add(*encoded_bytes,entry2)))
612 goto ERROR2;
613 if ((res = psiconv_list_add(*encoded_bytes,entry3)))
614 goto ERROR2;
615 i += len*3;
616 } else {
617 len = 1;
618 while (((*next1 != *entry1) || (*next2 != *entry2) ||
619 (*next3 != *entry3)) &&
620 (i+len*3+6 < psiconv_list_length(plain_bytes)) &&
621 len < 0x80) {
622 len ++;
623 entry1 = next1;
624 entry2 = next2;
625 entry3 = next3;
626 if (!(next1 = psiconv_list_get(plain_bytes,i+len*3))) {
627 res = -PSICONV_E_NOMEM;
628 goto ERROR2;
629 }
630 if (!(next2 = psiconv_list_get(plain_bytes,i+len*3+1))) {
631 res = -PSICONV_E_NOMEM;
632 goto ERROR2;
633 }
634 if (!(next3 = psiconv_list_get(plain_bytes,i+len*3+2))) {
635 res = -PSICONV_E_NOMEM;
636 goto ERROR2;
637 }
638 }
639 len --;
640 temp = 0x100 - len;
641 if ((res = psiconv_list_add(*encoded_bytes,&temp)))
642 goto ERROR2;
643 for (j = 0; j < len; j++) {
644 if (!(next1 = psiconv_list_get(plain_bytes,i+j*3))) {
645 res = -PSICONV_E_NOMEM;
646 goto ERROR2;
647 }
648 if (!(next2 = psiconv_list_get(plain_bytes,i+j*3+1))) {
649 res = -PSICONV_E_NOMEM;
650 goto ERROR2;
651 }
652 if (!(next2 = psiconv_list_get(plain_bytes,i+j*3+2))) {
653 res = -PSICONV_E_NOMEM;
654 goto ERROR2;
655 }
656 if ((res = psiconv_list_add(*encoded_bytes,next1)))
657 goto ERROR2;
658 if ((res = psiconv_list_add(*encoded_bytes,next2)))
659 goto ERROR2;
660 if ((res = psiconv_list_add(*encoded_bytes,next3)))
661 goto ERROR2;
662 }
663 i += len*3;
664 }
665 }
666 return 0;
667
668ERROR2:
669 psiconv_list_free(*encoded_bytes);
670ERROR1:
671 return res;
672}
673
674
305int psiconv_write_sketch_section(const psiconv_config config, 675int psiconv_write_sketch_section(const psiconv_config config,
306 psiconv_buffer buf, 676 psiconv_buffer buf,
307 const psiconv_sketch_section value) 677 const psiconv_sketch_section value)
308{ 678{
309 int res; 679 int res;
330 goto ERROR1; 700 goto ERROR1;
331 if ((res = psiconv_write_u16(config,buf,value->form_ysize))) 701 if ((res = psiconv_write_u16(config,buf,value->form_ysize)))
332 goto ERROR1; 702 goto ERROR1;
333 if ((res = psiconv_write_u16(config,buf,0x0000))) 703 if ((res = psiconv_write_u16(config,buf,0x0000)))
334 goto ERROR1; 704 goto ERROR1;
335 if ((res = psiconv_write_paint_data_section(config,buf,value->picture))) 705 if ((res = psiconv_write_paint_data_section(config,buf,value->picture,0)))
336 goto ERROR1; 706 goto ERROR1;
337 if ((res = psiconv_write_u16(config,buf,value->magnification_x * 0x03e8))) 707 if ((res = psiconv_write_u16(config,buf,value->magnification_x * 0x03e8)))
338 goto ERROR1; 708 goto ERROR1;
339 if ((res = psiconv_write_u16(config,buf,value->magnification_y * 0x03e8))) 709 if ((res = psiconv_write_u16(config,buf,value->magnification_y * 0x03e8)))
340 goto ERROR1; 710 goto ERROR1;
350 if ((res = psiconv_write_u32(config,buf,value->cut_bottom * 0x0c * 720 if ((res = psiconv_write_u32(config,buf,value->cut_bottom * 0x0c *
351 value->displayed_ysize))) 721 value->displayed_ysize)))
352 goto ERROR1; 722 goto ERROR1;
353 723
354ERROR1: 724ERROR1:
725 return res;
726}
727
728int psiconv_write_clipart_section(const psiconv_config config,
729 psiconv_buffer buf,
730 const psiconv_clipart_section value)
731{
732 int res;
733
734
735 if (!value) {
736 psiconv_warn(config,0,psiconv_buffer_length(buf),
737 "NULL Clipart Section");
738 res = -PSICONV_E_GENERATE;
739 goto ERROR;
740 }
741 if ((res = psiconv_write_u32(config,buf,PSICONV_ID_CLIPART_ITEM)))
742 goto ERROR;
743 if ((res = psiconv_write_u32(config,buf,0x00000002)))
744 goto ERROR;
745 if ((res = psiconv_write_u32(config,buf,0x00000000)))
746 goto ERROR;
747 if ((res = psiconv_write_u32(config,buf,0x00000000)))
748 goto ERROR;
749 if ((res = psiconv_write_u32(config,buf,0x0000000C)))
750 goto ERROR;
751 if ((res = psiconv_write_paint_data_section(config,buf,value->picture,1)))
752 goto ERROR;
753
754ERROR:
355 return res; 755 return res;
356} 756}
357 757
358int psiconv_write_jumptable_section(const psiconv_config config, 758int psiconv_write_jumptable_section(const psiconv_config config,
359 psiconv_buffer buf, 759 psiconv_buffer buf,
383 } 783 }
384 784
385ERROR: 785ERROR:
386 return res; 786 return res;
387} 787}
788

Legend:
Removed from v.171  
changed lines
  Added in v.175

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26