PureZIP_UnpackMemory

Windows specific forum
User avatar
JHPJHP
Addict
Addict
Posts: 2273
Joined: Sat Oct 09, 2010 3:47 am

PureZIP_UnpackMemory

Post by JHPJHP »

Hi,

I'm trying to compress an image I have encoded using Base64 (saved to a database), then uncompress it at a later date. Modifying the PureZIP_PackMemory example... lets just say that I don't know a lot about assigning strings to memory address' :)

In the following example, the top REMed out string works, but the larger string only produces +++++

Thank you for any insite, your expertise is appreciated:

Code: Select all

;PackData.s = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"

;size reduced for readability
PackData.s = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAoHBwgHBgoICAgLCgoLDhgQDg0NDh0VFhEYIx8lJCIfIiEmKzcvJik0KSEiMEExNDk7Pj4+JS5ESUM8SDc9Pjv/2wBDAQoCw4"
SourceLength = Len(PackData)
Debug SourceLength
Debug PackData

*SourceMemoryID = AllocateMemory(SourceLength)
PokeS(*SourceMemoryID, PackData, SourceLength)
DestinationLength = SourceLength + (SourceLength * 0.01) + 12
*DestinationMemoryID = AllocateMemory(DestinationLength)
PureZIP_PackMemory(*SourceMemoryID, SourceLength, *DestinationMemoryID, @DestinationLength)
PackTest.s = Space(DestinationLength)
PackTest = PeekS(*DestinationMemoryID, @DestinationLength)
Debug Len(PackTest)
Debug PackTest

FreeMemory(*SourceMemoryID)
FreeMemory(*DestinationMemoryID)

*SourceMemoryID = AllocateMemory(SourceLength)
PokeS(*SourceMemoryID, PackTest, SourceLength)
DestinationLength = SourceLength + (SourceLength * 0.01) + 12
*DestinationMemoryID = AllocateMemory(DestinationLength)
PureZIP_UnpackMemory(*SourceMemoryID, SourceLength, *DestinationMemoryID, @DestinationLength)
UnpackTest.s = Space(DestinationLength)
UnpackTest = PeekS(*DestinationMemoryID, @DestinationLength)
Debug Len(UnpackTest)
Debug UnpackTest

FreeMemory(*SourceMemoryID)
FreeMemory(*DestinationMemoryID)
Last edited by JHPJHP on Wed Oct 05, 2011 7:37 pm, edited 3 times in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
JHPJHP
Addict
Addict
Posts: 2273
Joined: Sat Oct 09, 2010 3:47 am

Re: PureZIP_UnpackMemory

Post by JHPJHP »

Ahhh it's a moot point - Base64 being an encryption algorithm, won't have enough similarities to make a difference - I'll need to compress the image at the binary level before the base64 conversion...

Included a small fix to the code from the previous post:

Code: Select all

;PackData.s = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
PackData.s = "The other day I went to the store to buy a bag of milk.  At the store I met an old friend, her name is Susan, she is a good friend - we had coffee together."
SourceLength = Len(PackData)
Debug SourceLength
Debug PackData

*SourceMemoryID = AllocateMemory(SourceLength)
PokeS(*SourceMemoryID, PackData, SourceLength)
DestinationLength = SourceLength + (SourceLength * 0.01) + 12
*DestinationMemoryID = AllocateMemory(DestinationLength)
PureZIP_PackMemory(*SourceMemoryID, SourceLength, *DestinationMemoryID, @DestinationLength)
PackTest.s = Space(DestinationLength)
PackTest = PeekS(*DestinationMemoryID, @DestinationLength)
Debug Len(PackTest)
Debug PackTest

FreeMemory(*SourceMemoryID)
FreeMemory(*DestinationMemoryID)

DestinationLength = SourceLength
*DestinationMemoryID = AllocateMemory(DestinationLength)
SourceLength = Len(PackTest)
*SourceMemoryID = AllocateMemory(SourceLength)
PokeS(*SourceMemoryID, PackTest, SourceLength)
PureZIP_UnpackMemory(*SourceMemoryID, SourceLength, *DestinationMemoryID, @DestinationLength)
UnpackTest.s = Space(DestinationLength)
UnpackTest = PeekS(*DestinationMemoryID, @DestinationLength)
Debug Len(UnpackTest)
Debug UnpackTest

FreeMemory(*SourceMemoryID)
FreeMemory(*DestinationMemoryID)
Last edited by JHPJHP on Wed Oct 05, 2011 7:38 pm, edited 2 times in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
JHPJHP
Addict
Addict
Posts: 2273
Joined: Sat Oct 09, 2010 3:47 am

Re: PureZIP_UnpackMemory

Post by JHPJHP »

Further update...

Using compression on an image SEEMS not to have had much of an effect - it actually gave an unexpected (reverse) result:

Saved a Bitmap image (screenshot) to a NamedPipe
Converted it to JPEG (quality 10)
Resized the image to 25% its original size
[1] not compressed
[2] compressed with PureZIP_PackMemory
[3] compressed with PackMemory (compression level 9)
Encoded it using Base64Encoder
Saved it to a PostgreSQL database (text)

[1] Size: 440232
[2] Size: 444652
[3] Size: 440244

As you can see - not doing any compression (at least with these tests) gave the best result.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Thorium
Addict
Addict
Posts: 1308
Joined: Sat Aug 15, 2009 6:59 pm

Re: PureZIP_UnpackMemory

Post by Thorium »

JHPJHP wrote: As you can see - not doing any compression (at least with these tests) gave the best result.
Not true, you did compress it by converting it to JPEG, even if the quality is set to 10 it still compresses it.
A specialised image compression will be allways better than a general purpose compression.
User avatar
JHPJHP
Addict
Addict
Posts: 2273
Joined: Sat Oct 09, 2010 3:47 am

Re: PureZIP_UnpackMemory

Post by JHPJHP »

For sure, compared to the alternative (leaving it as BMP) - made a world of difference... thanks for the clarification :)

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Post Reply