Пример #1
0
        static void Render(Bitmap bmp, PixelQuery query)
        {
            //lock bitmapData & copy it to buffer
            Rectangle  area = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData data = bmp.LockBits(area, ImageLockMode.ReadWrite, bmp.PixelFormat);
            int        bpp  = Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;
            int        size = data.Stride * data.Height;

            byte[] buffer = new byte[size];
            IntPtr ptr    = data.Scan0;

            Marshal.Copy(ptr, buffer, 0, size);
            //query a color for each pixel and write it into the buffer
            for (int x = 0; x < bmp.Width; x++)
            {
                for (int y = 0; y < bmp.Height; y++)
                {
                    Color c = query(x, y);
                    int   i = y * data.Stride + x * bpp;
                    buffer[i]     = c.B;
                    buffer[i + 1] = c.G;
                    buffer[i + 2] = c.R;
                    buffer[i + 3] = c.A;
                }
            }
            //copy buffer back to bitmapData & unlock
            Marshal.Copy(buffer, 0, ptr, size);
            bmp.UnlockBits(data);
        }
Пример #2
0
        /// <summary>
        /// method returns the Click Handler
        /// </summary>
        public void ButtonClickHandler(int buttonNumber, string buttonName)
        {
            // Hide this
            PixelInfo.Visible = false;

            // Determine the action by the buttonNumber
            switch (buttonNumber)
            {
            case 1:

                // Close the current file
                CloseFile();

                // required
                break;

            case 2:

                // Save the current file
                Save();

                // required
                break;

            case 3:

                // Save the current file
                SaveAs();

                // required
                break;

            case 4:

                // Reset Image
                Reset();

                // required
                break;

            case 5:

                // Undo the last change
                UndoChanges();

                // required
                break;

            case 6:

                // Set ColorPickerMode true or false
                this.ColorPickerMode = !this.ColorPickerMode;

                // We can't be in both at the same time
                this.RectangleMode = !ColorPickerMode;

                // if not in ColorPickerMode
                if (this.ColorPickerMode)
                {
                    // it is on
                    MessagesLabel.Text = "Color Picker Mode On";

                    // Hide the control
                    this.PixelInfo.Visible = false;
                }
                else
                {
                    // it is on
                    MessagesLabel.Text = "Color Picker Mode Off";

                    // Hide the control
                    this.PixelInfo.Visible = false;
                }

                // required
                break;

            case 7:

                // Apply Query

                // Parse and apply the current query
                PixelQuery pixelQuery = PixelDatabase.ApplyQuery(QueryTextBox.Text, this.StatusUpdate);

                // Set the Bitmap now that is has been updated
                Canvas.BackgroundImage = PixelDatabase.DirectBitmap.Bitmap;

                // required
                break;

            case 8:

                // Parse and apply the current query
                this.Abort = true;

                // required
                break;
            }

            // Refresh everything
            Refresh();
            Application.DoEvents();
        }
        /// <summary>
        /// This method Set Update Parameters
        /// </summary>
        public static void SetUpdateParameters(string updateText, ref PixelQuery pixelQuery)
        {
            // if the updateText exists and the pixelQuery object exists
            if ((TextHelper.Exists(updateText)) && (NullHelper.Exists(pixelQuery)))
            {
                // if the updateText starts with the word set (else this is not a proper BQL Update query).
                if (updateText.StartsWith("set"))
                {
                    // only use a space as a delimiter character
                    char[] delimiterChars = { ' ' };

                    // Get a list of words from this text
                    List <Word> words = WordParser.GetWords(updateText);

                    // If the words collection exists and has one or more items
                    if (ListHelper.HasOneOrMoreItems(words))
                    {
                        // if there are six words.
                        // Example: Set Color 100 150 200 40 - (Red = 100, Green = 150, Blue = 200, Alpha = 40)
                        if (words.Count == 6)
                        {
                            // Set to red
                            pixelQuery.Red   = NumericHelper.ParseInteger(words[2].Text, -1, -2);
                            pixelQuery.Green = NumericHelper.ParseInteger(words[3].Text, -1, -2);
                            pixelQuery.Blue  = NumericHelper.ParseInteger(words[4].Text, -1, -2);
                            pixelQuery.Alpha = NumericHelper.ParseInteger(words[5].Text, -1, -2);

                            // verify everything is valid
                            if ((pixelQuery.Red >= 0) && (pixelQuery.Green >= 0) && (pixelQuery.Red >= 0) && (pixelQuery.Alpha >= 0))
                            {
                                // Set the Color
                                pixelQuery.Color = Color.FromArgb(pixelQuery.Alpha, pixelQuery.Red, pixelQuery.Green, pixelQuery.Blue);
                            }
                        }
                        // if there are 5 words
                        // Example: Set Color 100 150 200 - (Red = 100, Green = 150, Blue = 200, Alpha = 255 default value)
                        else if (words.Count == 5)
                        {
                            // Set to red
                            pixelQuery.Red   = NumericHelper.ParseInteger(words[2].Text, -1, -2);
                            pixelQuery.Green = NumericHelper.ParseInteger(words[3].Text, -1, -2);
                            pixelQuery.Blue  = NumericHelper.ParseInteger(words[4].Text, -1, -2);
                            pixelQuery.Alpha = 255;

                            // verify everything is valid
                            if ((pixelQuery.Red >= 0) && (pixelQuery.Green >= 0) && (pixelQuery.Red >= 0) && (pixelQuery.Alpha >= 0))
                            {
                                // Set the Color
                                pixelQuery.Color = Color.FromArgb(pixelQuery.Alpha, pixelQuery.Red, pixelQuery.Green, pixelQuery.Blue);
                            }

                            // Set the Color
                            pixelQuery.Color = Color.FromArgb(pixelQuery.Alpha, pixelQuery.Red, pixelQuery.Green, pixelQuery.Blue);
                        }
                        // if there are 3 words
                        // Example: Set Color Orchid (the color must be a named color)
                        else if (words.Count == 3)
                        {
                            // Set the Color
                            pixelQuery.Color = Color.FromName(words[2].Text);
                            pixelQuery.Alpha = 255;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// This class is used to parse querries from the queryText given
        /// </summary>
        public static PixelQuery ParsePixelQuery(string queryText)
        {
            // initial value
            PixelQuery pixelQuery = new PixelQuery();

            // locals
            int           count        = 0;
            StringBuilder sb           = null;
            string        criteriaText = String.Empty;
            string        updateText   = String.Empty;

            // If the queryText string exists
            if (TextHelper.Exists(queryText))
            {
                // get the lowercase version of the text
                queryText = queryText.ToLower().Trim();

                // parse the ActionType (Show Pixels, Hide Pixels, Draw Line, Update)
                pixelQuery.ActionType = ParseActionType(queryText);

                // if we are doing an update query, we have to modify this a little
                if (pixelQuery.ActionType == ActionTypeEnum.Update)
                {
                    // for an update query, the second line sets the color values to update to
                    // and the third line starts the where clause.

                    // Get the text lines
                    List <TextLine> lines = WordParser.GetTextLines(queryText);

                    // If the lines collection exists and has one or more items
                    if (ListHelper.HasOneOrMoreItems(lines))
                    {
                        // Create a string builder
                        sb = new StringBuilder();

                        // Iterate the collection of TextLine objects
                        foreach (TextLine line in lines)
                        {
                            // Increment the value for count
                            count++;

                            // skip the first line, as it is not really needed
                            if (count > 2)
                            {
                                // Add this lines test
                                sb.Append(line.Text + Environment.NewLine);
                            }

                            // if the value of count equals 2
                            if (count == 2)
                            {
                                // Set the UpdateText
                                updateText = line.Text;
                            }
                        }

                        // Set the criteria text
                        criteriaText = sb.ToString();

                        // Parse the Criteria (this is the matching part)
                        pixelQuery.Criteria = ParseCriteria(criteriaText, pixelQuery.ActionType);

                        // If the updateText string exists
                        if (TextHelper.Exists(updateText))
                        {
                            // Set the update values
                            SetUpdateParameters(updateText, ref pixelQuery);
                        }
                    }
                }
                else if (pixelQuery.ActionType == ActionTypeEnum.HideFrom)
                {
                    // hide from (Left, Right, Up, Down, All)

                    // set the directionsText
                    string directionsText = queryText.Substring(10).Trim();

                    // Parse the directions
                    pixelQuery.Directions = ParseDirections(directionsText);
                }
                else
                {
                    // This is a Hide or Show Query or DrawLine
                    // and the second line starts the where clause.

                    // Parse the Criteria (this is the matching part)
                    pixelQuery.Criteria = ParseCriteria(queryText, pixelQuery.ActionType);
                }
            }

            // return value
            return(pixelQuery);
        }