Changeset - f2fafa9734f5
[Not reviewed]
default
0 2 0
Jason Maltzen (jmaltzen) - 9 years ago 2016-03-04 05:37:20
jason.maltzen@unsanctioned.net
Update code to detect paint swatch - increase the range allowed for papyrus, but by more detailed in looking for the top/left border of the swatch.
2 files changed with 76 insertions and 17 deletions:
0 comments (0 inline, 0 general)
ReactionRecorder.cs
Show inline comments
...
 
@@ -8,151 +8,210 @@
 
 copies of the Software, and to permit persons to whom the Software is
 
 furnished to do so, subject to the following conditions:
 

	
 
 The above copyright notice and this permission notice shall be included in
 
 all copies or substantial portions of the Software.
 

	
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
 THE SOFTWARE.
 
*/
 

	
 
using System;
 

	
 
namespace DesertPaintLab
 
{
 
    // ReactionRecorder - business logic for recording paint reactions
 
    public class ReactionRecorder
 
    {
 
        const int COLOR_TOLERANCE = 3;
 

	
 
        const int DEFAULT_SWATCH_HEIGHT = 24;
 
        const int DEFAULT_SWATCH_HEIGHT = 24; // including top and bottom borders
 
        const int DEFAULT_SWATCH_WIDTH = 260;
 
        const int DEFAULT_COLOR_BAR_WIDTH = 306;
 
        const int DEFAULT_RED_BAR_SPACING = 32;
 
        const int DEFAULT_GREEN_BAR_SPACING = 42;
 
        const int DEFAULT_BLUE_BAR_SPACING = 52;
 

	
 
        const int DEFAULT_SWATCH_TEST_WIDTH = 26; // width to test on ends of swatch (10% on either end)
 

	
 
        static int swatchHeight = DEFAULT_SWATCH_HEIGHT;
 
        static int swatchWidth = DEFAULT_SWATCH_WIDTH;
 
        static int swatchTestWidth = DEFAULT_SWATCH_TEST_WIDTH;
 
        static int colorBarWidth = DEFAULT_COLOR_BAR_WIDTH;
 
        static int redBarSpacing = DEFAULT_RED_BAR_SPACING;
 
        static int greenBarSpacing = DEFAULT_GREEN_BAR_SPACING;
 
        static int blueBarSpacing = DEFAULT_BLUE_BAR_SPACING;
 
        static int pixelMultiplier = 1;
 

	
 
        private static bool IsPapyTexture(byte r, byte g, byte b)
 
        {
 
            return ((r > 0xD0) && (g >= 0xC4) && (b >= 0x91)) &&
 
            // red between 208 and 244
 
            // green between 192 and 237
 
            // blue between 145 and 205
 
            return ((r > 0xD0) && (g >= 0xC0) && (b >= 0x91)) &&
 
                    ((r < 0xF4) && (g <= 0xED) && (b <= 0xCD));
 
        }
 

	
 
        public static void SetPixelMultiplier(int pixelMultiplier)
 
        {
 
            swatchHeight    = DEFAULT_SWATCH_HEIGHT * pixelMultiplier;
 
            swatchWidth     = DEFAULT_SWATCH_WIDTH * pixelMultiplier;
 
            colorBarWidth   = DEFAULT_COLOR_BAR_WIDTH * pixelMultiplier;
 
            redBarSpacing   = DEFAULT_RED_BAR_SPACING * pixelMultiplier;
 
            greenBarSpacing = DEFAULT_GREEN_BAR_SPACING * pixelMultiplier;
 
            blueBarSpacing  = DEFAULT_BLUE_BAR_SPACING * pixelMultiplier;
 
            swatchTestWidth = DEFAULT_SWATCH_TEST_WIDTH * pixelMultiplier;
 
            ReactionRecorder.pixelMultiplier = pixelMultiplier;
 
        }
 

	
 
        unsafe private static void ColorAt(byte *pixBytes, int x, int y, int stride, out byte r, out byte g, out byte b)
 
        {
 
            int pixelStart = (y * stride) + (x * 3);
 
                    r = pixBytes[pixelStart];
 
                    g = pixBytes[pixelStart + 1];
 
                    b = pixBytes[pixelStart + 2];
 
        }
 

	
 
        unsafe private static bool IsPossibleBorderPixel(byte* pixBytes, int x, int y, int stride)
 
        private static bool IsDarkPixel(int r, int g, int b)
 
        {
 
            return ((r < 0x46) && (g < 0x46) && (b < 0x47));
 
        }
 

	
 
        private static bool IsColorMatch(byte test_r, byte test_g, byte test_b, byte target_r, byte target_g, byte target_b)
 
        {
 
            return ((Math.Abs(test_r - target_r) <= COLOR_TOLERANCE) &&
 
                (Math.Abs(test_g - target_g) <= COLOR_TOLERANCE) &&
 
                (Math.Abs(test_b - target_b) <= COLOR_TOLERANCE));
 
        }
 

	
 
        unsafe private static bool IsPossibleSwatchSlice(byte* pixBytes, int x, int y, int stride)
 
        {
 
            int testPixelStart = (y * stride) + (x * 3);
 
            byte r = pixBytes[testPixelStart];
 
            byte g = pixBytes[testPixelStart+1];
 
            byte b = pixBytes[testPixelStart+2];
 

	
 
            bool isDarkPixel = false;
 
            bool isPapyAbove = false;
 
            bool isPapyBelow = false;
 
            // 1.) Check if this is a dark pixel.
 
            isDarkPixel = ((r < 0x46) && (g < 0x46) && (b < 0x46));
 
            
 
            // 2.) Check the pixel above it,
 
            // to see if it's from the papy texture.
 
            // 1.) Check if the top pixel is a dark pixel.
 
            isDarkPixel = IsDarkPixel(r, g, b); // ((r < 0x46) && (g < 0x46) && (b < 0x46));
 
            
 
            // 2.) Check the pixel above it to see if it's from the papy texture.
 
            int otherPixelStart = testPixelStart - stride;
 
            isPapyAbove = ((otherPixelStart >= 0) &&
 
                IsPapyTexture(pixBytes[otherPixelStart++], pixBytes[otherPixelStart++], pixBytes[otherPixelStart]));
 

	
 
            // 3.) Check the pixel below where the swatch should be,
 
            // to see if it's also from the papy texture.
 
            // 3.) Check the pixel below where the swatch should be to see if it's also from the papy texture.
 
            otherPixelStart = testPixelStart + (stride * swatchHeight);
 
            isPapyBelow = (IsPapyTexture(pixBytes[otherPixelStart++], pixBytes[otherPixelStart++], pixBytes[otherPixelStart]));
 

	
 
            bool result = isDarkPixel && isPapyAbove && isPapyBelow;
 

	
 
            // grab the swatch color
 
            int swatchColorStart = testPixelStart + (2 * pixelMultiplier * stride); // 2 rows below the test pixel, skipping the faded color
 
            r = pixBytes[swatchColorStart];
 
            g = pixBytes[swatchColorStart+1];
 
            b = pixBytes[swatchColorStart+2];
 

	
 
            // scan the column from 2 below the top to 2 above the bottom to ensure the color matches
 
            for (int i = (2*pixelMultiplier); result && (i < swatchHeight-(2*pixelMultiplier)); ++i)
 
            {
 
                otherPixelStart = testPixelStart + (stride * i);
 
                result &= IsColorMatch(pixBytes[otherPixelStart], pixBytes[otherPixelStart+1], pixBytes[otherPixelStart+2], r, g, b);
 
            }
 

	
 
            return result;
 
        }
 

	
 
        unsafe private static bool IsPossibleSwatchUpperLeft(byte* pixBytes, int x, int y, int stride)
 
        {
 
            int testPixelStart = (y * stride) + (x * 3);
 

	
 
            bool result = true;
 
            // test the left edge for dark pixels
 
            for (int i = 0; result && (i < swatchHeight-pixelMultiplier); ++i)
 
            {
 
                int otherPixelStart = testPixelStart + (stride * i);
 
                result &= IsDarkPixel(pixBytes[otherPixelStart], pixBytes[otherPixelStart+1], pixBytes[otherPixelStart+2]);
 
            }
 

	
 
            // test the dark top border and for papyrus below the swatch
 
            for (int i = 0; result && (i < swatchWidth); ++i)
 
            {
 
                int otherPixelStart = testPixelStart + (3 * i);
 
                result &= IsDarkPixel(pixBytes[otherPixelStart], pixBytes[otherPixelStart+1], pixBytes[otherPixelStart+2]);
 
                otherPixelStart = otherPixelStart - stride;
 
                result &= IsPapyTexture(pixBytes[otherPixelStart], pixBytes[otherPixelStart+1], pixBytes[otherPixelStart+2]);
 
                otherPixelStart = testPixelStart + (stride * swatchHeight) + (3 * i);
 
                result &= IsPapyTexture(pixBytes[otherPixelStart], pixBytes[otherPixelStart+1], pixBytes[otherPixelStart+2]);
 
            }
 

	
 
            return result;
 
        }
 

	
 
        unsafe public static bool CaptureReaction(byte* pixBytes, int screenshotWidth, int screenshotHeight, int stride, ref PaintColor reactedColor, ref int redPixelStart)
 
        {
 
            byte pixel_r, pixel_g, pixel_b;
 
            int pixelStart, otherPixelStart;
 
            bool colorMatch = true;
 
            for (int x = 0; x < screenshotWidth - colorBarWidth; ++x)
 
            {
 
                for (int y = 0; y < (screenshotHeight - 53); ++y)
 
                for (int y = 0; y < (screenshotHeight - (blueBarSpacing + 1)  /*53*/); ++y)
 
                {
 
                    // Look for the color swatch.
 
                    pixelStart = (y * stride) + (x * 3);
 
                    pixel_r = pixBytes[pixelStart];
 
                    pixel_g = pixBytes[pixelStart + 1];
 
                    pixel_b = pixBytes[pixelStart + 2];
 
                    
 
                    bool foundSwatch = IsPossibleBorderPixel(pixBytes, x, y, stride); // ((pixel_r < 0x46) && (pixel_g < 0x46) && (pixel_b < 0x46));
 
                    bool foundSwatch = IsPossibleSwatchUpperLeft(pixBytes, x, y, stride); // ((pixel_r < 0x46) && (pixel_g < 0x46) && (pixel_b < 0x46));
 
                    if (foundSwatch)
 
                    {
 
                        int borderXOffset = 0;
 
                        for (borderXOffset = 0; foundSwatch && (borderXOffset < swatchWidth); ++borderXOffset)
 
                        for (borderXOffset = (2 * pixelMultiplier); foundSwatch && (borderXOffset < swatchTestWidth); ++borderXOffset)
 
                        {
 
                            foundSwatch &= IsPossibleBorderPixel(pixBytes, x + borderXOffset, y, stride);
 
                            foundSwatch &= IsPossibleSwatchSlice(pixBytes, x + borderXOffset, y, stride);
 
                            foundSwatch &= IsPossibleSwatchSlice(pixBytes, x + swatchWidth - borderXOffset, y, stride);
 
                        }
 
                    }
 

	
 
                    if (foundSwatch)
 
                    {
 
                        // found a swatch with an appropriate dark top border with papyrus texture above it and papyrus a jump below it
 
                        // 4.) Scan the left border of the potential swatch
 
                        // location.
 
                        colorMatch = true;
 
                        for (int i = 1; i < swatchHeight - 2; ++i)
 
                        {
 
                            otherPixelStart = pixelStart + (stride * i);
 
                            if ((Math.Abs(pixel_r - pixBytes[otherPixelStart++]) > COLOR_TOLERANCE) ||
 
                                (Math.Abs(pixel_g - pixBytes[otherPixelStart++]) > COLOR_TOLERANCE) ||
 
                                (Math.Abs(pixel_b - pixBytes[otherPixelStart]) > COLOR_TOLERANCE))
 
                            if (!IsColorMatch(pixel_r, pixel_g, pixel_b, pixBytes[otherPixelStart], pixBytes[otherPixelStart+1], pixBytes[otherPixelStart+2]))
 
                            {
 
                                colorMatch = false;
 
                                break;
 
                            }
 
                        }
 
                        
 
                        if (colorMatch)
 
                        {
 
                            // WE FOUND THE SWATCH!
 
                            // Now we know where the color bars are.
 
                            redPixelStart = pixelStart + (redBarSpacing * stride);
 
                            int redPixelCount = 0;
 
                            while ((pixBytes[redPixelStart] > 0x9F) &&
 
                                   (pixBytes[redPixelStart + 1] < 0x62) &&
 
                                   (pixBytes[redPixelStart + 2]  < 0x62))
 
                            {
 
                                redPixelCount++;
 
                                // pixBytes[redPixelStart] = 0x00;
 
                                // pixBytes[redPixelStart + 1] = 0xFF;
 
                                // pixBytes[redPixelStart + 2] = 0xFF;
 
                                redPixelStart += 3;
 
                            }
 
                                
 
                            reactedColor.Red = (byte)Math.Round((float)redPixelCount * 255f / (float)colorBarWidth);
gtk-gui/gui.stetic
Show inline comments
 
<?xml version="1.0" encoding="utf-8"?>
 
<stetic-interface>
 
  <configuration>
 
    <images-root-path>..</images-root-path>
 
    <target-gtk-version>2.12</target-gtk-version>
 
  </configuration>
 
  <import>
 
    <widget-library name="glade-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
 
    <widget-library name="../bin/Debug/DesertPaintLab.exe" internal="true" />
 
    <widget-library name="../bin/Release/DesertPaintLab.exe" internal="true" />
 
  </import>
 
  <widget class="Gtk.Window" id="MainWindow" design-size="732 384">
 
    <action-group name="Default">
 
      <action id="FileAction">
 
        <property name="Type">Action</property>
 
        <property name="Accelerator">&lt;Alt&gt;f</property>
 
        <property name="Label" translatable="yes">_File</property>
 
        <property name="ShortLabel" translatable="yes">_File</property>
 
      </action>
 
      <action id="HelpAction">
 
        <property name="Type">Action</property>
 
        <property name="Accelerator">&lt;Alt&gt;a</property>
 
        <property name="Label" translatable="yes">_Help</property>
 
        <property name="ShortLabel" translatable="yes">_Help</property>
 
      </action>
 
      <action id="AboutAction">
 
        <property name="Type">Action</property>
 
        <property name="Accelerator">&lt;Alt&gt;a</property>
 
        <property name="Label" translatable="yes">_About...</property>
 
        <property name="ShortLabel" translatable="yes">_About...</property>
 
        <signal name="Activated" handler="OnAbout" />
 
      </action>
 
      <action id="ExitAction">
 
        <property name="Type">Action</property>
0 comments (0 inline, 0 general)