Fix for half pixel offset rasterizing masks
authorCampbell Barton <ideasman42@gmail.com>
Wed, 16 Apr 2014 13:25:10 +0000 (23:25 +1000)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 16 Apr 2014 13:25:10 +0000 (23:25 +1000)
source/blender/blenkernel/intern/mask_rasterize.c
source/blender/compositor/operations/COM_MaskOperation.cpp
source/blender/compositor/operations/COM_MaskOperation.h
source/blender/editors/mask/mask_draw.c

index 92628387dc6f46467327dd892d908abbb0a9564c..ea039dabce438c2ee8dfcaf00f19ca1f631032cb 100644 (file)
@@ -1434,6 +1434,10 @@ void BKE_maskrasterize_buffer(MaskRasterHandle *mr_handle,
                               const unsigned int width, const unsigned int height,
                               float *buffer)
 {
+       const float x_inv = 1.0f / (float)width;
+       const float y_inv = 1.0f / (float)height;
+       const float x_px_ofs = x_inv * 0.5f;
+       const float y_px_ofs = y_inv * 0.5f;
 #ifdef _MSC_VER
        int y;  /* msvc requires signed for some reason */
 
@@ -1449,9 +1453,9 @@ void BKE_maskrasterize_buffer(MaskRasterHandle *mr_handle,
                unsigned int i = y * width;
                unsigned int x;
                float xy[2];
-               xy[1] = (float)y / (float)height;
+               xy[1] = ((float)y * y_inv) + y_px_ofs;
                for (x = 0; x < width; x++, i++) {
-                       xy[0] = (float)x / (float)width;
+                       xy[0] = ((float)x * x_inv) + x_px_ofs;
 
                        buffer[i] = BKE_maskrasterize_handle_sample(mr_handle, xy);
                }
index 112b5912ad36c1aa18c8b1a52822ae9532a07239..8c8ba93327d30fe40fa5dad8a045c56bc3e64db7 100644 (file)
@@ -129,8 +129,9 @@ void MaskOperation::determineResolution(unsigned int resolution[2], unsigned int
 
 void MaskOperation::executePixelSampled(float output[4], float x, float y, PixelSampler sampler)
 {
-       const float xy[2] = {x * this->m_maskWidthInv,
-                            y * this->m_maskHeightInv};
+       const float xy[2] = {
+           (x * this->m_maskWidthInv)  + this->m_mask_px_ofs[0],
+           (y * this->m_maskHeightInv) + this->m_mask_px_ofs[1]};
 
        if (this->m_rasterMaskHandleTot == 1) {
                if (this->m_rasterMaskHandles[0]) {
index 18d7e594104db306c369e744c86f19e3f3078520..522b873e167d2134faa2145952a3a955633867c4 100644 (file)
@@ -43,6 +43,7 @@ protected:
        int   m_maskHeight;
        float m_maskWidthInv;  /* 1 / m_maskWidth  */
        float m_maskHeightInv; /* 1 / m_maskHeight */
+       float m_mask_px_ofs[2];
 
        float m_frame_shutter;
        int   m_frame_number;
@@ -70,11 +71,13 @@ public:
        {
                this->m_maskWidth    = width;
                this->m_maskWidthInv = 1.0f / (float)width;
+               this->m_mask_px_ofs[0] = this->m_maskWidthInv * 0.5f;
        }
        void setMaskHeight(int height)
        {
                this->m_maskHeight = height;
                this->m_maskHeightInv = 1.0f / (float)height;
+               this->m_mask_px_ofs[1] = this->m_maskHeightInv * 0.5f;
        }
        void setFramenumber(int frame_number) { this->m_frame_number = frame_number; }
        void setSmooth(bool smooth) { this->m_do_smooth = smooth; }
index e84bdf120674db3acbd4c41c5b56579d84de1692..08896b396823d592f6106f57284f20ce2666afc0 100644 (file)
@@ -643,15 +643,21 @@ static void mask_rasterize_func(TaskPool *pool, void *taskdata, int UNUSED(threa
        ThreadedMaskRasterizeState *state = (ThreadedMaskRasterizeState *) BLI_task_pool_userdata(pool);
        ThreadedMaskRasterizeData *data = (ThreadedMaskRasterizeData *) taskdata;
        int scanline;
+       const float x_inv = 1.0f / (float)state->width;
+       const float y_inv = 1.0f / (float)state->height;
+       const float x_px_ofs = x_inv * 0.5f;
+       const float y_px_ofs = y_inv * 0.5f;
 
        for (scanline = 0; scanline < data->num_scanlines; scanline++) {
+               float xy[2];
                int x, y = data->start_scanline + scanline;
+
+               xy[1] = ((float)y * y_inv) + y_px_ofs;
+
                for (x = 0; x < state->width; x++) {
                        int index = y * state->width + x;
-                       float xy[2];
 
-                       xy[0] = (float) x / state->width;
-                       xy[1] = (float) y / state->height;
+                       xy[0] = ((float)x * x_inv) + x_px_ofs;
 
                        state->buffer[index] = BKE_maskrasterize_handle_sample(state->handle, xy);
                }