FBImage static Win/Lin 32/64-bit

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Roland Chastain
Posts: 1008
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Library FBImage for Windows/Linux 32/64-bit.

Post by Roland Chastain »

Yet another great contribution! Thank you D.J.Peters.

Tested successfully all the examples under Windows 10.
lordbla9
Posts: 11
Joined: Feb 25, 2016 6:55

Re: Library FBImage for Windows/Linux 32/64-bit.

Post by lordbla9 »

Hi Joshy, Not sure if this is what you're expecting.
Under Ubuntu Linux 64bit it displayed a window with a solid blue circle on a red background and it saved a file that displayed a window with a solid blue circle on a black and white checkerboard background. I would display the file here but I still haven't figured out how to display images in these comments so I did my best to describe it. Hope this helps.

PS: Did you get CSFML working on 64bit linux already? I really need audio input for alot of my progs. Thx
D.J.Peters
Posts: 8588
Joined: May 28, 2005 3:28
Contact:

Re: Library FBImage for Windows/Linux 32/64-bit.

Post by D.J.Peters »

@lordbla9 first you should delete the old FBImage folder from your home folder and any old FBImage *.so file from your system may be in /usr/lib.

Donload latest version and extract the archive in your home folder now you are ready to compile the *.bas files.
again don't owerwrite any old FBImage folder delete it before

"solid blue circle on a red background" which example do you mean ? post the result here please.

You have to upload the image on any server and than use the "img" tag something like this:

Code: Select all

[img]http://server.xyz/image.png[/img]
And yes CSFML works here from begining you have to learn how to make the right symbolic links (the symbolic links in the zip file are absolute to my home folder and must be changed to relative links) but you can read it in the CSFML thread.

Joshy
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Library FBImage for Windows/Linux 32/64-bit.

Post by leopardpm »

lordbla9 wrote:I would display the file here but I still haven't figured out how to display images in these comments...
I use a server called "PostImage, the link is: https://postimg.org/

once you upload your image (screen shot, or whatever), it will give you a 'Direct Link' which you copy then paste into your forum post between the IMG tags as Herr Peters said above:

Code: Select all

[img]http://s19.postimg.org/n2o7yvt9v/no_profile_image5.png[/img]
which, in this case, will show the following image in this post:
Image
jdebord
Posts: 550
Joined: May 27, 2005 6:20
Location: Limoges, France
Contact:

Re: Library FBImage for Windows/Linux 32/64-bit.

Post by jdebord »

lordbla9 wrote: Under Ubuntu Linux 64bit it displayed a window with a solid blue circle on a red background and it saved a file that displayed a window with a solid blue circle on a black and white checkerboard background
Are you referring to the example that I have posted before ? In this case, the checkerboard would correspond to the transparent background.
jdebord
Posts: 550
Joined: May 27, 2005 6:20
Location: Limoges, France
Contact:

Re: Library FBImage for Windows/Linux 32/64-bit.

Post by jdebord »

Is there a version of this library for the Raspberry Pi ? Do I need to recompile the source code ?

I use a Raspberry Pi 3 with FB from the debian package at freebasic-portal.de and FLTK from the arm library which is included in the FLTK package.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Library FBImage for Windows/Linux 32/64-bit.

Post by MrSwiss »

jdebord wrote:Is there a version of this library for the Raspberry Pi ? Do I need to recompile the source code ?
If the first answer is NO (I don't know if there is). Then the second is YES (different Target).
jdebord
Posts: 550
Joined: May 27, 2005 6:20
Location: Limoges, France
Contact:

Re: Library FBImage for Windows/Linux 32/64-bit.

Post by jdebord »

I have compiled the library on the Raspberry Pi but when I try to compile a demo program I get an error:

Code: Select all

undefined reference to `stbi_write_png'
What did I miss ?
jdebord
Posts: 550
Joined: May 27, 2005 6:20
Location: Limoges, France
Contact:

Re: Library FBImage for Windows/Linux 32/64-bit.

Post by jdebord »

I think I have found the bug :)

For compiling with GCC on the Raspberry Pi : in the file FBImage.c, you must add the line:

Code: Select all

#define STB_IMAGE_WRITE_IMPLEMENTATION
before the line:

Code: Select all

#include "stb_image_write.h"
(This is explained in the comments in stb_image_write.h)

Then compile the library:

Code: Select all

gcc -c -O2 FBImage.c
gcc -c -O2 image_DXT.c
gcc -c -O2 image_helper.c
gcc -c -O2 stb_image_aug.c
ar r libFBImage-32-static.a *.o
Place the library (libFBImage-32-static.a) in /usr/lib/freebasic/linux-arm/ and the include file (FBImage.bi) in /usr/include/freebasic

Now all the demo programs should work.

For those who are interested, I have placed a copy of the compiled library at:

https://www.dropbox.com/s/0hj0chbzyz1a9 ... tic.a?dl=0
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Library FBImage for Windows/Linux 32/64-bit.

Post by vdecampo »

How did I miss this library?!
Another great addition thanks to DJ!

You rock!

-Vince
moises1953
Posts: 3
Joined: Aug 25, 2017 9:33
Location: Santander - Spain

Re: Library FBImage windows/Linux 32/64-bit.

Post by moises1953 »

D.J.Peters wrote:Of course the result of LoadRGBAFile() and LoadRGBAMemory()
is an 32 bit fbgfx image so you can use it with 24 or 32bit fbgfx modes.
If you ned RGB images in 16 bit mode here a simple converter.

Joshy....
Here is code to use images in 8 bit mode preserving the standard VGA palette:
Works in "fblite" dialect, but i think that must work in "fb" also
Buids a first 32 basic color palette, adding new colors on the fly, and uses a distance algorithm to locate colors exceding 223
Is based in the Joshy's 16 bits conververter, so reads the same file formats.

Code: Select all

#LANG "fblite"
#include once "FBImage.bi"
#include once "string.bi"

CONST NEGRO = 0, AZUL = 1, VERDE = 2, CIAN = 3
CONST ROJO = 4, MAGENTA = 5, MARRON = 6, BLANCO = 7

CONST GRISCLA=8, AZULCLA = 9, VERDECLA = 10, CIANCLA = 11
CONST ROJOCLA = 12, MAGENTACLA = 13, AMARILLO = 14, BLANCOCLA = 15

DIM img AS ANY PTR

declare function Load8BitRGB(byval filename as const zstring PTR, BYREF mensaje$) as any PTR

OPEN ERR FOR OUTPUT as#1
CHDIR exepath()

'imgfile$="carter-8-8.png"
'imgfile$="carter-8-4.png"
imgfile$="tire.jpg"
'imgfile$="test_rgb.png"
'imgfile$="color.jpg"
SCREENRES 640,480,8
WIDTH 80,30

t0=TIMER
'img = LoadRGBAFile(imgfile$)
PRINT #1, imgfile$
img = LOAD8BITRGB(imgfile$, men$)
t1=TIMER-t0

if img=0 THEN
  print #1, "error cargando: "& *GetLastResult()
  BEEP
ELSE
  PRINT #1, "Tiempo de carga:"; FORMAT$(1000*t1,"##0.00")
  IF LEN(men$) THEN PRINT #1, men$
  LINE (0,0)-(8*LEN(imgfile$)+15, 15),CIAN,BF
  PUT (0,17),img,PSET
END IF
DRAW STRING (8,0), imgfile$,AMARILLO
SLEEP

END


FUNCTION Load8BitRGB(BYVAL filename as const zstring PTR, mensaje$) as any ptr
  CONST ColorMargin=7
  DIM AS ANY PTR imgSrc, imgDst  
  imgSrc = LoadRGBAFile(filename)
  mensaje$=""
  IF imgSrc=0  THEN 
  	FUNCTION = 0
  	EXIT FUNCTION
  ENDIF
  DIM AS INTEGER w,h,bypp,spitch,dpitch 
  DIM AS UBYTE PTR s, d
  
  DIM AS LONG i,j,k,kmin,x,y,dr,dg,db,distan,distamin,pal(0 TO 255,0 TO 2)
  DIM AS UBYTE ipal, lastpal
  
  IMAGEINFO imgSrc,w,h,bypp,spitch,s
  PRINT #1, "Bypp:";bypp
  
	imgDst = IMAGECREATE(w,h)'8bpp
  IMAGEINFO imgDst,,,,dpitch,d 
  'Build palette
  'standard VGA colors to draw and label images
  'pal(NEGRO,0)=0 black
  pal(AZUL,0)=128 'blue 1
  pal(VERDE,1)=128 'green 2
  pal(CIAN,1)=128:pal(CIAN,0)=128
  pal(ROJO,2)=128 'red 4
  pal(MAGENTA,2)=128:pal(MAGENTA,0)=128
  pal(MARRON,2)=128: pal(MARRON,1)=128 'brown 6
  pal(BLANCO,2)=128: pal(BLANCO,1)=128: pal(BLANCO,0)=128 'white 7 
  pal(GRISCLA, 2)=192: pal(GRISCLA, 1)=192: pal(GRISCLA, 0)=192 'light white 8
  pal(AZULCLA,0)=255 'light blue 9 
  pal(VERDECLA,1)=255 'light green 10
  pal(CIANCLA,1)=255:pal(CIANCLA,0)=255 'light cian 11
  pal(ROJOCLA,2)=255 'light red  12
  pal(MAGENTACLA,2)=255:pal(MAGENTACLA,0)=255 'Light magenta 13
  pal(AMARILLO,2)=255: pal(AMARILLO,1)=255 'yellow (light brown) 14
  pal(BLANCO,2)=128: pal(BLANCO,1)=128: pal(BLANCO,0)=128 'super light white 15 
  lastpal=15
  
  'aditional palette for coberture
  FOR i=1 TO 8  
    FOR j= 0 TO 2
      pal(i+lastpal,j)=(pal(i,j)+pal(i+8,j))\2
    NEXT j
  NEXT
  lastpal=23 'medium
   
  FOR i=1 TO 8  
    FOR j= 0 TO 2
     pal(i+lastpal,j)=pal(i,j)\2
    NEXT j
  NEXT
  lastpal=31 'darkest
  
  FOR i=0 TO lastpal  
    PALETTE i, pal(i,2), pal(i,1), pal(i,0) 'rgb
  NEXT
  
  FOR y = 1 TO h
  	i=0
    FOR x =0 to w-1
      IF s[i+2] OR s[i+1] OR s[i] THEN
        distamin=446:kmin=0
        FOR k=1 TO lastpal
        	dr=ABS(s[i+2]-pal(k,2))
        	dg=ABS(s[i+1]-pal(k,1))
        	db=ABS(s[i]-pal(k,0))
        	IF dr<ColorMargin AND dg<ColorMargin AND db<ColorMargin THEN EXIT FOR
        	distan=SQR(dr*dr+dg*dg+db*db)
        	IF distan<distamin THEN 'nearest
        		distamin=distan
        		kmin=k
        	ENDIF
        NEXT
        IF k>lastpal THEN
        	IF k<=255 THEN 
        		lastpal=k
        		pal(k,0)=s[i]:pal(k,1)=s[i+1]:pal(k,2)=s[i+2]
        		PALETTE k,s[i+2],s[i+1],s[i]
        	ELSE 'select nearest
            IF k>255 THEN mensaje$= "Big palette"
        		k=kmin
        	ENDIF
        END IF
        ipal=k
      ELSE
     	  ipal=0
      END IF
      d[x] = ipal
      i+=4 ' next source pixel
    NEXT
    s+=spitch ' next src row
    d+=dpitch ' next dst row
  NEXT
  PRINT #1, "Palette colors:";lastpal
  IMAGEDESTROY imgSrc
  FUNCTION = imgDst
  #UNDEF RGB8
END function
moises1953
Posts: 3
Joined: Aug 25, 2017 9:33
Location: Santander - Spain

Re: Library FBImage for Windows/Linux 32/64-bit.

Post by moises1953 »

New version of 8 bit image converter:
- Some defects eliminated
- Standard 16 initial colors preserved. Up to 240 new colors replaced (from 16 to 255) taken on the fly from image
- Uses a tolerance algorithm (ColorMargin) to locate colors under 255, and minimum distance over 255
- Now runs also in "fb" dialect

Test program

Code: Select all

#LANG "fblite"
#INCLUDE ONCE "FBImage-lite.bi"
#INCLUDE ONCE "string.bi"

CONST NEGRO = 0, AZUL = 1, VERDE = 2, CIAN = 3
CONST ROJO = 4, MAGENTA = 5, MARRON = 6, BLANCO = 7

CONST GRISCLA=8, AZULCLA = 9, VERDECLA = 10, CIANCLA = 11
CONST ROJOCLA = 12, MAGENTACLA = 13, AMARILLO = 14, BLANCOCLA = 15

DIM img AS ANY PTR
DIM AS STRING men, imgfile
DIM AS DOUBLE t0, t1
DIM AS SHORT i,j,k

DECLARE FUNCTION Load8BitRGB(byval filename as const zstring PTR, BYREF mensaje AS STRING) as any PTR

OPEN ERR FOR OUTPUT as#1
CHDIR exepath()
'SCREENRES 640,480,24 'RGB
SCREENRES 640,480,8
WIDTH 80,30 'font 16x8

'imgfile="carter-8-8.png"
'imgfile="tire.jpg"
'imgfile="test_rgb.png"
imgfile="color.jpg"

PRINT #1, imgfile
t0=TIMER
'img = LoadRGBAFile(imgfile$)
img = LOAD8BITRGB(imgfile, men)
t1=TIMER-t0

if img=0 THEN
  print #1, "error cargando: "& *GetLastResult()
  BEEP
ELSE
  PRINT #1, "Tiempo de carga (ms):"; FORMAT$(1000*t1,"##0.00")
  IF LEN(men) THEN PRINT #1, men
  LINE (0,0)-(8*LEN(imgfile)+15, 15),CIAN,BF
  PUT (0,17),img,PSET
END IF
DRAW STRING (8,0), imgfile,AMARILLO
FOR i=0 TO 3
  LOCATE i+26	
	FOR j=0 TO 63
	  k=i*64+j
	  COLOR 0,k
	  LOCATE,j+1+j\16:PRINT FORMAT$(j MOD 8, "0");
	NEXT
NEXT
COLOR 7
SLEEP

END
Load8BitRGB: Converter

Code: Select all

FUNCTION Load8BitRGB(BYVAL filename as const zstring PTR, mensaje AS STRING) as any ptr
  CONST ColorMargin=15
  DIM AS ANY PTR imgSrc, imgDst  
  imgSrc = LoadRGBAFile(filename)
  mensaje=""
  IF imgSrc=0  THEN 
  	FUNCTION = 0
  	EXIT FUNCTION
  ENDIF
  DIM AS INTEGER w,h,bypp,spitch,dpitch 
  DIM AS UBYTE PTR s, d
  
  DIM AS LONG i,j,k,kmin,x,y,dr,dg,db,distan,distamin,pal(0 TO 255,0 TO 2)
  DIM AS UBYTE ipal, lastpal
  
  IMAGEINFO imgSrc,w,h,bypp,spitch,s
	imgDst = IMAGECREATE(w,h)'8bpp
  IMAGEINFO imgDst,,,,dpitch,d 
  
  'Initial palette:
  'standard VGA first 16 colors to draw and label images
  lastpal=15
  FOR i=0 TO lastpal  
    PALETTE GET i, pal(i,2), pal(i,1), pal(i,0) 'rgb
  NEXT

  'aditional palette on the fly
  FOR y = 1 TO h
  	i=0
    FOR x =0 to w-1
      IF s[i+2] OR s[i+1] OR s[i] THEN
        distamin=446:kmin=0
        FOR k=1 TO lastpal
        	dr=ABS(s[i+2]-pal(k,2))
        	dg=ABS(s[i+1]-pal(k,1))
        	db=ABS(s[i]-pal(k,0))
        	IF dr<ColorMargin AND dg<ColorMargin AND db<ColorMargin THEN EXIT FOR
        	distan=SQR(dr*dr+dg*dg+db*db)
        	IF distan<distamin THEN 'nearest
        		distamin=distan
        		kmin=k
        	ENDIF
        NEXT
        IF k>lastpal THEN
        	IF k<=255 THEN 
        		lastpal=k
        		pal(k,0)=s[i]:pal(k,1)=s[i+1]:pal(k,2)=s[i+2]
        		PALETTE k,s[i+2],s[i+1],s[i]
        	ELSE 'select nearest
            IF k>255 THEN mensaje= "Big palette"
        		k=kmin
        	ENDIF
        END IF
        ipal=k
      ELSE
     	  ipal=0
      END IF
      d[x] = ipal
      i+=4 ' next source pixel
    NEXT
    s+=spitch ' next src row
    d+=dpitch ' next dst row
  NEXT
  PRINT #1, "Palette colors:";lastpal
  IMAGEDESTROY imgSrc
  FUNCTION = imgDst
END FUNCTION
FBimage-lite.bi: Include valid for both fb dialects

Code: Select all

#ifndef __FBImage_bi__
#define __FBImage_bi__


#ifdef __FB_WIN32__
# libpath "lib/win"
#else
# libpath "lib/lin"
#endif

#ifndef __FB_64BIT__
# inclib "FBImage-32-static"
#else
# inclib "FBImage-64-static"
#endif


' Load BMP, PNG, JPG, TGA, DDS from file or memory as FBImage

' screenres 640,480,32 ' <--- RGBA
' var jpg = LoadRGBAFile("test_rgb.jpg")
' put (0,0),jpg,PSET
'
' var png = LoadRGBAFile("test_rgba.png")
' put (256,0),png,ALPHA

' var img = LoadRGBAFile("filenotfound.xxx")
' if img=0 then
'   print "error: loading test.pcx " & *GetLastResult()
' end if

' Save PNG
' var ok = SavePNGFile(img,"saved.png")

extern "C"

declare function LoadRGBAFile(byval filename as const zstring ptr) as any ptr

declare function LoadRGBAMemory(byval buffer as const any ptr, byval buffersize as long) as any ptr

declare function GetLastResult() as const zstring ptr

declare function SavePNGFile (byval img as any ptr, byval filename as const zstring ptr,byval saveAlpha as boolean=false) as boolean

end extern

' load (32bit) RGBA jpg image and convert it for 16 bit RGB mode
function Load16BitRGB(filename as const zstring ptr) as any ptr
  #define RGB16(_r,_g,_b) (((_r shr 3) shl 11) or ((_g shr 2) shl 5) or (_b shr 3))
  DIM AS ANY PTR imgSrc, imgDst 
  imgSrc = LoadRGBAFile(filename)
  if imgSrc=0 then return 0
  dim as integer w,h,spitch,dpitch
  dim as ubyte ptr s
  imageinfo imgSrc,w,h,,spitch,s
  imgDst = ImageCreate(w,h,0,16)
  dim as ushort ptr d
  imageinfo imgDst,,,,dpitch,d 
  dpitch shr= 1 ' pitch in bytes to pitch in pixels
  for y as integer =1 to h
    dim as integer i
    for x as integer =0 to w-1
      d[x] = RGB16(s[i],s[i+1],s[i+2]) 
      i+=4 ' next source pixel
    next
    s+=spitch ' next src row
    d+=dpitch ' next dst row
  next
  ImageDestroy imgSrc
  return imgDst
  #undef RGB16
end function

#endif ' __FBImage_bi__
moises1953
Posts: 3
Joined: Aug 25, 2017 9:33
Location: Santander - Spain

Re: Library FBImage for Windows/Linux 32/64-bit.

Post by moises1953 »

A maping algorithn like used in 16 bit version is also posible, using a predefined and sorted palette:

Define Palette:

Code: Select all

FUNCTION Create8BitPalette
	'.standard 16 colors VGA palette untouched (0-15)
	'.Map 255->6 colors for 6^3=216 new colors, spaced 255\5=51: 0,51,102,153,204,255 
	'.Final palette: 15 + 216 = 231 last palette color
	i=15 
	FOR r=0 TO 255 STEP 51
	  FOR g=0 TO 255 STEP 51
		FOR b=0 TO 255 STEP 51
		    i+=1
		    PALETTE i,r,g,b 
		NEXT
	  NEXT
	NEXT
  FUNCTION = i
END FUNCTION
Load file:

Code: Select all

FUNCTION Load8BitRGB(BYVAL filename as const zstring PTR, mensaje AS STRING) AS ANY PTR
  DIM AS ANY PTR imgSrc, imgDst  
  imgSrc = LoadRGBAFile(filename)
  mensaje=""
  IF imgSrc=0  THEN 
  	FUNCTION = 0
  	EXIT FUNCTION
  ENDIF
  DIM AS INTEGER w,h,spitch,dpitch 
  DIM AS UBYTE PTR s, d
  
  DIM AS LONG i,x,y,r,g,b,map
  DIM AS UBYTE ipal
  
  IMAGEINFO imgSrc,w,h,,spitch,s
  imgDst = IMAGECREATE(w,h)'8bpp
  IMAGEINFO imgDst,,,,dpitch,d 
  
  FOR y = 1 TO h
    i=0
    FOR x =0 to w-1
      IF s[i+2] OR s[i+1] OR s[i] THEN
      	r=s[i+2]
      	g=s[i+1]
      	b=s[i]
        map=(b+25)\51+((g+25)\51)*6+((r+25)\51)*36 ' map to sorted palette
        ipal=map+16
      ELSE
        ipal=0
      END IF
      d[x] = ipal
      i+=4 ' next source pixel
    NEXT
    s+=spitch ' next src row
    d+=dpitch ' next dst row
  NEXT

  IMAGEDESTROY imgSrc
  FUNCTION = imgDst
END FUNCTION
D.J.Peters
Posts: 8588
Joined: May 28, 2005 3:28
Contact:

Re: Library FBImage for Windows/Linux 32/64-bit.

Post by D.J.Peters »

I added my base64 memory en/decoders to FBimage so you can create include filed from images or post base64 encoded images on the forum.

or if you need base64 encoded images in your HTML web page
or in your JavaScript code
or HTML5 canvas
or as texture for WebGL
or for your pet :-)

Joshy
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Library FBImage for Windows/Linux 32/64-bit.

Post by lizard »

Works here on Mint 18 64 bit. Thanks for all Joshy!
Post Reply