+// Propagate alpha information as far as we can in the graph.
+// Similar to propagate_gamma_and_color_space().
+void EffectChain::propagate_alpha()
+{
+ // We depend on going through the nodes in order.
+ sort_all_nodes_topologically();
+
+ for (unsigned i = 0; i < nodes.size(); ++i) {
+ Node *node = nodes[i];
+ if (node->disabled) {
+ continue;
+ }
+ assert(node->incoming_links.size() == node->effect->num_inputs());
+ if (node->incoming_links.size() == 0) {
+ assert(node->output_alpha_type != ALPHA_INVALID);
+ continue;
+ }
+
+ // The alpha multiplication/division effects are special cases.
+ if (node->effect->effect_type_id() == "AlphaMultiplicationEffect") {
+ assert(node->incoming_links.size() == 1);
+ assert(node->incoming_links[0]->output_alpha_type == ALPHA_POSTMULTIPLIED);
+ node->output_alpha_type = ALPHA_PREMULTIPLIED;
+ continue;
+ }
+ if (node->effect->effect_type_id() == "AlphaDivisionEffect") {
+ assert(node->incoming_links.size() == 1);
+ assert(node->incoming_links[0]->output_alpha_type == ALPHA_PREMULTIPLIED);
+ node->output_alpha_type = ALPHA_POSTMULTIPLIED;
+ continue;
+ }
+
+ // GammaCompressionEffect and GammaExpansionEffect are also a special case,
+ // because they are the only one that _need_ postmultiplied alpha.
+ if (node->effect->effect_type_id() == "GammaCompressionEffect" ||
+ node->effect->effect_type_id() == "GammaExpansionEffect") {
+ assert(node->incoming_links.size() == 1);
+ if (node->incoming_links[0]->output_alpha_type == ALPHA_BLANK) {
+ node->output_alpha_type = ALPHA_BLANK;
+ } else if (node->incoming_links[0]->output_alpha_type == ALPHA_POSTMULTIPLIED) {
+ node->output_alpha_type = ALPHA_POSTMULTIPLIED;
+ } else {
+ node->output_alpha_type = ALPHA_INVALID;
+ }
+ continue;
+ }
+
+ // Only inputs can have unconditional alpha output (OUTPUT_BLANK_ALPHA
+ // or OUTPUT_POSTMULTIPLIED_ALPHA), and they have already been
+ // taken care of above. Rationale: Even if you could imagine
+ // e.g. an effect that took in an image and set alpha=1.0
+ // unconditionally, it wouldn't make any sense to have it as
+ // e.g. OUTPUT_BLANK_ALPHA, since it wouldn't know whether it
+ // got its input pre- or postmultiplied, so it wouldn't know
+ // whether to divide away the old alpha or not.
+ Effect::AlphaHandling alpha_handling = node->effect->alpha_handling();
+ assert(alpha_handling == Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA ||
+ alpha_handling == Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK ||
+ alpha_handling == Effect::DONT_CARE_ALPHA_TYPE);
+
+ // If the node has multiple inputs, check that they are all valid and
+ // the same.
+ bool any_invalid = false;
+ bool any_premultiplied = false;
+ bool any_postmultiplied = false;
+
+ for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
+ switch (node->incoming_links[j]->output_alpha_type) {
+ case ALPHA_INVALID:
+ any_invalid = true;
+ break;
+ case ALPHA_BLANK:
+ // Blank is good as both pre- and postmultiplied alpha,
+ // so just ignore it.
+ break;
+ case ALPHA_PREMULTIPLIED:
+ any_premultiplied = true;
+ break;
+ case ALPHA_POSTMULTIPLIED:
+ any_postmultiplied = true;
+ break;
+ default:
+ assert(false);
+ }
+ }
+
+ if (any_invalid) {
+ node->output_alpha_type = ALPHA_INVALID;
+ continue;
+ }
+
+ // Inputs must be of the same type.
+ if (any_premultiplied && any_postmultiplied) {
+ node->output_alpha_type = ALPHA_INVALID;
+ continue;
+ }
+
+ if (alpha_handling == Effect::INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA ||
+ alpha_handling == Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK) {
+ // If the effect has asked for premultiplied alpha, check that it has got it.
+ if (any_postmultiplied) {
+ node->output_alpha_type = ALPHA_INVALID;
+ } else if (!any_premultiplied &&
+ alpha_handling == Effect::INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK) {
+ // Blank input alpha, and the effect preserves blank alpha.
+ node->output_alpha_type = ALPHA_BLANK;
+ } else {
+ node->output_alpha_type = ALPHA_PREMULTIPLIED;
+ }
+ } else {
+ // OK, all inputs are the same, and this effect is not going
+ // to change it.
+ assert(alpha_handling == Effect::DONT_CARE_ALPHA_TYPE);
+ if (any_premultiplied) {
+ node->output_alpha_type = ALPHA_PREMULTIPLIED;
+ } else if (any_postmultiplied) {
+ node->output_alpha_type = ALPHA_POSTMULTIPLIED;
+ } else {
+ node->output_alpha_type = ALPHA_BLANK;
+ }
+ }
+ }
+}
+