OSDN Git Service

delete png_set_gAMA
[swfed/swfed.git] / src / swf_shape_record.c
1 #include <stdio.h>
2 #include "bitstream.h"
3 #include "swf_shape_record.h"
4
5 int
6 swf_shape_record_parse(bitstream_t *bs, swf_shape_record_t *shape_record,
7                        swf_tag_t *tag) {
8     int first_bit, next_5bits;
9     swf_shape_record_t *current_record = shape_record;
10     int limit;
11     for (limit = 1; current_record ; limit ++) {
12         current_record->next = NULL; // stopper
13         int ret = bitstream_getbits(bs, 6);
14         if (ret == -1) {
15             fprintf(stderr, "swf_shape_record_parse: bitstream_getbits failed at L%d\n", __LINE__);
16             return ret;
17         }
18         current_record->first_6bits = ret;
19         first_bit = (current_record->first_6bits >> 5) & 1;
20         next_5bits = current_record->first_6bits & 0x1f;
21         bitstream_incrpos(bs, 0, -6); // 6 bit back
22         if ((first_bit == 0) && (next_5bits == 0)) {
23             ret = swf_shape_record_end_parse(bs, &(current_record->shape.shape_end));
24             if (ret) {
25                 fprintf(stderr, "swf_shape_record_parse: swf_shape_record_end_parse at L%d\n", __LINE__);
26                 return ret;
27             }
28             break; // end
29         } if (first_bit == 0) {
30             ret = swf_shape_record_setup_parse(bs, &(current_record->shape.shape_setup),
31                                          tag);
32             if (ret) {
33                 fprintf(stderr, "swf_shape_record_parse: swf_shape_record_setup_parse at L%d\n", __LINE__);
34                 return ret;
35             }
36 } else {
37             ret = swf_shape_record_edge_parse(bs, &(current_record->shape.shape_edge), tag);
38             if (ret) {
39                 fprintf(stderr, "swf_shape_record_parse: swf_shape_record_edge_parse at L%d\n", __LINE__);
40                 return ret;
41             }
42
43         }
44         if (100000 <= limit) { // XXX 100000???
45             current_record->next = NULL;
46             fprintf(stderr, "swf_shape_record_parse: limit(%d) over\n", limit);
47             return 1;
48         }
49         current_record->next = calloc(1, sizeof(swf_shape_record_t));
50         current_record = current_record->next;
51     }
52     return 0;
53 }
54
55 int
56 swf_shape_record_build(bitstream_t *bs, swf_shape_record_t *shape_record,
57                        swf_tag_t *tag) {
58     int first_bit, next_5bits;
59     swf_shape_record_t *current_record = shape_record;
60     while (current_record) {
61         first_bit = (current_record->first_6bits >> 5) & 1;
62         next_5bits = current_record->first_6bits & 0x1f;
63         if ((first_bit == 0) && (next_5bits == 0)) {
64             swf_shape_record_end_build(bs, &(current_record->shape.shape_end));
65             break;
66         } if (first_bit == 0) {
67             swf_shape_record_setup_build(bs, &(current_record->shape.shape_setup),
68                                          tag);
69         } else {
70             swf_shape_record_edge_build(bs, &(current_record->shape.shape_edge), tag);
71         }
72         current_record = current_record->next;
73     }
74     return 0;
75 }
76
77 int
78 swf_shape_record_print(swf_shape_record_t *shape_record, int indent_depth,
79                        swf_tag_t *tag) {
80     int first_bit, next_5bits;
81     swf_shape_record_t *current_record = shape_record;
82     int i;
83     for (i = 0 ; current_record ; i++) {
84         first_bit = (current_record->first_6bits >> 5) & 1;
85         next_5bits = current_record->first_6bits & 0x1f;
86         print_indent(indent_depth);
87         printf("shape_record [%d]  ", i);
88         if ((first_bit == 0) && (next_5bits == 0)) {
89             printf("end\n");
90             swf_shape_record_end_print(indent_depth + 1);
91             break;
92         } if (first_bit == 0) {
93             printf("setup\n");
94             swf_shape_record_setup_print(&(current_record->shape.shape_setup),
95                                          indent_depth + 1, tag);
96         } else {
97             printf("edge\n");
98             swf_shape_record_edge_print(&(current_record->shape.shape_edge),
99                                         indent_depth + 1);
100         }
101         current_record = current_record->next;
102     }
103     return 0;
104 }
105
106 int
107 swf_shape_record_delete(swf_shape_record_t *shape_record) {
108     swf_shape_record_t *current, *next;
109     int first_bit, next_5bits;
110     
111     first_bit = (shape_record->first_6bits >> 5) & 1;
112     next_5bits = shape_record->first_6bits & 0x1f;
113
114     if ((first_bit == 0) && (next_5bits != 0)) {
115         swf_shape_record_setup_delete(&(shape_record->shape.shape_setup));
116     }
117
118     current = shape_record->next;
119     while (current) {
120         first_bit = (current->first_6bits >> 5) & 1;
121         next_5bits = current->first_6bits & 0x1f;
122         if ((first_bit == 0) && (next_5bits != 0)) {
123             swf_shape_record_setup_delete(&(current->shape.shape_setup));
124         }
125         next = current->next;
126         free(current);
127         current = next;
128     }
129     return 0;
130 }
131
132 int
133 swf_shape_record_edge_apply_factor(swf_shape_record_t *shape_record,
134                                    double scale_x, double scale_y,
135                                    signed trans_x, signed trans_y) {
136     int first_bit, next_5bits;
137     swf_shape_record_t *current_record;
138
139     // top-left base adjust
140     signed min_x = SWF_TWIPS*10000, min_y = SWF_TWIPS*10000;
141     for (current_record = shape_record ; current_record ; current_record = current_record->next) {
142         first_bit = (current_record->first_6bits >> 5) & 1;
143         next_5bits = current_record->first_6bits & 0x1f;
144         if (first_bit) { // edge
145             swf_shape_record_edge_t *edge = &(current_record->shape.shape_edge);
146             min_x = (edge->shape_x<min_x)?edge->shape_x:min_x;
147             min_y = (edge->shape_y<min_y)?edge->shape_y:min_y;
148         } else if (next_5bits) { // setup
149             swf_shape_record_setup_t *setup = &(current_record->shape.shape_setup);
150             min_x = (setup->shape_move_x<min_x)?setup->shape_move_x:min_x;
151             min_y = (setup->shape_move_y<min_y)?setup->shape_move_y:min_y;
152         } else { // end
153             break;
154         }
155     }
156     // scale and trans
157     for (current_record = shape_record ; current_record ; current_record = current_record->next) {
158         first_bit = (current_record->first_6bits >> 5) & 1;
159         next_5bits = current_record->first_6bits & 0x1f;
160         if (first_bit) { // edge
161             swf_shape_record_edge_t *edge = &(current_record->shape.shape_edge);
162             edge->shape_x = (edge->shape_x - min_x) * scale_x + min_x + trans_x * SWF_TWIPS;
163             edge->shape_y = (edge->shape_y - min_y) * scale_y + min_y + trans_y * SWF_TWIPS;
164         } else if (next_5bits) { // setup
165             swf_shape_record_setup_t *setup = &(current_record->shape.shape_setup);
166             setup->shape_move_x = (setup->shape_move_x - min_x) * scale_x + min_x + trans_x * SWF_TWIPS;
167             setup->shape_move_y = (setup->shape_move_y - min_y) * scale_y + min_y + trans_y * SWF_TWIPS;
168         } else { // end
169             break;
170         }
171     }
172     return 0;
173 }