Use JPG, JIF, GIF, BMP, DIB, RLE, TGA and PCX

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

This procedure loads many different image formats
by using the free NVIEWLIB.DLL available here:
http://www.programmersheaven.com/file.asp?FileID=2045

Code: Select all

Procedure LoadNViewImage(ImageNumber,FileName$)
;
; Load JPG, JIF, GIF, BMP, DIB, RLE, TGA and PCX images with PB and NViewLib.DLL
; NViewLib 1.1.4 is free: [url]http://www.programmersheaven.com/file.asp?FileID=2045[/url]
;
;
; SYNTAX
;    ImageHandle = LoadNViewImage(#Image, FileName$)
;
; DESCRIPTION
;    Load the specified image.
;    The image format can be a JPG, JIF, GIF, BMP, DIB, RLE, TGA or PCX file.
;    If the function fails, 0 is returned, Else all is fine.
;    This command requires the NVIEWLIB.DLL in the Path.
;
   If OpenLibrary(0,"nviewlib.dll")
      AddrImage = CallFunction(0,"NViewLibLoad",FileName$,0)
      Width     = CallFunction(0,"GetWidth"))
      Height    = CallFunction(0,"GetHeight"))
      newImage  = CreateImage(ImageNumber,Width,Height)
                  StartDrawing(ImageOutput())
                     DrawImage(AddrImage,0,0)
                  StopDrawing()
                  DeleteObject_(AddrImage)
      CloseLibrary(0)
   Else
      MessageRequester("ERROR","Cant find NVIEWLIB.DLL in Path",0):End
   EndIf
ProcedureReturn newImage
EndProcedure






File$ = OpenFileRequester("Load Image", "", "All supported images | *.jpg;*.jpeg;*.jif;*.gif;*.bmp;*.dib;*.rle;*.tga;*.pcx | All files *.* | *.*", 0)
If File$  ""
 If LoadNViewImage(1,File$)

  OpenWindow(0,200,200,ImageWidth(),ImageHeight(),#PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget,"PB Image Viewer")
  SetForeGroundWindow_(WindowID())

  Repeat
     event=WaitWindowEvent()
     If event=#WM_PAINT
        StartDrawing(WindowOutput())
           DrawImage(ImageID(),0,0)
        StopDrawing()         
     EndIf
  Until event=#PB_EventCloseWindow

 EndIf
EndIf
End
The NVIEWLIB.DLL must be in the same directory
like your created .exe file or in the windows-path.
At development time, the .DLL must also be in the
directory \PureBasic\Compilers\ (for compiling with F5) !!

Thanks for the hint go to halo.

Have fun...
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> This procedure loads many different image formats
> by using the free NVIEWLIB.DLL available here:
> http://www.programmersheaven.com/file.asp?FileID=2045

Thanks Danilo -- I've used NViewLib with Visual Basic before but
wasn't sure how to use it with PureBasic, but now I know! :)

UPDATE: Danilo, do you know how to get this DLL to save an Image
as a JPEG? It does support JPEG saving but I'm not sure how to
do it...


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

Hey PB, basically you do this:
CallFunction(0,"NViewLibSaveAsJPG",80,"test.jpg")
0 =high compression/poor image quality
80=low compression/high image quality


Here is what the docs say:

Saving as Jpeg is little tricky... You CANNOT save GIF and PCX files as jpg using
NViewLibSaveAsJPG, but you can still save them if you save them to file as bitmap and load bitmap and save as jpg like this,

// Try NViewLibSaveAsJPG first
if not NViewLibSaveAsJPG(80, 'junk.jpg') then
begin
//If NViewLibSaveAsJPG did not then work save as bitmap.
Image1.picture.savetofile('~temp.bmp');
//Load the bitmap
Image1.Picture.Bitmap.Handle:= NViewLibLoad('~temp.bmp',false);
//Save the bitmap as jpeg with 80% of the original picture quality
NViewLibSaveAsJPG(80, 'junk.jpg');
//Delete the bitmap from hard drive and done!
DeleteFile('~temp.bmp');
end;
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> Hey PB, basically you do this:
> CallFunction(0,"NViewLibSaveAsJPG",80,"test.jpg")

Hmm, but how does NViewLib know to save the image from an Image, if
I'm not loading the image with NViewLib? The image I want to save is
created at runtime, not loaded from disk. So NViewLib somehow needs
to know how to save it, and I don't see how using the function shown
above (ie. there's no reference to an image handle?).


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

I would try it like Paul said:

Code: Select all

SaveImage(1, "temp.bmp")
NViewLibLoad("temp.bmp", 0)
NViewLibSaveAsJPG(80, "my.jpg")
With PureBasic´s SaveImage() saved to a temp .bmp

cya,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

Thanks Paul/Danilo: it works, although I don't like saving and loading
a large bitmap to disk (2.5 MB) just to achieve the goal. :cry:


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

Thats NViewLIB, not our fault...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> Thats NViewLIB, not our fault...

I know -- I wasn't implying it was your or Paul's fault.


PB - Registered PureBasic Coder
Post Reply