memory block of the RGBSurface?

I can get a memory block of a picture by calling GetData method.
But is that all the header information as well as the pixel data?
What if I only want to pixel data… like … how do I convert a RGBSurface to a MemoryBlock?
Going through each pixel of the RGBSurface and writing R, G & B as U8Ints sounds time consuming!

Store the data for the picture directly as an RGBSurface

//Dim mb as MemoryBlock = Picture.FromData(data)
Dim r as RGBSurface = Picture.FromData(data).RGBSurface

What exactly are you trying to achieve by placing the RGBSurface in a MemoryBlock? If you want specific pixel data you’ll have to tap directly into the RGBSurface. As a MemoryBlock, the pixel data is going to be a binary jumbled mess.

Perhaps if you can explain what your end goal is, we can find a fast solution.

Although, going through each pixel, can in fact be quite fast. I wrote a program a while back that converts files directly to images for sharing via the web, and could convert a 20MB file into a nice compact image pixel by pixel and back to file in a few seconds.

Not sure I get you Matt…
dim p as new picture…
dim r as RGBSurface = p.RGBSurface
I don’t want an RGB Surface I want a memory block
I want that binary… not jumbled… not a mess.

dim p as new picture(x) dim m as new MemoryBlock(0) dim o as integer=0 for r as integer=0 to p.Height-1 for c as integer=0 to p.Width-1 m.UInt8Value(o) = p.RGBSurface.Pixel(r,c).Red m.UInt8Value(o+1) = p.RGBSurface.Pixel(r,c).Green m.UInt8Value(o+2) = p.RGBSurface.Pixel(r,c).Blue o = o + 3 next next

This in my opinion is gonna be slow.

[quote=205594:@Brian O’Brien]I can get a memory block of a picture by calling GetData method.
But is that all the header information as well as the pixel data?[/quote]
It’s pixel data.

I believe its header and pixel data.

You can get the BMP data and it’s header is only 54 bytes. Just skip that and the rest of the data are raw pixel values in, if I remember correctly, ABGR format. I use this to get image data for OpenGL textures.

[code]dim pic As new Picture(10, 10)

dim data As MemoryBlock = pic.GetData(Picture.FormatBMP)

dim p As Ptr = data //faster access

p.Single(54) = first byte of pixel data
[/code]

[quote=205615:@Brian O’Brien]for r as integer=0 to p.Height-1
for c as integer=0 to p.Width-1
m.UInt8(o) = p.RGBSurface.Pixel(r,c).Red
m.UInt8(o+1) = p.RGBSurface.Pixel(r,c).Green[/quote]

1 use a Ptr for faster access
2 don’t re-get the RGBSurface and Color every time
3 define values outside of loops

[code]dim pic as new picture(10, 10)
dim surf As RGBSurface = pic.RGBSurface

dim m as new MemoryBlock(p.Width * p.Height * 3)
dim p As Ptr = m

dim idx as integer = 0
dim col As Color
dim c As integer
dim lastR As integer = p.Height-1
dim lastC As integer = p.Width-1

for r As integer = 0 to lastR
for c = 0 to lastC
col = surf.Pixel(r, c)
p.UInt8(idx) = col.Red
p.UInt8(idx+1) = col.Green
p.UInt8(idx+2) = col.Blue
idx = idx + 3
next
next[/code]

or for BGR format…

dim m as new MemoryBlock(p.Width * p.Height * 3 + 1) //plus 1 for extra alpha byte //... for r As integer = 0 to lastR for c = 0 to lastC p.Color(idx) = surf.Pixel(r, c) idx = idx + 3 next next

Why do you need it as MemoryBlock? Proper use of RGBSurface is generally pretty fast. They would need to be timed to know for sure, don’t rely on it seems one way is faster. Also, don’t forget to flip pragmas for top speed.

And I believe using the Y position as the outer loop provides some extra performance (or so I’ve been lead to believe)

for y As integer = 0 to surf.height-1
   for x= 0 to surf.width-1
      col = surf.Pixel(x,y)
   next x
next y

With MBS Plugins?

This plugin part has a couple of methods to copy from picture to/from memoryblock:
https://www.monkeybreadsoftware.net/pluginpart-picturememory.shtml

And that’s faster than using RGBSurface.

Thanks everyone.

Ironically in tests that I did (a fair few years ago) I found RGB surface to be the quickest way in pure Xojo code.

On OS X you can use declares to get a memory block of pixel data, in various formats and such.

The quickest way (on OS X) is to use CoreImage, if you need to, you can write your own CIKernels and have CoreImage process them on the GPU or multi-core CPU.

Or take a look at the Einhugur picture effects plugin, Einhugur Software - Plugin Libraries for Xojo