【问题标题】:Need help using libpng to read an image需要帮助使用 libpng 读取图像
【发布时间】:2011-02-11 19:03:28
【问题描述】:

这是我的功能...我不知道为什么它不起作用。生成的图像看起来不像 .png 的样子。但也没有错误。

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 只是一个简单的结构体,用于保存 x、y、宽度和高度,而 imageData 是该结构体的数组。

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

这是输入和输出图形的并排屏幕截图(尺寸 8x30)(我在一分钟内进行了测试),这是在将 alpha 设置为 255 以使其显示之后(因为 alpha我回来是1)。左边是原创,右边是通过这个函数阅读后发生的事情。扩大 400% 以提高可见度。

http://img217.imageshack.us/img217/1627/testbullet.png

以下是跟踪记录:

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

在这个问题上卡了几天,所以如果我能得到任何帮助,我将不胜感激。

编辑:这是另外 2 组之前和之后的比较......再一次,在所有跟踪中,alpha 为 1,在截屏之前更改为 255。

http://img707.imageshack.us/img707/6953/82597335.png

【问题讨论】:

  • 谢谢罗杰!除了添加缩进之外没有其他方法吗?有没有办法自动添加缩进而不是手动?
  • @jonathan:不客气。 Ctrl-k 或者有一个工具栏按钮,或者在选择文本之后。 editing help 可能有用(即使它不包括快捷方式或工具栏)。
  • 啊。再次感谢。这正是编辑帮助没有用的原因 - 我尝试使用
     但效果不佳。从来没想过看工具栏,因为我从不使用这类富文本工具栏:)
  • 是的,我对这类工具栏也有同样的感觉,只是需要可发现快捷方式(所以在菜单项上显示它们!但在网页上很难做到菜单......)。顺便说一句,我对 libpng 不熟悉,现在阅读所有文档以给出足够的答案会有点多,但看起来你正在翻转通道(绿色变为蓝色)和行/列。 (你的字节顺序正确吗?)
  • 是的,我不希望完全不熟悉它的人能够通过文档给出答案....除非我的问题与库本身无关就像你说的那样搞乱了频道/行/列哈哈。我会再检查一次。我要交换的唯一通道是 Alpha 通道,因此从 RGBA(默认为 .png)切换到 ARGB(默认为 flash),但该转换是通过库中内置的函数完成的,所以我不希望这样错了

标签: c++ image-processing libpng alchemy


【解决方案1】:

我相信你在这一行有一个错误: memcpy(&amp;imageData[pos].data[i*width], &amp;row_pointers[i], width*sizeof(unsigned int)); 行指针的类型似乎是 char**。这意味着您正在将指向 char 的指针复制到您的图像数据!

编译器没有报告错误,因为所有指针都可以隐式转换为void*,这是一个指针。

【讨论】:

  • 是的,行指针的类型是 unsigned char** 并且 ... 是有道理的。哈哈哈我真的累了,我试试看。
  • 哇..我不敢相信..这毕竟是问题所在。我怎么会忽略这一点。
猜你喜欢
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 2015-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-13
  • 1970-01-01
相关资源
最近更新 更多