Reading and writing to a image in memory!?

Just starting out? Need help? Post your questions and find answers here.
zued
User
User
Posts: 27
Joined: Wed Dec 17, 2003 11:20 pm

Reading and writing to a image in memory!?

Post by zued »

Hi.

Cant find any usefull info about peek's and poke's with memory access adressing images(bitmaps).

Here's my 'problem'

Im going to use a dll that does some color correction with colorprofiles
(littleCMS), It cant use the whole image, it whants only partial information about the bitmap(pixel,scanline or 'block')

api info says this:

Code: Select all

/*
        here a loop for translating your image.
     */

     /*
     for (i=0; i < AllScanlinesTilesOrWatseverBlocksYouUse; i++)
     {
           cmsDoTransform(hTransform, YourInputBuffer,
                                      YourOutputBuffer,
                                      YourBuffersSizeInPixels);
     }
     */
So.. how can I adress the image in blocks/chunks/lines?
with gdi/gdi+? or with peek's ?

api for cmsDoTransform says:
-------------------------------------------------------------
_________________________________________________________________________________

void cmsDoTransform(cmsHTRANSFORM hTransform,
LPVOID InputBuffer,
LPVOID OutputBuffer, unsigned int Size);
_________________________________________________________________________________

This function translates bitmaps according of transform. Format
of buffers is described by InputFormat and OutputFormat
parameters in function cmsCreateTransform() or
cmsCreateProofingTransform()

Parameters:
hTransform: A handle to a transform describing the translation.
InputBuffer: A pointer to a input bitmap
OutputBuffer: A pointer to a output bitmap.
Size: the number of PIXELS to be transformed.

Comments:

Windows, stores the bitmaps in a particular way... for speed
purposes, does align the scanlines to doubleword boundaries, so
a bitmap has in windows always a size multiple of 4. This is
ok, since no matter if you waste a couple of bytes, but if you
call cmsDoTransform() and passes it WHOLE image, lcms doesn't
know nothing about this extra padding bytes. So, it assumes
that you are passing a block of BGR triplets with no alignment
at all. This result in a strange looking "lines" in obtained
bitmap.

The solution most evident is to convert scanline by scanline
instead of whole image. This is as easy as to add a for() loop,
and the time penalty is so low that is impossible to detect.
--------------------------------------------------------------------------------

Thanks!

And it would be nice if someone could guide me into
peek poke pointer @ * ? jungle :D

Thanks again!

ZuedTheSwede
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

I normally shut up about convencing people to move to other library, but in this case why are you even bothering dude? you MUST use littleCMS for reals?, else use DevIL library... go for it, and if you want I could PM you about porting it to PB (as I already have done so, but cant give out any code, its private). If not, Im not a littleCMS fan so I'll shut up :twisted: The only good thing about littleCMS is the size heh.. but if you can add 200kb more, then just get DevIL among with its ILU (and compress the devil dll so it weights about 320kb.. and you're done). If not, its either a whim or someones slapping you for saving space
:lol:
Again, if its just for color correction, then it might be better to implement a simple algorithm for this in your PB application, it will weight less also. if that matters now adays.
zued
User
User
Posts: 27
Joined: Wed Dec 17, 2003 11:20 pm

Post by zued »

hehe.. whats up!?..

The reason I find littleCMS is that I use IsedQuickPdf for exporting pdf-pages to bitmaps.. but quickpdf dont have the option/function of using colorprofiles so when exporting from a pdf that is in CMYK-format colors gets some oversaturated/"neon-like"(ok its not that simple like desaturate them).

I shall take a look at devIL lib (never heard of it before).. to see if its something I can use!

Thanks!?
And btw SIZE dosent matter.. or does it? :roll: (cant remeber :wink: )

ZuedTheSwede
zued
User
User
Posts: 27
Joined: Wed Dec 17, 2003 11:20 pm

Post by zued »

ok..dagcrack

I come this far :
result=OpenLibrary(0,"devIL.dll")
result2=OpenLibrary(1,"ILU.dll")
imagename=CreateImage(0,100,100)
owerwrite$="IL_FILE_OVERWRITE"
CallFunction(0,"ilInit")

CallFunction(0,"ilGenImages",1,@imagename)

CallFunction(0,"ilBindImage",imagename)

CallFunction(0,"ilLoadImage","C:\Devil-SDK-1.6.7\lib\kalle1.jpg")

;width=CallFunction(0,"ilGetInteger","IL_IMAGE_WIDTH")
;Debug width
colorprofile=CallFunction(0,"ilApplyProfile","C:\Devil-SDK-1.6.7\lib\SRGBSPAC.ICM","C:\Devil-SDK-1.6.7\lib\CMYK.icm")
CallFunction(1,"iluScale",100,100,8)
Debug colorprofile
errorcode=CallFunction(0,"ilGetError")
CallCFunction(1,"ilEnable",IL_FILE_OVERWRITE)
CallFunction(0,"ilSaveImage","C:\Devil-SDK-1.6.7\lib\devil.jpg")
CallFunction(0,"ilDeleteImages",1,@imagename)
CloseLibrary(0):CloseLibrary(1)

End

Code: Select all


But i cant get the ilApplyProfile and ilEnable to work!?
I tried everything... thats why the code is mess right now :D

question when reading .headern to devIL I se this:
// File definitions
#define IL_FILE_OVERWRITE	0x0620

must I call this to or something to get ilEnable to work?

I found this in the Blitz-forum:
; File definitions 
Const IL_FILE_OVERWRITE = $620 

Is there a way of doing this i pure?

link to blitz-code
[url]http://216.239.59.104/search?q=cache:di-mALz4TMUJ:blitzbasic.com/Community/posts.php%3Ftopic%3D24495+ilApplyProfile&hl=sv[/url]

Thanks again

ZuedTheSwede
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

you need the header file converted...
eg

Code: Select all

Const IL_FILE_OVERWRITE = $620 
=

Code: Select all

#IL_FILE_OVERWRITE = $620 

the "Header Converter" tool following purebasic might be able to help you getting this done quickly..!
zued
User
User
Posts: 27
Joined: Wed Dec 17, 2003 11:20 pm

Post by zued »

Thanks for pointing me in the right direction !!!

ZuedTheSwede
zued
User
User
Posts: 27
Joined: Wed Dec 17, 2003 11:20 pm

Post by zued »

.. only one more question:

ilApplyProfile doesnt work...
everything else in this .dll workes now /with good quality

Anyone used this function before?

api-guide says:

ILboolean ilApplyProfile(
const ILstring InProfile,
const ILstring OutProfile
);

And I have got some valid .icm-files to try with /I know they works


latest code (header converted):

Code: Select all


IncludeFile "C:\_tillwebben\il.pb"
IncludeFile "C:\_tillwebben\ilu.pb"

; #IL_FILE_OVERWRITE = $620
; #IL_IMAGE_WIDTH = $DE4

result=OpenLibrary(0,"devIL.dll")
result2=OpenLibrary(1,"ILU.dll")
imagename=CreateImage(0,100,100)

CallFunction(0,"ilInit")

CallFunction(0,"ilGenImages",1,@imagename)

CallFunction(0,"ilBindImage",imagename)
CallFunction(0,"ilSetSharedPal",#IL_PAL_RGB24)
CallFunction(0,"ilLoadImage","C:\Devil-SDK-1.6.7\lib\kalle2.jpg")

width=CallFunction(0,"ilGetInteger",#IL_IMAGE_HEIGHT)
Debug width
CallFunction(0,"ilApplyProfile",0,"CMYK.icm")

CallFunction(1,"iluImageParameter",#ILU_FILTER,#ILU_SCALE_LANCZOS3)
CallFunction(1,"iluScale",400,700,4)
CallFunction(0,"ilEnable",#IL_FILE_OVERWRITE)
CallFunction(0,"ilSaveImage","C:\Devil-SDK-1.6.7\lib\devil.jpg")
CallFunction(0,"ilDeleteImages",1,@imagename)
CloseLibrary(0):CloseLibrary(1)

End

 
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

hmmm ( ? ) you might input one profile in order to have this function working. If you give null it will try to catch the color profile from the current image, if theres none you should get a return of 0. Hmm I tryed with none profile and the function does not return 0... I wonder why
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

Although... I used a random image and a random profile.. seems to work ok.. did you tried with other profile and/or image to see if that could be the problem?
zued
User
User
Posts: 27
Joined: Wed Dec 17, 2003 11:20 pm

Post by zued »

dagcrack.. did you compile your own dll or did you use the official one?
I heard some "rumors" that the official one missed some of the 3rd parties plugs, like the lcms plug.

/ZuedTheSwede
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

As stated by his license, if I compile my own (I think that if I even did not modify it, but in fact you must always modify a bit..) you must include its source, to public. And I dont want to be giving out source for this source for the other (not that I claim it to be mine, but I dont want a messy product). So thats a "im using one of the 'official' compiled dlls". So far I found out that the functions iluSaturate1f and iluSaturate4f are missing in the ILU dll.. but they are documented..
zued
User
User
Posts: 27
Joined: Wed Dec 17, 2003 11:20 pm

Post by zued »

Thanks dagcrack and thefool for all help!

I cracked the lcms thing today so i thought that i could share some of my findings :) (.. after all help i got from you.. maybe this could be of some assistance for you or others who stumbles into this thread ;))

This only workes between rgb-profiles at the moment (has to come up with a cmyk-rgb value translation before cmyk->rgb-profile workes)

ok here it is:


Code: Select all

# INTENT_PERCUPTUAL = 0
#cmsFLAGS_NOTPRECALC = $100
#TYPE_CMYK_8 = $60021
#TYPE_RGB_8 = $40019


UseJPEGImageDecoder()
  

image1=LoadImage(#PB_Any,"in.jpg")
hda=UseImage(image1)
device1=ImageOutput()
hgt=ImageHeight()
wdt=ImageWidth()


If OpenLibrary(10,"lcms.dll")
  *hin = CallFunction(10,"cmsOpenProfileFromFile","name of the profile.ICC","r") ; - expirement with your profiles
  *hout = CallFunction(10,"cmsOpenProfileFromFile","name of the profile.icc","r")
    Debug *hin
    Debug *hout
    
hdc=StartDrawing(ImageOutput())
   
;--
*transform=CallFunction(10, "cmsCreateTransform",*hin, #TYPE_RGB_8,*hout, #TYPE_RGB_8, #INTENT_PERCUPTUAL, #cmsFLAGS_NOTPRECALC) 
For x=1 To wdt-1
      For y =1 To hgt-1
        pixelvalue.l=GetPixel_(hdc,x,y)
        ; Debug pixelvalue
        
        newpixelvalue.l = 0
        result=CallFunction(10, "cmsDoTransform", *transform, @pixelvalue, @newpixelvalue,1)
        Debug pixelvalue
        SetPixel_(hdc,x,y,newpixelvalue)
        ;SetPixel_(hdc,x,y,RGB(1,1,1)) ; testing...... ;)
        
        
        ; Debug result
        Debug newpixelvalue
      Next
    Next
    CallFunction(10,"cmsDeleteTransform",*transform)
    CallFunction(10,"cmsCloseProfileFrom",*hin)
    CallFunction(10,"cmsCloseProfileFrom",*hout)
     
;--
StopDrawing()     
  
 
SaveImage(image1,"out.bmp")



Else
  MessageRequester(" drop lcms.dll in this/same folder..","Please! drop lcms.dll in this/same folder..",#PB_MessageRequester_Ok)

EndIf

CloseLibrary(10)

End

my lack of programing skills gets better by the hour :lol:
(only logic matters, as the smallest problem always becomes the biggest)

Thanks again

ZuedTheSwede

Btw dagcrack... maybe you could post me an official 'dragcrack' devIL.dll sample :?: (with all the goodies in :) )
anyway thanks!
Post Reply