Jump to content

8 bpp raw file


caseyd

Recommended Posts

Hi,

I am trying to work with some heightmaps in Paint.net, and am looking to write a plugin that will do the following:

Load and save an 8bpp greyscale image. No header information, just the pixel data in a RGB format. I tried using the RawLoader plugin, and mucked around with its settings but no dice. I am currently using Gimp for this, but would like to be able to use PDN for it all. I am new to C# as well, but here was my first stab at doing this:

int width = 257;
               int height = 257;

               byte[] data = new byte[width * height];

               for (int index = 0; index < width * height; index++)
                   data [index] = (byte)input.ReadByte ();

               Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);

               //Create a BitmapData and Lock all pixels to be written
               BitmapData bmpData = bmp.LockBits(
                                    new Rectangle(0, 0, bmp.Width, bmp.Height),
                                    ImageLockMode.WriteOnly, bmp.PixelFormat);

               //Copy the data from the byte array into BitmapData.Scan0
               System.Runtime.InteropServices.Marshal.Copy(data, 0, bmpData.Scan0, data.Length);


               //Unlock the pixels
               bmp.UnlockBits(bmpData);

               return Document.FromImage(bmp);

It loads the image, but it is all messed up. I am pretty sure it has to do with the PixelFormat. Is there an easier way to do this?

Thanks,

Casey

Link to comment
Share on other sites

protected unsafe override Document OnLoad(Stream input)
       {
//create our surface
           Surface surface = new Surface(width, height);
//grab a pointer to the image's memory
           ColorBgra* ptr = surface.GetRowAddress(0);

           int b;
//loop through the stream. (+perf by making a buffer and reading a few bytes at a time?)
           for (int i = 0; i             {
//read
               b = input.ReadByte();
//set the pixel's a to opaque, and r,g,and b to the read value
               ptr->Bgra = (uint)((255 ;
//move to the next pixel in memory.
               ptr++;
           }

//return
           return Document.FromImage(surface.CreateAliasedBitmap());
       }

A lot of room for improvement, performance-wise, but it does the trick. I think. Worked with the images I tested, anyway.

xZYt6wl.png

ambigram signature by Kemaru

[i write plugins and stuff]

If you like a post, upvote it!

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...