Author Topic: C# Paint.net Plugin - 8x8 Picture to Grayscale Hexadecimal  (Read 5822 times)

0 Members and 1 Guest are viewing this topic.

Offline Omar Chehab

  • LV2 Member (Next: 40)
  • **
  • Posts: 30
  • Rating: +0/-0
    • View Profile
C# Paint.net Plugin - 8x8 Picture to Grayscale Hexadecimal
« on: June 07, 2015, 08:45:39 am »
Hello, I have recently needed a converter to hexadecimal from colored images. This plugin is used for gray scale sprite development on the ti-84 graphing calculator. I surfed the internet and couldn't find what I needed, so I decided to write a plugin on paint.net that will process my selection and display the output. Here's my progress, it currently is functional however it is conditional. It only properly works when my selection is 8x8, i am aware that i can put it in try and catch i will for sure implement that later, I want to focus on the things that matter now.
Code: [Select]
bool done = false;
void Render(Surface dst, Surface src, Rectangle rect)
{
    Rectangle selection = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();
    ColorBgra CurrentPixel;
    string[,] binary1 = new string[8,8];
    string[,] binary2 = new string[8,8];
    int xx;
    int yy = 0;
    for (int y = selection.Top; y < selection.Bottom; y++)
    {
        xx=0;
        if (IsCancelRequested) return;
        for (int x = selection.Left; x < selection.Right;x++)
        {
            CurrentPixel = src[x,y];
            int rgb = (CurrentPixel.R+CurrentPixel.G+CurrentPixel.B)/3;
            if(rgb >= 0 && rgb < 64)
            {
                binary1[xx,yy] = "1";
                binary2[xx,yy] = "1";
            }
            else if(rgb >= 64 && rgb < 128)
            {
                binary1[xx,yy] = "1";
                binary2[xx,yy] = "0";
            }
            else if(rgb >= 128 && rgb < 192)
            {
                binary1[xx,yy] = "0";
                binary2[xx,yy] = "1";
            }
            else if(rgb >= 192 && rgb < 256)
            {
                binary1[xx,yy] = "0";
                binary2[xx,yy] = "0";
            }
            dst[x,y] = CurrentPixel;
            xx++;
        }
        yy++;
    }
    if(done == false)
    {
        string line;
        string hex = "[";
        for(int y = 0; y < 8; y++)
        {
            line = "";
            for(int x = 0; x < 8; x++)
                line += binary1[x,y];
            hex += ConvertHex(line);
        }
        hex += "][";
        for(int y = 0; y < 8; y++)
        {
            line = "";
            for(int x = 0; x < 8; x++)
                line += binary2[x,y];
            hex += ConvertHex(line);
        }
        hex += "]";
        System.IO.File.WriteAllText(@"C:\Users\user\Desktop\Dota Duelz\hex.txt", hex);
        done = true;
    }
}

string ConvertHex(string strBinary)
{
    string hex = Convert.ToInt32(strBinary,2).ToString("X");
    if(hex.Length == 1)
        hex = "0" + hex;
    return hex;
}

Keep in mind this is the first plugin i write on paint.net, so it probably isn't the most efficient way to do this. Now my question is: what is a good way to display the output string "hex" to the user? I am currently writing to a file. Coming from java development I don't know the tools i can use to do these operations. I tried to display a message box but it seems to open it 8 times and that's why i tried implementing the Boolean done so i make sure it runs only once, but i am not sure why it didn't work.

visual example of what it does:

It starts with the first image, i have to manually select 8 x 8 pixels and run the program to generate these four pairs hexs
[04050B3434742EEC][070E1A3B166E655B]
[20F0782C0A1E3577][E0A090BC3E34771E]
[5455D4F4CA5F7B00][F9FAF3CFFD6E6A00]
[1B9B1B3733FBFE00][0E2FFF5B777B3200]
and using this website, I am able to see the images visually
http://clrhome.org/pix/

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: C# Paint.net Plugin - 8x8 Picture to Grayscale Hexadecimal
« Reply #1 on: June 07, 2015, 08:59:37 am »
That sounds like some pretty awesome and useful tool!

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline c4ooo

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 252
  • Rating: +10/-1
  • The impossible chemical compound.
    • View Profile
Re: C# Paint.net Plugin - 8x8 Picture to Grayscale Hexadecimal
« Reply #2 on: June 07, 2015, 03:45:01 pm »
This is a very useful tool that I might start using.  :thumbsup: The inconvenient part thou is that it runs on a PC, while the rest of the code is [probably, at least for me) on a calculator.  :P
Also, your algorithem to convert pixels to hex seems a bit inconvenient. Idk what is realy going on here (always was a java person and never worked with the PDN API), but I will see if I can try to optimize some of the plugin. :)
Edit:
Here is an optimized version of the plugin. I know the algorithm is error free because i use it in a small pixel to hex converter on my calc, however it might contain syntex errors. :)
Code: [Select]
bool done = false;
void Render(Surface dst, Surface src, Rectangle rect)
{
    Rectangle selection = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();
    ColorBgra CurrentPixel;
    int itr = 3;
    string hex1;
    string hex2;
    string hexi = "0123456789ABCDEF";
    int tempInt1;
    int tempInt2;
    for (int y = selection.Top; y < selection.Bottom; y++)
    {
        if (IsCancelRequested) return;
        for (int x = selection.Left; x < selection.Right;x++)
        {
            CurrentPixel = src[x,y];
            int rgb = 4-((CurrentPixel.R+CurrentPixel.G+CurrentPixel.B)/192);
            if(rgb>1){
tempInt1+= Math.pow(2,itr)
    }
            if(rgb==1||rgb==3){
tempInt2+= Math.pow(2,itr)
            }
    if(itr == 0){
irt = 3;
hex1 += hexi[tempInt1];
                hex2 += hexi[tempInt2];
  temp1 = 0;
temp2 = 0;
            }
            itr--;
        }
    }
    if(done == false)
    {
        System.IO.File.WriteAllText(@"C:\Users\user\Desktop\Dota Duelz\hex.txt", "["+hex1+" "+hex2+"]");
        done = true;
    }
}
And here is a more optimized version, has less of a chance of working then first version, remember that none of this is actually tested. :p
Code: [Select]
bool done = false;
void Render(Surface dst, Surface src, Rectangle rect)
{
    Rectangle selection = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();
    ColorBgra CurrentPixel;
    int itr = 3;
    string hex1;
    string hex2;
    string hexi = "FEDCBA9876543210";
    byte temp1;
    byte temp2;
    for (int y = selection.Top; y < selection.Bottom; y++)
    {
        if (IsCancelRequested) return;
        for (int x = selection.Left; x < selection.Right;x++)
        {
            CurrentPixel = src[x,y];
            byte rgb = (CurrentPixel.R+CurrentPixel.G+CurrentPixel.B)/192;
      temp1=((rgb >> 1) + temp1) *2; //might have to change to temp1=((rgb & 2 >> 1) + temp1) *2;
      temp2=((rgb & 1 )+ temp2) *2;
    if(itr == 0){
irt = 3;
hex1 += hexi[temp1];
                hex2 += hexi[temp2];
  tempInt1 = 0;
tempInt2 = 0;
            }
            itr--;
        }
    }
    if(done == false)
    {
        System.IO.File.WriteAllText(@"C:\Users\user\Desktop\Dota Duelz\hex.txt", "["+hex1+" "+hex2+"]");
        done = true;
    }
}
« Last Edit: June 08, 2015, 06:38:06 am by c4ooo »
-German Kuznetsov
The impossible chemical compound.

Offline Omar Chehab

  • LV2 Member (Next: 40)
  • **
  • Posts: 30
  • Rating: +0/-0
    • View Profile
Re: C# Paint.net Plugin - 8x8 Picture to Grayscale Hexadecimal
« Reply #3 on: June 07, 2015, 11:19:45 pm »
The code is on the computer because it is used to convert regular images such as png, jpeg, bmp to useable grayscale hexadecimal. So on a calculator it would be redundant because all pictures are already in that form! :P
Thanks for your help, I am currently working on making it easy to use so it can be used by the community.
I will look into your optimizations and definitely try to learn and apply the concepts.
« Last Edit: June 07, 2015, 11:24:15 pm by Omar Chehab »

Offline c4ooo

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 252
  • Rating: +10/-1
  • The impossible chemical compound.
    • View Profile
Re: C# Paint.net Plugin - 8x8 Picture to Grayscale Hexadecimal
« Reply #4 on: June 08, 2015, 06:43:23 am »
Can you actualy try to run my actual code? I dont have a C# compiler and was doing stuff with my knolage of java.  :hyper:
I have become interested if it will actually work, and if it does work if the output would is correct, or if it is perhaps inverted or a shade off. If the code actually works i can explain it to you later.  ;)
-German Kuznetsov
The impossible chemical compound.

Offline Omar Chehab

  • LV2 Member (Next: 40)
  • **
  • Posts: 30
  • Rating: +0/-0
    • View Profile
Re: C# Paint.net Plugin - 8x8 Picture to Grayscale Hexadecimal
« Reply #5 on: June 08, 2015, 07:12:20 am »
It had many errors at the start, but after fixing them it would output hexs that arent in the format i am trying to put them in such as:
"[000000000000030070170 F77777777777747727607]"
when selecting 8x8 and huge unseperated hex when selecting more.
I finished up the code I am just reasearching on some more things before i publish this tool

Offline c4ooo

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 252
  • Rating: +10/-1
  • The impossible chemical compound.
    • View Profile
Re: C# Paint.net Plugin - 8x8 Picture to Grayscale Hexadecimal
« Reply #6 on: June 08, 2015, 07:21:27 am »
Hmm thats weird. Can i see what you are trying to convert? Also to get your format just replace the spaces between the hexes with a "][".  And it does not work with spaces bigger then 8*8, its not ment to.
-German Kuznetsov
The impossible chemical compound.

Offline Omar Chehab

  • LV2 Member (Next: 40)
  • **
  • Posts: 30
  • Rating: +0/-0
    • View Profile
Re: C# Paint.net Plugin - 8x8 Picture to Grayscale Hexadecimal
« Reply #7 on: June 08, 2015, 09:35:36 am »
I am just testing random images i have around on my computer, theres nothing in specific. Your code seems to generate for an 8x8 image a hex string bigger than the size i am trying to reach which is 16 numbers. I have fixed this issue though, my script is complete iam just having an issue with paint.net that several instances of this method are being run which is generating redundant data making the out put file hard to read. So what i am doing is i am researching on how to force it to run once.