OSDN Git Service

Once more, Allow building with either giflib 4.2 or 5.0.
[swfed/swfed.git] / src / swf_shape_record_edge.c
1 #include <stdio.h>
2 #include "bitstream.h"
3 #include "swf_shape_record_edge.h"
4 #include "swf_tag_shape.h"
5
6 int
7 swf_shape_record_edge_parse(bitstream_t *bs,
8                             swf_shape_record_edge_t *shape_record_edge,
9                             swf_tag_t *tag) {
10     int result;
11     unsigned int shape_coord_real_size;
12     swf_tag_shape_detail_t *swf_tag_shape = (swf_tag_shape_detail_t *) tag->detail;
13
14     result = bitstream_getbit(bs);
15     if (result == -1) {
16         return 1;
17     }
18     shape_record_edge->shape_record_type = result;
19     shape_record_edge->shape_edge_type   = bitstream_getbit(bs);
20     shape_record_edge->shape_coord_size  = bitstream_getbits(bs, 4);
21     shape_coord_real_size = shape_record_edge->shape_coord_size + 2;
22     if (shape_record_edge->shape_edge_type == 0) {
23         signed control_delta_x, control_delta_y;
24         signed anchor_delta_x, anchor_delta_y;
25         control_delta_x = bitstream_getbits_signed(bs, shape_coord_real_size);
26         control_delta_y = bitstream_getbits_signed(bs, shape_coord_real_size);
27         anchor_delta_x = bitstream_getbits_signed(bs, shape_coord_real_size);
28         anchor_delta_y = bitstream_getbits_signed(bs, shape_coord_real_size);
29
30         swf_tag_shape->_current_x += control_delta_x;
31         swf_tag_shape->_current_y += control_delta_y;
32         shape_record_edge->shape_control_x = swf_tag_shape->_current_x;
33         shape_record_edge->shape_control_y = swf_tag_shape->_current_y;
34         swf_tag_shape->_current_x += anchor_delta_x;
35         swf_tag_shape->_current_y += anchor_delta_y;
36         shape_record_edge->shape_anchor_x = swf_tag_shape->_current_x;
37         shape_record_edge->shape_anchor_y = swf_tag_shape->_current_y;
38     } else {
39         shape_record_edge->shape_line_has_x_and_y = bitstream_getbit(bs);
40         if (shape_record_edge->shape_line_has_x_and_y == 1) {
41             signed delta_x, delta_y;
42             delta_x = bitstream_getbits_signed(bs, shape_coord_real_size);
43             delta_y = bitstream_getbits_signed(bs, shape_coord_real_size);
44             swf_tag_shape->_current_x += delta_x;
45             swf_tag_shape->_current_y += delta_y;
46         } else {
47             shape_record_edge->shape_line_has_x_or_y = bitstream_getbit(bs);
48             if (shape_record_edge->shape_line_has_x_or_y == 0) {
49                 signed delta_x;
50                 delta_x = bitstream_getbits_signed(bs, shape_coord_real_size);
51                 swf_tag_shape->_current_x += delta_x;
52             } else {
53                 signed delta_y;
54                 delta_y = bitstream_getbits_signed(bs, shape_coord_real_size);
55                 swf_tag_shape->_current_y += delta_y;
56             }
57         }
58         shape_record_edge->shape_x = swf_tag_shape->_current_x;
59         shape_record_edge->shape_y = swf_tag_shape->_current_y;
60     }
61     return 0;
62 }
63
64 int
65 swf_shape_record_edge_build(bitstream_t *bs,
66                             swf_shape_record_edge_t *shape_record_edge,
67                             swf_tag_t *tag) {
68 //    int ret;
69     unsigned int size, shape_coord_real_size = 2;
70     swf_tag_shape_detail_t *swf_tag_shape = (swf_tag_shape_detail_t *) tag->detail;
71     
72     bitstream_putbit(bs, shape_record_edge->shape_record_type);
73     bitstream_putbit(bs, shape_record_edge->shape_edge_type);
74
75     if (shape_record_edge->shape_edge_type == 0) {
76         size = bitstream_need_bits_signed(shape_record_edge->shape_control_x - swf_tag_shape->_current_x);
77         shape_coord_real_size =  (shape_coord_real_size>size)?shape_coord_real_size:size;
78         size = bitstream_need_bits_signed(shape_record_edge->shape_control_y - swf_tag_shape->_current_y);
79         shape_coord_real_size =  (shape_coord_real_size>size)?shape_coord_real_size:size;
80         size = bitstream_need_bits_signed(shape_record_edge->shape_anchor_x - shape_record_edge->shape_control_x);
81         shape_coord_real_size =  (shape_coord_real_size>size)?shape_coord_real_size:size;
82         size = bitstream_need_bits_signed(shape_record_edge->shape_anchor_y - shape_record_edge->shape_control_y);
83         shape_coord_real_size =  (shape_coord_real_size>size)?shape_coord_real_size:size;
84     } else {
85         size = bitstream_need_bits_signed(shape_record_edge->shape_x - swf_tag_shape->_current_x);
86         shape_coord_real_size =  (shape_coord_real_size>size)?shape_coord_real_size:size;
87         size = bitstream_need_bits_signed(shape_record_edge->shape_y - swf_tag_shape->_current_y);
88         shape_coord_real_size =  (shape_coord_real_size>size)?shape_coord_real_size:size;
89     }
90     shape_record_edge->shape_coord_size = shape_coord_real_size - 2;
91     bitstream_putbits(bs, shape_record_edge->shape_coord_size, 4);
92
93     if (shape_record_edge->shape_edge_type == 0) {
94         signed control_delta_x = shape_record_edge->shape_control_x - swf_tag_shape->_current_x;
95         signed control_delta_y = shape_record_edge->shape_control_y - swf_tag_shape->_current_y;
96         signed anchor_delta_x = shape_record_edge->shape_anchor_x - shape_record_edge->shape_control_x;
97         signed anchor_delta_y = shape_record_edge->shape_anchor_y - shape_record_edge->shape_control_y;
98         bitstream_putbits_signed(bs, control_delta_x, shape_coord_real_size);
99         bitstream_putbits_signed(bs, control_delta_y, shape_coord_real_size);
100         bitstream_putbits_signed(bs, anchor_delta_x, shape_coord_real_size);
101         bitstream_putbits_signed(bs, anchor_delta_y, shape_coord_real_size);
102         swf_tag_shape->_current_x = shape_record_edge->shape_anchor_x;
103         swf_tag_shape->_current_y = shape_record_edge->shape_anchor_y;
104     } else {
105         signed delta_x = shape_record_edge->shape_x - swf_tag_shape->_current_x;
106         signed delta_y = shape_record_edge->shape_y - swf_tag_shape->_current_y;
107         if (delta_x && delta_y) {
108             shape_record_edge->shape_line_has_x_and_y = 1;
109         } else {
110             shape_record_edge->shape_line_has_x_and_y = 0;
111         }
112         bitstream_putbit(bs, shape_record_edge->shape_line_has_x_and_y);
113         if (shape_record_edge->shape_line_has_x_and_y == 1) {
114             bitstream_putbits_signed(bs, delta_x, shape_coord_real_size);
115             bitstream_putbits_signed(bs, delta_y, shape_coord_real_size);
116         } else {
117             if (delta_x) {
118                 shape_record_edge->shape_line_has_x_or_y = 0;
119             } else {
120                 shape_record_edge->shape_line_has_x_or_y = 1;
121             }
122             bitstream_putbit(bs, shape_record_edge->shape_line_has_x_or_y);
123             if (shape_record_edge->shape_line_has_x_or_y == 0) {
124                 bitstream_putbits_signed(bs, delta_x, shape_coord_real_size);
125             } else {
126                 bitstream_putbits_signed(bs, delta_y, shape_coord_real_size);
127             }
128         }
129         swf_tag_shape->_current_x = shape_record_edge->shape_x;
130         swf_tag_shape->_current_y = shape_record_edge->shape_y;
131     }
132     return 0;
133 }
134
135 int
136 swf_shape_record_edge_print(swf_shape_record_edge_t *shape_record_edge,
137                             int indent_depth) {
138     print_indent(indent_depth);
139     printf("shape_edge_type=%d  (shape_coord_size=%d+2)\n",
140            shape_record_edge->shape_edge_type ,
141            shape_record_edge->shape_coord_size);
142     if (shape_record_edge->shape_edge_type == 0) {
143         print_indent(indent_depth);
144         printf("shape_control_(x,y)=(%.2f,%.2f)  shape_anchor_(x,y)=(%.2f,%.2f)\n",
145                (float) shape_record_edge->shape_control_x / SWF_TWIPS,
146                (float) shape_record_edge->shape_control_y / SWF_TWIPS,
147                (float) shape_record_edge->shape_anchor_x / SWF_TWIPS,
148                (float) shape_record_edge->shape_anchor_y / SWF_TWIPS);
149     } else {
150         print_indent(indent_depth);
151 /*
152   printf("shape_line_has_(x_and_y, x_or_y)=(%d, %d)  ",
153                shape_record_edge->shape_line_has_x_and_y,
154                shape_record_edge->shape_line_has_x_or_y);
155 */
156         printf("shape_(x,y)=(%.2f,%.2f)\n",
157                (float) shape_record_edge->shape_x / SWF_TWIPS,
158                (float) shape_record_edge->shape_y / SWF_TWIPS);
159     }
160     return 0;
161 }