Eleganterer Lösungsweg?
-
Hi,
zum Problem: Ich habe eine Grafikdatei die ich in ein Array schreibe und vorher auf transparente pixel getestet habe (transparent_ = true/false).
Beim Darstellen frage ich vorher ab ob beim Darstellen überhaupt auf die Transparenz eingegangen werden muss, wenn ja wird jede Position im Array mit der Transparenten Farbe getestet.
Das blöde ist, im if/else ist der Code fast zu 99% identisch bis auf die if-abfrage um die Position im Array auf die Transparente Farbe zu vergleichen.
source = buffer_ + srcLeft + srcTop * width_; srcWidth = srcRight - srcLeft; if (transparent_) { for (unsigned long i=srcTop; i<srcBottom; ++i) { for (unsigned long j=0; j<srcWidth; ++j) { if (source[j] != core::directdraw::getInstance().rgbInfo_.transColor) dest[j] = source[j]; } source += width_; dest += core::directdraw::getInstance().width_; } } else { for (unsigned long i=srcTop; i<srcBottom; ++i) { for (unsigned long j=0; j<srcWidth; ++j) dest[j] = source[j]; source += width_; dest += core::directdraw::getInstance().width_; } }
Jetzt suche ich nach einem etwas eleganteren Lösungsweg, denn irgendwie ist das Platz-Verschwendung. Würde ich das if/else für Transparenz entfernen wäre dies auch keine gute Lösung denn schon bei kleineren Grafiken wie z.B. einer 640x480 merkt man den Unterschied sehr, ob Transparenz enthalten ist oder nicht.
Bin für jeden Vorschlag offen!
Danke im voraus.
-
source = buffer_ + srcLeft + srcTop * width_; srcWidth = srcRight - srcLeft; for (unsigned long i=srcTop; i<srcBottom; ++i) { for (unsigned long j=0; j<srcWidth; ++j) { if (!transparent_ || source[j] != core::directdraw::getInstance().rgbInfo_.transColor) dest[j] = source[j]; } source += width_; dest += core::directdraw::getInstance().width_; } }
-
Hi,
diesen Ansatz hatte ich schon ausprobiert mit keinem positiven Ergebnis. Laut Profiler ist das genau so "schnell" bei Grafiken ohne Transparenz als wenn ich jetzt jeden Pixel der Grafik auf transparenz testen würde.
-
Mögliche Lösung:
struct Transparent { static bool shallCopy(int source) { return source != core::directdraw::getInstance().rgbInfo_.transColor; } }; struct NotTransparent { static bool shallCopy(int) { return true; } }; template<class Copy> void copy() { source = buffer_ + srcLeft + srcTop * width_; srcWidth = srcRight - srcLeft; for (unsigned long i=srcTop; i<srcBottom; ++i) { for (unsigned long j=0; j<srcWidth; ++j) { if (Copy::shallCopy(source[j])) dest[j] = source[j]; } source += width_; dest += core::directdraw::getInstance().width_; } } if(transparent_) copy<Transparent>(); else copy<NotTransparent>();
-
Shade Of Mine schrieb:
Mögliche Lösung:
struct Transparent { static bool shallCopy(int source) { return source != core::directdraw::getInstance().rgbInfo_.transColor; } }; struct NotTransparent { static bool shallCopy(int) { return true; } }; template<class Copy> void copy() { source = buffer_ + srcLeft + srcTop * width_; srcWidth = srcRight - srcLeft; for (unsigned long i=srcTop; i<srcBottom; ++i) { for (unsigned long j=0; j<srcWidth; ++j) { if (Copy::shallCopy(source[j])) dest[j] = source[j]; } source += width_; dest += core::directdraw::getInstance().width_; } } if(transparent_) copy<Transparent>(); else copy<NotTransparent>();
interessant.. kostet das if(Copy::shallCopy()) dann irgendwelche zeit bei dem NotTransparent struct oder wirds wegoptimiert?
-
Life|work schrieb:
interessant.. kostet das if(Copy::shallCopy()) dann irgendwelche zeit bei dem NotTransparent struct oder wirds wegoptimiert?
wenn der compiler auch nur ein bisschen was taugt, wirds wegoptimiert. ich könnte mir allerdings vorstellen, dass man hier noch mehr herausholen könnte, im not-transparent fall bietet sich for_each in combination mit copy and, im anderen fall evtl. for_each mit replace_copy_if - muss nicht besser sein, aber einfach mal ausprobieren.
-
@Shade Of Mine
also irgendwie verschwendet der neue code jetzt noch mehr platz wie der alte
und komplizierter ist er auch noch*grübel*