質問編集履歴
2
誤字の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -152,11 +152,11 @@
|
|
152
152
|
|
153
153
|
na = 0;
|
154
154
|
|
155
|
-
nr =
|
155
|
+
nr = max_val;
|
156
156
|
|
157
|
-
ng =
|
157
|
+
ng = max_val;
|
158
158
|
|
159
|
-
nb =
|
159
|
+
nb = max_val;
|
160
160
|
|
161
161
|
} else {
|
162
162
|
|
1
参考プログラム例示
test
CHANGED
File without changes
|
test
CHANGED
@@ -27,3 +27,163 @@
|
|
27
27
|
|
28
28
|
|
29
29
|
よろしくお願い致します。
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
まず png ファイルを ppm ファイルにしておき、以下のようなプログラムで、ppm ファイルから 色情報の ppm と、アルファチャンネルの pgm を生成、最後に、それらを結合して透過 png を生成…としてみることはできました。
|
34
|
+
|
35
|
+
もっといい方法があれば、教えて下さい。
|
36
|
+
|
37
|
+
```lang-bash
|
38
|
+
|
39
|
+
filename="a.png"
|
40
|
+
|
41
|
+
base=`basename $filename .png`
|
42
|
+
|
43
|
+
pngtopam $filename | pamtopnm -plain > ${base}.ppm
|
44
|
+
|
45
|
+
./pnmalpha ${base}.ppm ${base}_col.ppm ${base}_alpha.pgm
|
46
|
+
|
47
|
+
pnmtopng -force -alpha=${base}_alpha.pgm ${base}_col.ppm > ${base}_trans.png
|
48
|
+
|
49
|
+
```
|
50
|
+
|
51
|
+
```lang-C++
|
52
|
+
|
53
|
+
#include<iostream>
|
54
|
+
|
55
|
+
#include<fstream>
|
56
|
+
|
57
|
+
#include<algorithm>
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
int main(int argc, char **argv) {
|
62
|
+
|
63
|
+
if (argc < 4) {
|
64
|
+
|
65
|
+
std::cout << "too few arguments" << std::endl;
|
66
|
+
|
67
|
+
std::cout << "usage: src.ppm dst.ppm alpha.pgm" << std::endl;
|
68
|
+
|
69
|
+
return 1;
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
const std::string magic_ppm = "P3", magic_pgm = "P2";
|
76
|
+
|
77
|
+
std::string magic;
|
78
|
+
|
79
|
+
std::ifstream ifs(argv[1]);
|
80
|
+
|
81
|
+
std::ofstream ofs_color(argv[2]), ofs_alpha(argv[3]);
|
82
|
+
|
83
|
+
int w, h, max_val;
|
84
|
+
|
85
|
+
int r, g, b, min_rgb; // current val
|
86
|
+
|
87
|
+
int nr, ng, nb, na; // new val
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
if (!ifs || !ofs_color || !ofs_alpha) {
|
92
|
+
|
93
|
+
std::cerr << "file open error" << std::endl;
|
94
|
+
|
95
|
+
return 1;
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
ifs >> magic;
|
102
|
+
|
103
|
+
if (magic != magic_ppm) {
|
104
|
+
|
105
|
+
std::cerr << "input file format error" << std::endl;
|
106
|
+
|
107
|
+
return 1;
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
ifs >> w >> h;
|
112
|
+
|
113
|
+
ifs >> max_val;
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
ofs_color << magic_ppm << std::endl;
|
118
|
+
|
119
|
+
ofs_color << w << " " << h << std::endl;
|
120
|
+
|
121
|
+
ofs_color << max_val << std::endl;
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
ofs_alpha << magic_pgm << std::endl;
|
126
|
+
|
127
|
+
ofs_alpha << w << " " << h << std::endl;
|
128
|
+
|
129
|
+
ofs_alpha << max_val << std::endl;
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
for (int i = 0; i < h; ++i) {
|
134
|
+
|
135
|
+
for (int j = 0; j < w; ++j) {
|
136
|
+
|
137
|
+
ifs >> r >> g >> b;
|
138
|
+
|
139
|
+
min_rgb = std::min(r, std::min(g, b));
|
140
|
+
|
141
|
+
if (min_rgb == 0) {
|
142
|
+
|
143
|
+
na = max_val;
|
144
|
+
|
145
|
+
nr = r;
|
146
|
+
|
147
|
+
ng = g;
|
148
|
+
|
149
|
+
nb = b;
|
150
|
+
|
151
|
+
} else if (min_rgb == max_val) {
|
152
|
+
|
153
|
+
na = 0;
|
154
|
+
|
155
|
+
nr = 128;
|
156
|
+
|
157
|
+
ng = 128;
|
158
|
+
|
159
|
+
nb = 128;
|
160
|
+
|
161
|
+
} else {
|
162
|
+
|
163
|
+
na = max_val - min_rgb;
|
164
|
+
|
165
|
+
nr = (r + na - max_val) * max_val / na;
|
166
|
+
|
167
|
+
ng = (g + na - max_val) * max_val / na;
|
168
|
+
|
169
|
+
nb = (b + na - max_val) * max_val / na;
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
ofs_color << nr << " " << ng << " " << nb << " ";
|
174
|
+
|
175
|
+
ofs_alpha << na << " ";
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
ofs_color << std::endl;
|
180
|
+
|
181
|
+
ofs_alpha << std::endl;
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
return 0;
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
```
|