Jump to content

8x8 Picture to Grayscale Hexadecimal


Recommended Posts

Hello, I have recently needed a converter to hexadecimal from colored images. This plugin is used for 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.

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.

 

EDIT:

Forgot to add a visual example of what it does:

DXzGbrB.png

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/

Edited by Omar Chehab
Link to comment
Share on other sites

Hi Omar & welcome to the forum.

First of all - well done on your first plugin!

I can't escape the feeling that you're re-creating the wheel in trying to write this yourself. Especially since you're trying to get a CodeLab plugin to do something it wasn't originally designed to to. You see CodeLab plugins (Effect plugins) are meant to be used to read pixels in from the source selection or layer, process those pixels and output the pixels to the destination canvas.

In your case you really need a filetype plugin - one that will write the formatted pixel data out to a file.

Why not this process

1. Convert the entire image to grey scale.

2. Copy an 8x8 pixels block to a new image

3. Use the CSV Filetype plugin to save the new image to hex format.

The CSV plugin will handle an image of any size & allows output in a number of formats.

Link to comment
Share on other sites

Hi Omar & welcome to the forum.

First of all - well done on your first plugin!

I can't escape the feeling that you're re-creating the wheel in trying to write this yourself. Especially since you're trying to get a CodeLab plugin to do something it wasn't originally designed to to. You see CodeLab plugins (Effect plugins) are meant to be used to read pixels in from the source selection or layer, process those pixels and output the pixels to the destination canvas.

In your case you really need a filetype plugin - one that will write the formatted pixel data out to a file.

Why not this process

1. Convert the entire image to grey scale.

2. Copy an 8x8 pixels block to a new image

3. Use the CSV Filetype plugin to save the new image to hex format.

The CSV plugin will handle an image of any size & allows output in a number of formats.

 

Thank you for your reply, I was really confused by this. I will look into applying what you told me.

 

On a sidenote, how can I transfer this code from the render method to the onsetrenderinfo method so it can run only once? I can't seem to figure it out since the parameters differ.

Edited by Omar Chehab
Link to comment
Share on other sites

Maybe I've put you wrong.  Are you using CodeLab or Visual Studio?

 

Have a look at this template for Filetype plugins, it might get you started: http://forums.getpaint.net/index.php?/topic/26034-propertybasedfiletype-template-using-indirectui/

 

With Filetypes you get handed a Stream by paint.net to write the output to. No need to create the file handling dialogs or anything like that, so you can concentrate on writing your output.

Link to comment
Share on other sites

Maybe I've put you wrong.  Are you using CodeLab or Visual Studio?

 

Have a look at this template for Filetype plugins, it might get you started: http://forums.getpaint.net/index.php?/topic/26034-propertybasedfiletype-template-using-indirectui/

 

With Filetypes you get handed a Stream by paint.net to write the output to. No need to create the file handling dialogs or anything like that, so you can concentrate on writing your output.

 

Thanks for your time, much appreciated.

Link to comment
Share on other sites

Maybe I've put you wrong.  Are you using CodeLab or Visual Studio?

 

Have a look at this template for Filetype plugins, it might get you started: http://forums.getpaint.net/index.php?/topic/26034-propertybasedfiletype-template-using-indirectui/

 

With Filetypes you get handed a Stream by paint.net to write the output to. No need to create the file handling dialogs or anything like that, so you can concentrate on writing your output.

 

Okay, I have looked into filetypes. They look pretty interesting, how ever I can't seem to implement what I have currenty which is a codelab render script onto the filetype script on visual studio 2010. I don't understand how filetypes work and i cant find a propper newbie tutorial that takes me through installation and setup as well.

 

I really want to fix this issue, and what's bothering me is the fact that it is such a simple problem. It's taking me ages to solve, i keep running into brick walls. 

 

How can I force the render method in codelab to run once? I am aware that it will slow down the process, however my plugin deals with pixel art so it will always be fast

 

Ive looked into OnSetRenderInfo, where is that used? Codelab or visual studio? How can I implement it into code lab with the parameters of the render function? Is it possible to access those parameters on it?

Link to comment
Share on other sites

The Render routine is run a number of times. Basically the Effect is passed a Rectangle of Interest (ROI). This gives a number of pixels to the Effect to process. When that is done, another ROI is passed.

You can't control how many times this occurs from a CodeLab plugin.

More on this >> http://boltbait.com/pdn/CodeLab/help/overview.asp

In Visual Studio (not CodeLab) you have access to OnSetRenderInfo - this fires once, so it's a good place to do one-of-a-kind processing.

There is still the fundamental difference between Effect plugins and Filetype plugins to consider:

If you are altering pixels on the canvas you need an Effect plugin built with CodeLab or Visual Studio.

If you need to run complex code once only you need to build it with Visual Studio with it's access to OnSetRenderInfo.

If you want to write output to a file you should use a Filetype plugin.

What type of file are you wishing to write to? Text?

What parameters or configurable options do you need to access via the plugin dialog?

Link to comment
Share on other sites

How can I force the render method in codelab to run once? I am aware that it will slow down the process, however my plugin deals with pixel art so it will always be fast

http://forums.getpaint.net/index.php?/topic/880-codelab-for-advanced-users-v25-released-march-14-2015/?p=413407

http://forums.getpaint.net/index.php?/topic/880-codelab-for-advanced-users-v25-released-march-14-2015/?p=421942

Link to comment
Share on other sites

The Render routine is run a number of times. Basically the Effect is passed a Rectangle of Interest (ROI). This gives a number of pixels to the Effect to process. When that is done, another ROI is passed.

You can't control how many times this occurs from a CodeLab plugin.

More on this >> http://boltbait.com/pdn/CodeLab/help/overview.asp

In Visual Studio (not CodeLab) you have access to OnSetRenderInfo - this fires once, so it's a good place to do one-of-a-kind processing.

There is still the fundamental difference between Effect plugins and Filetype plugins to consider:

If you are altering pixels on the canvas you need an Effect plugin built with CodeLab or Visual Studio.

If you need to run complex code once only you need to build it with Visual Studio with it's access to OnSetRenderInfo.

If you want to write output to a file you should use a Filetype plugin.

What type of file are you wishing to write to? Text?

What parameters or configurable options do you need to access via the plugin dialog?

 

I was using write to file because I just wanted an output of some kind, I changed it to message box and that's what I am staying with.

 

I don't want any parameters on my plugin dialog either.

 

After lots of research I managed to finish up the plugin, it's for personal use so the conditional running isn't a problem.

 

Thanks for your time! I fixed my issue by implementing my RENDER code to the ONSETRENDERINFO method

Edited by Omar Chehab
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...