/[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 174 Revision 175
21#include "config.h" 21#include "config.h"
22#include "compat.h" 22#include "compat.h"
23 23
24#include "generate_routines.h" 24#include "generate_routines.h"
25#include "error.h" 25#include "error.h"
26#include "list.h"
26 27
27#ifdef DMALLOC 28#ifdef DMALLOC
28#include <dmalloc.h> 29#include <dmalloc.h>
29#endif 30#endif
30 31
47 const psiconv_pixel_floats_t palet); 48 const psiconv_pixel_floats_t palet);
48static int psiconv_pixel_data_to_bytes(const psiconv_config config, 49static int psiconv_pixel_data_to_bytes(const psiconv_config config,
49 psiconv_pixel_bytes *bytes, int xsize, 50 psiconv_pixel_bytes *bytes, int xsize,
50 int ysize, const psiconv_pixel_ints pixels, 51 int ysize, const psiconv_pixel_ints pixels,
51 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);
52 62
53#define PALET_GREY_2_LEN 4 63#define PALET_GREY_2_LEN 4
54float 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};
55#define PALET_GREY_4_LEN 16 65#define PALET_GREY_4_LEN 16
56float palet_grey_4_rgb[PALET_GREY_4_LEN] = 66float palet_grey_4_rgb[PALET_GREY_4_LEN] =
91 int is_clipart) 101 int is_clipart)
92{ 102{
93 int res,colordepth,i; 103 int res,colordepth,i;
94 psiconv_pixel_ints ints; 104 psiconv_pixel_ints ints;
95 psiconv_pixel_floats_t floats,palet; 105 psiconv_pixel_floats_t floats,palet;
96 psiconv_list bytes; 106 psiconv_list bytes,bytes_rle;
97 psiconv_u8 *byteptr; 107 psiconv_u8 *byteptr,encoding;
98 108
99 if (!value) { 109 if (!value) {
100 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");
101 res = -PSICONV_E_GENERATE; 111 res = -PSICONV_E_GENERATE;
102 goto ERROR1; 112 goto ERROR1;
122 132
123 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,
124 ints,config->colordepth))) 134 ints,config->colordepth)))
125 goto ERROR2; 135 goto ERROR2;
126 136
127 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))))
128 goto ERROR3; 151 goto ERROR3;
129 if ((res = psiconv_write_u32(config,buf,0x28))) 152 if ((res = psiconv_write_u32(config,buf,0x28)))
130 goto ERROR3; 153 goto ERROR3;
131 if ((res = psiconv_write_u32(config,buf,value->xsize))) 154 if ((res = psiconv_write_u32(config,buf,value->xsize)))
132 goto ERROR3; 155 goto ERROR3;
143 goto ERROR3; 166 goto ERROR3;
144 if ((res = psiconv_write_u32(config,buf,(config->color?1:0)))) 167 if ((res = psiconv_write_u32(config,buf,(config->color?1:0))))
145 goto ERROR3; 168 goto ERROR3;
146 if ((res = psiconv_write_u32(config,buf,0))) 169 if ((res = psiconv_write_u32(config,buf,0)))
147 goto ERROR3; 170 goto ERROR3;
148 /* Encoding: no RLE for now */
149 if ((res = psiconv_write_u32(config,buf,0))) 171 if ((res = psiconv_write_u32(config,buf,encoding)))
150 goto ERROR3; 172 goto ERROR3;
151 if (is_clipart) { 173 if (is_clipart) {
152 if ((res = psiconv_write_u32(config,buf,0xffffffff))) 174 if ((res = psiconv_write_u32(config,buf,0xffffffff)))
153 goto ERROR3; 175 goto ERROR3;
154 if ((res = psiconv_write_u32(config,buf,0x00000044))) 176 if ((res = psiconv_write_u32(config,buf,0x00000044)))
308 psiconv_list_free(*bytes); 330 psiconv_list_free(*bytes);
309ERROR1: 331ERROR1:
310 return res; 332 return res;
311} 333}
312 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
313int psiconv_write_sketch_section(const psiconv_config config, 675int psiconv_write_sketch_section(const psiconv_config config,
314 psiconv_buffer buf, 676 psiconv_buffer buf,
315 const psiconv_sketch_section value) 677 const psiconv_sketch_section value)
316{ 678{
317 int res; 679 int res;
358 if ((res = psiconv_write_u32(config,buf,value->cut_bottom * 0x0c * 720 if ((res = psiconv_write_u32(config,buf,value->cut_bottom * 0x0c *
359 value->displayed_ysize))) 721 value->displayed_ysize)))
360 goto ERROR1; 722 goto ERROR1;
361 723
362ERROR1: 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:
363 return res; 755 return res;
364} 756}
365 757
366int psiconv_write_jumptable_section(const psiconv_config config, 758int psiconv_write_jumptable_section(const psiconv_config config,
367 psiconv_buffer buf, 759 psiconv_buffer buf,
392 784
393ERROR: 785ERROR:
394 return res; 786 return res;
395} 787}
396 788
397int psiconv_write_clipart_section(const psiconv_config config,
398 psiconv_buffer buf,
399 const psiconv_clipart_section value)
400{
401 int res;
402
403
404 if (!value) {
405 psiconv_warn(config,0,psiconv_buffer_length(buf),
406 "NULL Clipart Section");
407 res = -PSICONV_E_GENERATE;
408 goto ERROR;
409 }
410 if ((res = psiconv_write_u32(config,buf,PSICONV_ID_CLIPART_ITEM)))
411 goto ERROR;
412 if ((res = psiconv_write_u32(config,buf,0x00000002)))
413 goto ERROR;
414 if ((res = psiconv_write_u32(config,buf,0x00000000)))
415 goto ERROR;
416 if ((res = psiconv_write_u32(config,buf,0x00000000)))
417 goto ERROR;
418 if ((res = psiconv_write_u32(config,buf,0x0000000C)))
419 goto ERROR;
420 if ((res = psiconv_write_paint_data_section(config,buf,value->picture,1)))
421 goto ERROR;
422
423ERROR:
424 return res;
425}

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

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