views:

187

answers:

1

Here is my function... I don't know why it's not working. The resulting image looks nothing like what the .png looks like. But there's no errors either.

bool Bullet::read_png(std::string file_name, int pos)
{
    png_structp png_ptr;
    png_infop info_ptr;
    FILE *fp;

    if ((fp = fopen(file_name.c_str(), "rb")) == NULL) {
        return false;
    }

    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

    if (png_ptr == NULL) {
        fclose(fp);
        return false;
    }

    info_ptr = png_create_info_struct(png_ptr);
    if (info_ptr == NULL) {
        fclose(fp);
        png_destroy_read_struct(&png_ptr, NULL, NULL);
        return false;
    }

    if (setjmp(png_jmpbuf(png_ptr))) {
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
        fclose(fp);
        return false;
    }

    png_init_io(png_ptr, fp);

    png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_SWAP_ALPHA | PNG_TRANSFORM_EXPAND, NULL);

    png_uint_32 width = png_get_image_width(png_ptr, info_ptr);
    png_uint_32 height = png_get_image_height(png_ptr, info_ptr);

    png_uint_32 bitdepth   = png_get_bit_depth(png_ptr, info_ptr);
    png_uint_32 channels   = png_get_channels(png_ptr, info_ptr);
    png_uint_32 color_type = png_get_color_type(png_ptr, info_ptr);

    AS3_Trace(AS3_String("BitDepth"));
    AS3_Trace(AS3_Int(bitdepth));
    AS3_Trace(AS3_String("Channels"));
    AS3_Trace(AS3_Int(channels));
    AS3_Trace(AS3_String("ColorType"));
    AS3_Trace(AS3_Int(color_type));

    imageData[pos].width = width;
    imageData[pos].height = height;

    png_bytepp row_pointers;
    row_pointers = png_get_rows(png_ptr, info_ptr);

    imageData[pos].data = new unsigned int[width*height];

    for (unsigned int i=0; i < height; ++i) {
        memcpy(&imageData[pos].data[i*width], &row_pointers[i], width*sizeof(unsigned int));
    }

    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
    fclose(fp);

    for (unsigned int i=0; i < height; ++i) {
        for (unsigned int j=0; j < width; ++j) {
            unsigned int val = imageData[pos].data[i*width+j];
            unsigned int a = ((val >> 24));
            unsigned int r = (((val - (a << 24)) >> 16));
            unsigned int g = (((val - (a << 24) - (r << 16)) >> 8));
            unsigned int b = (((val - (a << 24) - (r << 16) - (g << 8))));

            std::string s(AS3_StringValue(AS3_Int(i*width+j)));
            s += " ";
            s += AS3_StringValue(AS3_Int(val));
            s += " ";
            s += AS3_StringValue(AS3_Int(a));
            s += " ";
            s += AS3_StringValue(AS3_Int(r));
            s += " ";
            s += AS3_StringValue(AS3_Int(g));
            s += " ";
            s += AS3_StringValue(AS3_Int(b));
            AS3_Trace(AS3_String(s.c_str()));
        }
    }

    return true;
}

ImageData is just a simple struct to keep x, y, width, and height, and imageData is an array of that struct.

struct ImageData {
    int x;
    int y;
    int width;
    int height;
    unsigned int* data;
};

Here is a side by side screenshot of the input and output graphics (dimensions 8x30) (something I made in a minute for testing), and this was after setting alpha to 255 in order to make it show up (because the alpha I was getting back was 1). Left side is original, right side is what happened after reading it through this function. Scaled up 400% for visibility.

Here is a log of the traces:

BitDepth
8
Channels
4
ColorType
6  // RGBA

//^ All of my pngs will be of the above type    

0 16855328 1 1 49 32
1 16855424 1 1 49 128
2 16855456 1 1 49 160
3 16855488 1 1 49 192
4 16855520 1 1 49 224
5 16855552 1 1 50 0
6 16855584 1 1 50 32
7 16855616 1 1 50 64
8 16855424 1 1 49 128
9 16855456 1 1 49 160
10 16855488 1 1 49 192
11 16855520 1 1 49 224
12 16855552 1 1 50 0
13 16855584 1 1 50 32
14 16855616 1 1 50 64
15 16855648 1 1 50 96
16 16855456 1 1 49 160
17 16855488 1 1 49 192
18 16855520 1 1 49 224
19 16855552 1 1 50 0
20 16855584 1 1 50 32
21 16855616 1 1 50 64
22 16855648 1 1 50 96
23 16855680 1 1 50 128
24 16855488 1 1 49 192
25 16855520 1 1 49 224
26 16855552 1 1 50 0
27 16855584 1 1 50 32
28 16855616 1 1 50 64
29 16855648 1 1 50 96
30 16855680 1 1 50 128
31 16855712 1 1 50 160
32 16855520 1 1 49 224
33 16855552 1 1 50 0
34 16855584 1 1 50 32
35 16855616 1 1 50 64
36 16855648 1 1 50 96
37 16855680 1 1 50 128
38 16855712 1 1 50 160
39 16855744 1 1 50 192
40 16855552 1 1 50 0
41 16855584 1 1 50 32
42 16855616 1 1 50 64
43 16855648 1 1 50 96
44 16855680 1 1 50 128
45 16855712 1 1 50 160
46 16855744 1 1 50 192
47 16855776 1 1 50 224
48 16855584 1 1 50 32
49 16855616 1 1 50 64
50 16855648 1 1 50 96
51 16855680 1 1 50 128
52 16855712 1 1 50 160
53 16855744 1 1 50 192
54 16855776 1 1 50 224
55 16855808 1 1 51 0
56 16855616 1 1 50 64
57 16855648 1 1 50 96
58 16855680 1 1 50 128
59 16855712 1 1 50 160
60 16855744 1 1 50 192
61 16855776 1 1 50 224
62 16855808 1 1 51 0
63 16855840 1 1 51 32
64 16855648 1 1 50 96
65 16855680 1 1 50 128
66 16855712 1 1 50 160
67 16855744 1 1 50 192
68 16855776 1 1 50 224
69 16855808 1 1 51 0
70 16855840 1 1 51 32
71 16855872 1 1 51 64
72 16855680 1 1 50 128
73 16855712 1 1 50 160
74 16855744 1 1 50 192
75 16855776 1 1 50 224
76 16855808 1 1 51 0
77 16855840 1 1 51 32
78 16855872 1 1 51 64
79 16855904 1 1 51 96
80 16855712 1 1 50 160
81 16855744 1 1 50 192
82 16855776 1 1 50 224
83 16855808 1 1 51 0
84 16855840 1 1 51 32
85 16855872 1 1 51 64
86 16855904 1 1 51 96
87 16855936 1 1 51 128
88 16855744 1 1 50 192
89 16855776 1 1 50 224
90 16855808 1 1 51 0
91 16855840 1 1 51 32
92 16855872 1 1 51 64
93 16855904 1 1 51 96
94 16855936 1 1 51 128
95 16855968 1 1 51 160
96 16855776 1 1 50 224
97 16855808 1 1 51 0
98 16855840 1 1 51 32
99 16855872 1 1 51 64
100 16855904 1 1 51 96
101 16855936 1 1 51 128
102 16855968 1 1 51 160
103 16856000 1 1 51 192
104 16855808 1 1 51 0
105 16855840 1 1 51 32
106 16855872 1 1 51 64
107 16855904 1 1 51 96
108 16855936 1 1 51 128
109 16855968 1 1 51 160
110 16856000 1 1 51 192
111 16856032 1 1 51 224
112 16855840 1 1 51 32
113 16855872 1 1 51 64
114 16855904 1 1 51 96
115 16855936 1 1 51 128
116 16855968 1 1 51 160
117 16856000 1 1 51 192
118 16856032 1 1 51 224
119 16856064 1 1 52 0
120 16855872 1 1 51 64
121 16855904 1 1 51 96
122 16855936 1 1 51 128
123 16855968 1 1 51 160
124 16856000 1 1 51 192
125 16856032 1 1 51 224
126 16856064 1 1 52 0
127 16856096 1 1 52 32
128 16855904 1 1 51 96
129 16855936 1 1 51 128
130 16855968 1 1 51 160
131 16856000 1 1 51 192
132 16856032 1 1 51 224
133 16856064 1 1 52 0
134 16856096 1 1 52 32
135 16856128 1 1 52 64
136 16855936 1 1 51 128
137 16855968 1 1 51 160
138 16856000 1 1 51 192
139 16856032 1 1 51 224
140 16856064 1 1 52 0
141 16856096 1 1 52 32
142 16856128 1 1 52 64
143 16856160 1 1 52 96
144 16855968 1 1 51 160
145 16856000 1 1 51 192
146 16856032 1 1 51 224
147 16856064 1 1 52 0
148 16856096 1 1 52 32
149 16856128 1 1 52 64
150 16856160 1 1 52 96
151 16856192 1 1 52 128
152 16856000 1 1 51 192
153 16856032 1 1 51 224
154 16856064 1 1 52 0
155 16856096 1 1 52 32
156 16856128 1 1 52 64
157 16856160 1 1 52 96
158 16856192 1 1 52 128
159 16856224 1 1 52 160
160 16856032 1 1 51 224
161 16856064 1 1 52 0
162 16856096 1 1 52 32
163 16856128 1 1 52 64
164 16856160 1 1 52 96
165 16856192 1 1 52 128
166 16856224 1 1 52 160
167 16856256 1 1 52 192
168 16856064 1 1 52 0
169 16856096 1 1 52 32
170 16856128 1 1 52 64
171 16856160 1 1 52 96
172 16856192 1 1 52 128
173 16856224 1 1 52 160
174 16856256 1 1 52 192
175 16856288 1 1 52 224
176 16856096 1 1 52 32
177 16856128 1 1 52 64
178 16856160 1 1 52 96
179 16856192 1 1 52 128
180 16856224 1 1 52 160
181 16856256 1 1 52 192
182 16856288 1 1 52 224
183 16856320 1 1 53 0
184 16856128 1 1 52 64
185 16856160 1 1 52 96
186 16856192 1 1 52 128
187 16856224 1 1 52 160
188 16856256 1 1 52 192
189 16856288 1 1 52 224
190 16856320 1 1 53 0
191 0 0 0 0 0
192 16856160 1 1 52 96
193 16856192 1 1 52 128
194 16856224 1 1 52 160
195 16856256 1 1 52 192
196 16856288 1 1 52 224
197 16856320 1 1 53 0
198 0 0 0 0 0
199 0 0 0 0 0
200 16856192 1 1 52 128
201 16856224 1 1 52 160
202 16856256 1 1 52 192
203 16856288 1 1 52 224
204 16856320 1 1 53 0
205 0 0 0 0 0
206 0 0 0 0 0
207 0 0 0 0 0
208 16856224 1 1 52 160
209 16856256 1 1 52 192
210 16856288 1 1 52 224
211 16856320 1 1 53 0
212 0 0 0 0 0
213 0 0 0 0 0
214 0 0 0 0 0
215 0 0 0 0 0
216 16856256 1 1 52 192
217 16856288 1 1 52 224
218 16856320 1 1 53 0
219 0 0 0 0 0
220 0 0 0 0 0
221 0 0 0 0 0
222 0 0 0 0 0
223 0 0 0 0 0
224 16856288 1 1 52 224
225 16856320 1 1 53 0
226 0 0 0 0 0
227 0 0 0 0 0
228 0 0 0 0 0
229 0 0 0 0 0
230 0 0 0 0 0
231 0 0 0 0 0
232 16856320 1 1 53 0
233 0 0 0 0 0
234 0 0 0 0 0
235 0 0 0 0 0
236 0 0 0 0 0
237 0 0 0 0 0
238 0 0 0 0 0
239 0 0 0 0 0

Was stuck on this for a couple of days, so I would appreciate any help I can get.

Edit: Here's another 2 sets of before and after comparisons... once again, in all of the traces the alpha was 1, and was changed to 255 before the screenshots were taken.

+4  A: 

I believe you have an error at this line: memcpy(&imageData[pos].data[i*width], &row_pointers[i], width*sizeof(unsigned int)); the type of row pointers seems to be char**. that would mean you are copying pointers to char to your image data!

the compiler reports no error because all pointers are implicitly convertible to void* and this ìs a pointer.

flownt
yeah, the type of row pointers is unsigned char**and that ... would make sense. hahaha i was really tired, i'll try it out.
jonathanasdf
wow.. I can't believe it.. that was the problem after all. How could I have overlooked that.
jonathanasdf