Fix #34461: Inconsistent behavior of "Color Mix Node" and "Alpha Over Node"
authorSergey Sharybin <sergey.vfx@gmail.com>
Fri, 1 Mar 2013 15:37:15 +0000 (15:37 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Fri, 1 Mar 2013 15:37:15 +0000 (15:37 +0000)
Added compatibility option "Straight Alpha Output" to image input node

When this option is enabled, image input node will convert float buffer
to straight alpha.

This is not what you'll usually want with new alpha pipeline, nit this
is needed to preserve compatibility with older files saved in 2.65.
In that version byte image are resulting with straight alpha passing
to the compositor and alpha-overing required extra premultiplication
of inputs.

So, that's why Straight Alpha Output is needed -- it's set in versioning
code for byte node images so they'll still output straight alpha.

This option is currently only available in N-panel.

Additional change: added Alpha Mode for image input node to N-panel.

source/blender/blenloader/intern/readfile.c
source/blender/compositor/nodes/COM_ImageNode.cpp
source/blender/editors/space_node/drawnode.c
source/blender/makesdna/DNA_node_types.h
source/blender/makesrna/intern/rna_nodetree.c

index d70cbe4a145fb15bb97b9bd416ef6955e8133e71..9c6e82cf998741ecb26548ed9221e38542a63ea1 100644 (file)
@@ -7355,6 +7355,23 @@ static void do_version_node_fix_translate_wrapping(void *UNUSED(data), ID *UNUSE
        }
 }
 
+static void do_version_node_straight_image_alpha_workaround(void *data, ID *UNUSED(id), bNodeTree *ntree)
+{
+       FileData *fd = (FileData *) data;
+       bNode *node;
+
+       for (node = ntree->nodes.first; node; node = node->next) {
+               if (node->type == CMP_NODE_IMAGE) {
+                       Image *image = blo_do_versions_newlibadr(fd, ntree->id.lib, node->id);
+
+                       if (image) {
+                               if ((image->flag & IMA_DO_PREMUL) == 0 && image->alpha_mode == IMA_ALPHA_STRAIGHT)
+                                       node->custom1 |= CMP_NODE_IMAGE_USE_STRAIGHT_OUTPUT;
+                       }
+               }
+       }
+}
+
 static void do_version_node_fix_internal_links_264(void *UNUSED(data), ID *UNUSED(id), bNodeTree *ntree)
 {
        bNode *node;
@@ -8648,6 +8665,8 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                Scene *scene;
                Image *image, *nimage;
                Tex *tex, *otex;
+               bNodeTreeType *ntreetype;
+               bNodeTree *ntree;
 
                for (scene = main->scene.first; scene; scene = scene->id.next) {
                        Sequence *seq;
@@ -8752,6 +8771,13 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
 
                for (image = main->image.first; image; image = image->id.next)
                        image->flag &= ~IMA_DONE_TAG;
+
+               ntreetype = ntreeGetType(NTREE_COMPOSIT);
+               if (ntreetype && ntreetype->foreach_nodetree)
+                       ntreetype->foreach_nodetree(main, fd, do_version_node_straight_image_alpha_workaround);
+
+               for (ntree = main->nodetree.first; ntree; ntree = ntree->id.next)
+                       do_version_node_straight_image_alpha_workaround(fd, NULL, ntree);
        }
 
        if (main->versionfile < 265 || (main->versionfile == 265 && main->subversionfile < 7)) {
index 864d6f0831102e7ea09b7e6687917beb25836536..e4dd72d79e9cb27a8906f790b8ef2ba1c4424e57 100644 (file)
@@ -25,6 +25,7 @@
 #include "COM_ExecutionSystem.h"
 #include "COM_ImageOperation.h"
 #include "COM_MultilayerImageOperation.h"
+#include "COM_ConvertPremulToStraightOperation.h"
 #include "BKE_node.h"
 #include "BLI_utildefines.h"
 
@@ -72,6 +73,7 @@ void ImageNode::convertToOperations(ExecutionSystem *graph, CompositorContext *c
        ImageUser *imageuser = (ImageUser *)editorNode->storage;
        int framenumber = context->getFramenumber();
        int numberOfOutputs = this->getNumberOfOutputSockets();
+       bool outputStraightAlpha = editorNode->custom1 & CMP_NODE_IMAGE_USE_STRAIGHT_OUTPUT;
        BKE_image_user_frame_calc(imageuser, context->getFramenumber(), 0);
 
        /* force a load, we assume iuser index will be set OK anyway */
@@ -138,7 +140,15 @@ void ImageNode::convertToOperations(ExecutionSystem *graph, CompositorContext *c
                if (numberOfOutputs >  0) {
                        ImageOperation *operation = new ImageOperation();
                        if (outputImage->isConnected()) {
-                               outputImage->relinkConnections(operation->getOutputSocket());
+                               if (outputStraightAlpha) {
+                                       NodeOperation *alphaConvertOperation = new ConvertPremulToStraightOperation();
+                                       addLink(graph, operation->getOutputSocket(0), alphaConvertOperation->getInputSocket(0));
+                                       outputImage->relinkConnections(alphaConvertOperation->getOutputSocket());
+                                       graph->addOperation(alphaConvertOperation);
+                               }
+                               else {
+                                       outputImage->relinkConnections(operation->getOutputSocket());
+                               }
                        }
                        operation->setImage(image);
                        operation->setImageUser(imageuser);
index 72b8d0381df80725d82d0527bc0e5c6f237befcc..8a2e03f266031a8d0631c8ac8101d4447d397eb6 100644 (file)
@@ -1608,12 +1608,15 @@ static void node_composit_buts_image_details(uiLayout *layout, bContext *C, Poin
 
        node_composit_buts_image(layout, C, ptr);
 
+       uiItemR(layout, ptr, "use_straight_alpha_output", 0, NULL, 0);
+
        if (!node->id)
                return;
 
        imaptr = RNA_pointer_get(ptr, "image");
 
        uiTemplateColorspaceSettings(layout, &imaptr, "colorspace_settings");
+       uiItemR(layout, &imaptr, "alpha_mode", 0, NULL, 0);
 }
 
 static void node_composit_buts_renderlayers(uiLayout *layout, bContext *C, PointerRNA *ptr)
index 5aaf46a541f1bed6586c6f22481ad2be31304f6d..62c997b72c678d40b9bd7f41af1edeb01ae6ada3 100644 (file)
@@ -854,4 +854,7 @@ typedef struct NodeShaderNormalMap {
 
 #define CMP_NODE_MASK_MBLUR_SAMPLES_MAX 64
 
+/* image */
+#define CMP_NODE_IMAGE_USE_STRAIGHT_OUTPUT     1
+
 #endif
index 1fff567f25c150ebe86f526602f91d458209d6c5..73fe5f3a48dd52823b203e7e86b8ee7140f38e5b 100644 (file)
@@ -2412,7 +2412,12 @@ static void def_cmp_image(StructRNA *srna)
        RNA_def_property_flag(prop, PROP_EDITABLE);
        RNA_def_property_ui_text(prop, "Image", "");
        RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
-       
+
+       prop = RNA_def_property(srna, "use_straight_alpha_output", PROP_BOOLEAN, PROP_NONE);
+       RNA_def_property_boolean_sdna(prop, NULL, "custom1", CMP_NODE_IMAGE_USE_STRAIGHT_OUTPUT);
+       RNA_def_property_ui_text(prop, "Straight Alpha Output", "Put Node output buffer to straight alpha instead of premultiplied");
+       RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
+
        /* NB: image user properties used in the UI are redefined in def_node_image_user,
         * to trigger correct updates of the node editor. RNA design problem that prevents
         * updates from nested structs ...