Page 1 of 1

Help: SaveImage() vs. AllocateMemory()

Posted: Mon Feb 22, 2010 10:32 am
by scriptmaster
Hi all,

I am new to purebasic. I am writing a screen capture program where I am capturing the screen image into a #Image. Now I need the memory contents of #Image to be sent over network with compression, but I do not want to use SaveImage(). Would it be faster to not write to the disk but directly send the memory contents using SendNetworkData()? If so, how do I dump/read the memory contents of a #Image without using SaveImage()? I am not sure how to proceed after AllocateMemory()

Is there a way to do this with existing PB functions? Thanks in advance for your help

Re: Help: SaveImage() vs. AllocateMemory()

Posted: Mon Feb 22, 2010 9:28 pm
by idle
for windows only

Code: Select all


Procedure CopyImageToMemory(ImageNumber, Memory)

   Protected TemporaryDC.L, TemporaryBitmap.BITMAP, TemporaryBitmapInfo.BITMAPINFO

    TemporaryDC = CreateDC_("DISPLAY", #Null, #Null, #Null)
   
    GetObject_(ImageID(ImageNumber), SizeOf(BITMAP), TemporaryBitmap.BITMAP)
       
    TemporaryBitmapInfo\bmiHeader\biSize        = SizeOf(BITMAPINFOHEADER)
    TemporaryBitmapInfo\bmiHeader\biWidth       = TemporaryBitmap\bmWidth
    TemporaryBitmapInfo\bmiHeader\biHeight      = -TemporaryBitmap\bmHeight
    TemporaryBitmapInfo\bmiHeader\biPlanes      = 1
    TemporaryBitmapInfo\bmiHeader\biBitCount    = 32
    TemporaryBitmapInfo\bmiHeader\biCompression = #BI_RGB

    GetDIBits_(TemporaryDC, ImageID(ImageNumber), 0, TemporaryBitmap\bmHeight, Memory, TemporaryBitmapInfo, #DIB_RGB_COLORS)

    DeleteDC_(TemporaryDC)
   
EndProcedure

Procedure CopyMemoryToImage(Memory, ImageNumber)
 
     Protected TemporaryDC.L, TemporaryBitmap.BITMAP, TemporaryBitmapInfo.BITMAPINFO
 
    TemporaryDC = CreateDC_("DISPLAY", #Null, #Null, #Null)
   
    GetObject_(ImageID(ImageNumber), SizeOf(BITMAP), TemporaryBitmap.BITMAP)

    TemporaryBitmapInfo\bmiHeader\biSize        = SizeOf(BITMAPINFOHEADER)
    TemporaryBitmapInfo\bmiHeader\biWidth       = TemporaryBitmap\bmWidth
    TemporaryBitmapInfo\bmiHeader\biHeight      = -TemporaryBitmap\bmHeight
    TemporaryBitmapInfo\bmiHeader\biPlanes      = 1
    TemporaryBitmapInfo\bmiHeader\biBitCount    = 32
    TemporaryBitmapInfo\bmiHeader\biCompression = #BI_RGB
   
    SetDIBits_(TemporaryDC, ImageID(ImageNumber), 0, TemporaryBitmap\bmHeight, Memory, TemporaryBitmapInfo, #DIB_RGB_COLORS)
   
    DeleteDC_(TemporaryDC)
 
EndProcedure


Re: Help: SaveImage() vs. AllocateMemory()

Posted: Mon Feb 22, 2010 9:43 pm
by Fred
You can use DrawingBuffer() as well to get direct pixel access.

Re: Help: SaveImage() vs. AllocateMemory()

Posted: Tue Feb 23, 2010 7:52 am
by Thorium
You can use the include for my image format. It can encode a PB image without writing it to disk.
http://www.purebasic.fr/english/viewtop ... 27&t=39456
Download it, include it into your project.

Then you can use "Tsi_Save2MemoryFromImage" to encode your image. If you want to send it over the network don't use "Tsi_BestSave2MemoryFromImage" because it's much slower, instead use "Tsi_Save2MemoryFromImage" where you have to specify compression, filter and transformation settings, so the encoder don't have to test for the best combination.

Use it like that:

Code: Select all

Define.i EncodedImageSize
Define *EncodedImage

*EncodedImage = Tsi_Save2MemoryFromImage(PBImage, @ImageSize, #Tsi_Filter_Up, #Tsi_Transformation_AlphaSort, #Tsi_Compression_Zlib, 9)
That will likely result in a good compression ratio.
*EncodedImage is the pointer to the encoded image and EncodedImageSize is the size of the image in bytes. So you just can send it over network and on the other side use "Tsi_CatchTsi2Image(*Tsi)" it's basicly the same as PB's CatchImage but will catch a TSI and returns a PB image number.