Example #1
0
        public void MBCreateImage()
        {
            int imageWidth  = 100;
            int imageHeight = 100;

            Bitmap bmp = new Bitmap(imageWidth, imageHeight);

            double cx          = 0;
            double cy          = 0;
            double x_step      = (_request.max_x - _request.min_x) / imageWidth;
            double y_step      = (_request.max_y - _request.min_y) / imageHeight;
            double escapeCount = 0;
            double logMax      = Math.Log(1000 * _request.depth);

            Console.WriteLine($"Thread: {Task.CurrentId.ToString()} started building image for connection {_request.connectionId}, {_request.display_x}, {_request.display_y}");


            //run through each pixel in the bitmap, and calculate whether each pixel is in the mandelbrot set or not
            //based on the co-ordinates of the request.
            for (int x = 0; x <= imageWidth - 1; x++)
            {
                cx = _request.min_x + x * x_step;

                for (int y = 0; y <= imageHeight - 1; y++)
                {
                    cy = _request.min_y + y * y_step;

                    // calcuate the number of iterations to escape at this point in the set.

                    escapeCount = EscapeCount(cx, cy, _request.depth);

                    if (escapeCount < (1000 * _request.depth))
                    {
                        // use a log scale to get some nice grading in the colours
                        escapeCount = (Math.Log(escapeCount) / logMax);

                        // get a nice colour from looking up the hue of the escape count.
                        // note the test for zero puts a border around the image
                        ColorRGB colorRGB;
                        ColorRGB colourRGB;

                        if (x == 0 | y == 0)
                        {
                            colorRGB = HSL2RGB(escapeCount, 0.5, 0.55);
                        }
                        else
                        {
                            colorRGB = HSL2RGB(escapeCount, 0.5, 0.5);
                        }

                        // switch the colours around to make it a bit more interesting.
                        switch (_request.depth % 4)
                        {
                        case 0:
                        { colourRGB.R = colorRGB.G; colourRGB.G = colorRGB.R; colourRGB.B = colorRGB.B; break; }

                        case 1:
                        { colourRGB.R = colorRGB.B; colourRGB.G = colorRGB.G; colourRGB.B = colorRGB.R; break; }

                        case 2:
                        { colourRGB.R = colorRGB.R; colourRGB.G = colorRGB.B; colourRGB.B = colorRGB.G; break; }

                        default:
                        { colourRGB.R = colorRGB.R; colourRGB.G = colorRGB.G; colourRGB.B = colorRGB.B; break; }
                        }

                        // set the pixel colour
                        bmp.SetPixel(x, y, Color.FromArgb(colourRGB.R, colourRGB.B, colourRGB.G));
                    }
                    else
                    {
                        if (x == 0 | y == 0)
                        {
                            // set it to grey.
                            bmp.SetPixel(x, y, Color.FromArgb(25, 25, 25));
                        }
                        else
                        {
                            // set it to black.
                            bmp.SetPixel(x, y, Color.FromArgb(0, 0, 0));
                        }
                    }
                }
            }

            // assuming everything kafka and mongo is threadsafe....

            if (toFile)
            {
                // create a new file and write the image to the file in jpeg format.
                // get rid of this when the upload to Mongo works!
                string     filename   = $"./Images/Image{_request.display_x.ToString()}-{_request.display_y.ToString()}.jpg";
                FileStream fileStream = File.Create(filename);
                bmp.Save(fileStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                fileStream.Close();
            }
            else
            {
                // upload the bmp to mongoDB as a Jpeg
                MemoryStream tmpStream = new MemoryStream();
                bmp.Save(tmpStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                tmpStream.Position = 0;
                var id = _bucket.UploadFromStream("filename", tmpStream);
                bmp.Dispose();


                var _response = new imageResponse();
                _response.display_x    = _request.display_x;
                _response.display_y    = _request.display_y;
                _response.url          = id.ToString();
                _response.connectionId = _request.connectionId;

                // send the image Id back to the client
                _producer.ProduceAsync("imageResponse", new Message <Null, imageResponse> {
                    Value = _response
                });
                _producer.Flush();
            }

            Console.WriteLine($"Thread: {Task.CurrentId.ToString()} finished building image for connection {_request.connectionId}, {_request.display_x}, {_request.display_y}");
        }
Example #2
0
        public imageResponse post(imageRequest req)
        {
            imageResponse result = _repositery.getRandom(req);

            return(result);
        }