<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Threading.Tasks;
using static Android.Graphics.Bitmap;
using System.IO;
using Android.Graphics;
using Android.Util;
using Painter.Interfaces;
using Android.Provider;
using Android.Content.Res;

namespace Painter.Droid
{
    public class PainterExport : IPainterExport 
    {
        private DisplayMetrics metrics;
        public PainterExport()
        {
            IWindowManager windowManager = Application.Context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
            metrics = new DisplayMetrics();
            windowManager.DefaultDisplay.GetMetrics(metrics);
        }

        public async Task<byte[]> GetCurrentImageAsPNG(int width, int height, float scale, List<Abstractions.Stroke> strokes, Abstractions.Scaling scaling = Abstractions.Scaling.Relative_None, int quality = 80, Painter.Abstractions.Color BackgroundColor = null, bool useDevicePixelDensity = false, byte[] BackgroundImage = null)
        {
            return await ExportCurrentImage(width, height, scale, strokes, scaling, Abstractions.ExportFormat.Png, quality, BackgroundColor ?? new Abstractions.Color(1, 1, 1, 1), useDevicePixelDensity, BackgroundImage);
        }

        public async Task<byte[]> GetCurrentImageAsJPG(int width, int height, float scale, List<Abstractions.Stroke> strokes, Abstractions.Scaling scaling = Abstractions.Scaling.Relative_None, int quality = 80, Painter.Abstractions.Color BackgroundColor = null, bool useDevicePixelDensity = false, byte[] BackgroundImage = null)
        {
            return await ExportCurrentImage(width, height, scale, strokes, scaling, Abstractions.ExportFormat.Jpeg, quality, BackgroundColor ?? new Abstractions.Color(1, 1, 1, 1), useDevicePixelDensity, BackgroundImage);
        }

        public async Task<byte[]> ExportCurrentImage(int width, int height, float scale, List<Abstractions.Stroke> strokes, Abstractions.Scaling scaling, Abstractions.ExportFormat format, int quality, Painter.Abstractions.Color BackgroundColor, bool useDevicePixelDensity, byte[] BackgroundImage = null)
        {
            if (scale == 0)
                scale = 1.0f;

            //Initialize data holders
            byte[] data;
            Stream str = new MemoryStream();

            if (useDevicePixelDensity)
            {
                width *= (int)metrics.Density;
                height *= (int)metrics.Density;
            }

            Bitmap tempImage = null;
            Bitmap backgroundBitmap = null;
            Canvas tempCanvas = null;
            if (BackgroundImage == null)
            {
                tempImage = Bitmap.CreateBitmap(metrics, width, height, Bitmap.Config.Argb8888);
                tempCanvas = new Canvas(tempImage);
            }
            else
            {
                backgroundBitmap = await BitmapFactory.DecodeByteArrayAsync(BackgroundImage, 0, BackgroundImage.Length);
                var backgroundScale = GetDrawingScale(scaling, backgroundBitmap, width, height);
                tempImage = Bitmap.CreateBitmap(metrics, (int)Math.Min(width, backgroundBitmap.Width * backgroundScale), (int)Math.Min(height, backgroundBitmap.Height * backgroundScale), Bitmap.Config.Argb8888);
                tempCanvas = new Canvas(tempImage);
            }

            DrawStrokes(tempCanvas, strokes, backgroundBitmap, BackgroundColor, scaling, width, height, scale);

            //Compress the image and save it to the stream
            switch (format)
            {
                case Abstractions.ExportFormat.Png:
                    await tempImage.CompressAsync(CompressFormat.Png, quality, str);
                    break;
                case Abstractions.ExportFormat.Jpeg:
                    await tempImage.CompressAsync(CompressFormat.Jpeg, quality, str);
                    break;
            }
            <commit_msg>Calculate pixel density fixed<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Threading.Tasks;
using static Android.Graphics.Bitmap;
using System.IO;
using Android.Graphics;
using Android.Util;
using Painter.Interfaces;
using Android.Provider;
using Android.Content.Res;

namespace Painter.Droid
{
    public class PainterExport : IPainterExport 
    {
        private DisplayMetrics metrics;
        public PainterExport()
        {
            IWindowManager windowManager = Application.Context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
            metrics = new DisplayMetrics();
            windowManager.DefaultDisplay.GetMetrics(metrics);
        }

        public async Task<byte[]> GetCurrentImageAsPNG(int width, int height, float scale, List<Abstractions.Stroke> strokes, Abstractions.Scaling scaling = Abstractions.Scaling.Relative_None, int quality = 80, Painter.Abstractions.Color BackgroundColor = null, bool useDevicePixelDensity = false, byte[] BackgroundImage = null)
        {
            return await ExportCurrentImage(width, height, scale, strokes, scaling, Abstractions.ExportFormat.Png, quality, BackgroundColor ?? new Abstractions.Color(1, 1, 1, 1), useDevicePixelDensity, BackgroundImage);
        }

        public async Task<byte[]> GetCurrentImageAsJPG(int width, int height, float scale, List<Abstractions.Stroke> strokes, Abstractions.Scaling scaling = Abstractions.Scaling.Relative_None, int quality = 80, Painter.Abstractions.Color BackgroundColor = null, bool useDevicePixelDensity = false, byte[] BackgroundImage = null)
        {
            return await ExportCurrentImage(width, height, scale, strokes, scaling, Abstractions.ExportFormat.Jpeg, quality, BackgroundColor ?? new Abstractions.Color(1, 1, 1, 1), useDevicePixelDensity, BackgroundImage);
        }

        public async Task<byte[]> ExportCurrentImage(int width, int height, float scale, List<Abstractions.Stroke> strokes, Abstractions.Scaling scaling, Abstractions.ExportFormat format, int quality, Painter.Abstractions.Color BackgroundColor, bool useDevicePixelDensity, byte[] BackgroundImage = null)
        {
            if (scale == 0)
                scale = 1.0f;

            //Initialize data holders
            byte[] data;
            Stream str = new MemoryStream();

            if (useDevicePixelDensity)
            {
                // Calculate width and height with PixelDensity
                float widthF = (float)width * metrics.Density;
                float heightF = (float)height * metrics.Density;

                width = (int)widthF;
                height = (int)heightF;
            }

            Bitmap tempImage = null;
            Bitmap backgroundBitmap = null;
            Canvas tempCanvas = null;
            if (BackgroundImage == null)
            {
                tempImage = Bitmap.CreateBitmap(metrics, width, height, Bitmap.Config.Argb8888);
                tempCanvas = new Canvas(tempImage);
            }
            else
            {
                backgroundBitmap = await BitmapFactory.DecodeByteArrayAsync(BackgroundImage, 0, BackgroundImage.Length);
                var backgroundScale = GetDrawingScale(scaling, backgroundBitmap, width, height);
                tempImage = Bitmap.CreateBitmap(metrics, (int)Math.Min(width, backgroundBitmap.Width * backgroundScale), (int)Math.Min(height, backgroundBitmap.Height * backgroundScale), Bitmap.Config.Argb8888);
                tempCanvas = new Canvas(tempImage);
            }

            DrawStrokes(tempCanvas, strokes, backgroundBitmap, BackgroundColor, scaling, width, height, scale);

            //Compress the image and save it to the stream
            switch (format)
            {
                case Abstractions.ExportFormat.Png:
                    await tempImage.CompressAsync(CompressFormat.Png, quality, str);
                    break;
                case Abstractions.ExportFormat.Jpeg:
                    await tempImage.CompressAsync(CompressFormat.Jpeg, quality, str);
                    break;
            }
            
{'fraction_non_alphanumeric': 0.08457115476760901, 'fraction_numerical': 0.005749880210828941, 'mean_word_length': 2.7205882352941178, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3375126', 'n_tokens_mistral': 2180, 'n_tokens_neox': 2064, 'n_words': 541}