Changeset View
Changeset View
Standalone View
Standalone View
imagelib/effects/kpEffectReduceColors.cpp
Show First 20 Lines • Show All 68 Lines • ▼ Show 20 Line(s) | |||||
69 | QImage kpEffectReduceColors::convertImageDepth (const QImage &image, int depth, bool dither) | 69 | QImage kpEffectReduceColors::convertImageDepth (const QImage &image, int depth, bool dither) | ||
70 | { | 70 | { | ||
71 | qCDebug(kpLogImagelib) << "kpeffectreducecolors.cpp:ConvertImageDepth() changing image (w=" << image.width () | 71 | qCDebug(kpLogImagelib) << "kpeffectreducecolors.cpp:ConvertImageDepth() changing image (w=" << image.width () | ||
72 | << ",h=" << image.height () | 72 | << ",h=" << image.height () | ||
73 | << ") depth from " << image.depth () | 73 | << ") depth from " << image.depth () | ||
74 | << " to " << depth | 74 | << " to " << depth | ||
75 | << " (dither=" << dither << ")"; | 75 | << " (dither=" << dither << ")"; | ||
76 | 76 | | |||
77 | if (image.isNull ()) | 77 | if (image.isNull ()) { | ||
78 | return image; | 78 | return image; | ||
79 | } | ||||
79 | 80 | | |||
80 | if (depth == image.depth ()) | 81 | if (depth == image.depth ()) { | ||
81 | return image; | 82 | return image; | ||
83 | } | ||||
82 | 84 | | |||
83 | 85 | | |||
84 | for (int y = 0; y < image.height (); y++) | 86 | for (int y = 0; y < image.height (); y++) | ||
85 | { | 87 | { | ||
86 | for (int x = 0; x < image.width (); x++) | 88 | for (int x = 0; x < image.width (); x++) | ||
87 | { | 89 | { | ||
88 | fprintf (stderr, " %08X", image.pixel (x, y)); | 90 | fprintf (stderr, " %08X", image.pixel (x, y)); | ||
89 | } | 91 | } | ||
Show All 16 Lines | 105 | { | |||
106 | bool color0Valid = false, color1Valid = false; | 108 | bool color0Valid = false, color1Valid = false; | ||
107 | 109 | | |||
108 | bool moreThan2Colors = false; | 110 | bool moreThan2Colors = false; | ||
109 | 111 | | |||
110 | QImage monoImage (image.width (), image.height (), QImage::Format_MonoLSB); | 112 | QImage monoImage (image.width (), image.height (), QImage::Format_MonoLSB); | ||
111 | monoImage.setColorCount (2); | 113 | monoImage.setColorCount (2); | ||
112 | qCDebug(kpLogImagelib) << "\t\tinitialising output image w=" << monoImage.width () | 114 | qCDebug(kpLogImagelib) << "\t\tinitialising output image w=" << monoImage.width () | ||
113 | << ",h=" << monoImage.height () | 115 | << ",h=" << monoImage.height () | ||
114 | << ",d=" << monoImage.depth () | 116 | << ",d=" << monoImage.depth (); | ||
115 | << endl; | | |||
116 | for (int y = 0; y < image.height (); y++) | 117 | for (int y = 0; y < image.height (); y++) | ||
117 | { | 118 | { | ||
118 | for (int x = 0; x < image.width (); x++) | 119 | for (int x = 0; x < image.width (); x++) | ||
119 | { | 120 | { | ||
120 | // (this can be transparent) | 121 | // (this can be transparent) | ||
121 | QRgb imagePixel = image.pixel (x, y); | 122 | QRgb imagePixel = image.pixel (x, y); | ||
122 | 123 | | |||
123 | if (color0Valid && imagePixel == color0) | 124 | if (color0Valid && imagePixel == color0) { | ||
124 | monoImage.setPixel (x, y, 0); | 125 | monoImage.setPixel (x, y, 0); | ||
125 | else if (color1Valid && imagePixel == color1) | 126 | } | ||
127 | else if (color1Valid && imagePixel == color1) { | ||||
126 | monoImage.setPixel (x, y, 1); | 128 | monoImage.setPixel (x, y, 1); | ||
127 | else if (!color0Valid) | 129 | } | ||
128 | { | 130 | else if (!color0Valid) { | ||
129 | color0 = imagePixel; | 131 | color0 = imagePixel; | ||
130 | color0Valid = true; | 132 | color0Valid = true; | ||
131 | monoImage.setPixel (x, y, 0); | 133 | monoImage.setPixel (x, y, 0); | ||
132 | qCDebug(kpLogImagelib) << "\t\t\tcolor0=" << (int *) color0 | 134 | qCDebug(kpLogImagelib) << "\t\t\tcolor0=" << (int *) color0 | ||
133 | << " at x=" << x << ",y=" << y << endl; | 135 | << " at x=" << x << ",y=" << y; | ||
134 | } | 136 | } | ||
135 | else if (!color1Valid) | 137 | else if (!color1Valid) | ||
136 | { | 138 | { | ||
137 | color1 = imagePixel; | 139 | color1 = imagePixel; | ||
138 | color1Valid = true; | 140 | color1Valid = true; | ||
139 | monoImage.setPixel (x, y, 1); | 141 | monoImage.setPixel (x, y, 1); | ||
140 | qCDebug(kpLogImagelib) << "\t\t\tcolor1=" << (int *) color1 | 142 | qCDebug(kpLogImagelib) << "\t\t\tcolor1=" << (int *) color1 | ||
141 | << " at x=" << x << ",y=" << y << endl; | 143 | << " at x=" << x << ",y=" << y; | ||
142 | } | 144 | } | ||
143 | else | 145 | else | ||
144 | { | 146 | { | ||
145 | qCDebug(kpLogImagelib) << "\t\t\timagePixel=" << (int *) imagePixel | 147 | qCDebug(kpLogImagelib) << "\t\t\timagePixel=" << (int *) imagePixel | ||
146 | << " at x=" << x << ",y=" << y | 148 | << " at x=" << x << ",y=" << y | ||
147 | << " moreThan2Colors - abort hack" << endl; | 149 | << " moreThan2Colors - abort hack"; | ||
148 | moreThan2Colors = true; | 150 | moreThan2Colors = true; | ||
149 | 151 | | |||
150 | // Dijkstra, this is clearer than double break'ing or | 152 | // Dijkstra, this is clearer than double break'ing or | ||
151 | // a check in both loops | 153 | // a check in both loops | ||
152 | goto exit_loop; | 154 | goto exit_loop; | ||
153 | } | 155 | } | ||
154 | } | 156 | } | ||
155 | } | 157 | } | ||
Show All 28 Lines | |||||
184 | return retImage; | 186 | return retImage; | ||
185 | } | 187 | } | ||
186 | 188 | | |||
187 | //--------------------------------------------------------------------- | 189 | //--------------------------------------------------------------------- | ||
188 | 190 | | |||
189 | // public static | 191 | // public static | ||
190 | void kpEffectReduceColors::applyEffect (QImage *destPtr, int depth, bool dither) | 192 | void kpEffectReduceColors::applyEffect (QImage *destPtr, int depth, bool dither) | ||
191 | { | 193 | { | ||
192 | if (!destPtr) | 194 | if (!destPtr) { | ||
193 | return; | 195 | return; | ||
196 | } | ||||
194 | 197 | | |||
195 | // You can't "reduce" to 32-bit since it's the highest depth. | 198 | // You can't "reduce" to 32-bit since it's the highest depth. | ||
196 | if (depth != 1 && depth != 8) | 199 | if (depth != 1 && depth != 8) { | ||
197 | return; | 200 | return; | ||
201 | } | ||||
198 | 202 | | |||
199 | *destPtr = convertImageDepth(*destPtr, depth, dither); | 203 | *destPtr = convertImageDepth(*destPtr, depth, dither); | ||
200 | 204 | | |||
201 | // internally we always use QImage::Format_ARGB32_Premultiplied and | 205 | // internally we always use QImage::Format_ARGB32_Premultiplied and | ||
202 | // this effect is just an "effect" in that it changes the image (the look) somehow | 206 | // this effect is just an "effect" in that it changes the image (the look) somehow | ||
203 | // When one wants a different depth on the file, then he needs to save the image | 207 | // When one wants a different depth on the file, then he needs to save the image | ||
204 | // in that depth | 208 | // in that depth | ||
205 | *destPtr = destPtr->convertToFormat(QImage::Format_ARGB32_Premultiplied); | 209 | *destPtr = destPtr->convertToFormat(QImage::Format_ARGB32_Premultiplied); | ||
Show All 12 Lines |