What I'm more or less using with the static lib is something like this (cross platform).
Code: Select all
Enumeration
#TJPF_RGB
#TJPF_BGR
#TJPF_RGBX
#TJPF_BGRX
#TJPF_XBGR
#TJPF_XRGB
#TJPF_GRAY
#TJPF_RGBA
#TJPF_BGRA
EndEnumeration
Enumeration
#TJSAMP_444
#TJSAMP_422
#TJSAMP_420
EndEnumeration
#TJFLAG_BOTTOMUP = 2
#TJFLAG_FASTDCT = 2048
#TJFLAG_ACCURATEDCT = 4096
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
#LIBTURBO = "turbojpeg-static.lib"
CompilerCase #PB_OS_MacOS
#LIBTURBO = "libturbojpeg_osx.a"
CompilerCase #PB_OS_Linux
#LIBTURBO = "libturbojpeg_lin64.a"
CompilerEndSelect
ImportC #LIBTURBO
tjAlloc(bytes)
tjCompress2.l(handle, *srcBuf, width, pitch, height, pixelFormat, *p_jpegBuf, *jpegSize, jpegSubsamp, jpegQual, flags)
tjDecompress2.l(handle, *jpegBuf, jpegSize, *dstBuf, width, pitch, height, pixelFormat, flags)
tjDecompressHeader3.l(handle, *jpegBuf, jpegSize, *width, *height, *jpegSubsamp, *jpegColorspace)
tjFree(*buffer)
tjGetErrorStr()
tjInitCompress()
tjInitDecompress()
EndImport
Global tjC, tjD
tjC = tjInitCompress()
tjD = tjInitDecompress()
; TJPF from PB PixelFormat
Procedure TJPF_(Format)
If Format & #PB_PixelFormat_32Bits_RGB
ProcedureReturn #TJPF_RGBA
ElseIf Format & #PB_PixelFormat_24Bits_RGB
ProcedureReturn #TJPF_RGB
ElseIf Format & #PB_PixelFormat_32Bits_BGR
ProcedureReturn #TJPF_BGRA
Else
ProcedureReturn #TJPF_BGR
EndIf
EndProcedure
Procedure.i CatchImage_(Image, *Memory, Size)
Protected.i result, w, h, subs, cs, r, pf, flags
If PeekL(*Memory) & $ffffff <> $ffd8ff
ProcedureReturn CatchImage(Image, *Memory, Size)
ElseIf tjDecompressHeader3(tjD, *Memory, Size, @w, @h, @subs, @cs) = 0 And w And h
result = CreateImage(Image, w, h, 24)
If result
If Image = #PB_Any : Image = result : EndIf
StartDrawing(ImageOutput(Image))
pf = DrawingBufferPixelFormat() : flags = #TJFLAG_ACCURATEDCT
If pf & #PB_PixelFormat_ReversedY : flags | #TJFLAG_BOTTOMUP : EndIf
r = tjDecompress2(tjD, *Memory, Size, DrawingBuffer(), w, DrawingBufferPitch(), h, TJPF_(pf), flags)
StopDrawing()
If r
FreeImage(Image) : result = 0
EndIf
EndIf
EndIf
ProcedureReturn result
EndProcedure
Procedure LoadJPEG(Image, FileName.s)
Protected.i result, fhandle, size, w, h, subs, cs, r, pf, flags, sig.l, *buffer.Long
fhandle = ReadFile(#PB_Any, FileName)
If fhandle
size = Lof(fhandle)
If size > 96
sig = ReadLong(fhandle)
If sig & $ffffff = $ffd8ff; possible jpeg
*buffer = AllocateMemory(size, #PB_Memory_NoClear)
If *buffer
*buffer\l = sig
ReadData(fhandle, *buffer + 4, size - 4)
; jpeg decompression
If tjDecompressHeader3(tjD, *buffer, size, @w, @h, @subs, @cs) = 0 And w And h
; create image
result = CreateImage(Image, w, h, 24)
If result
If Image = #PB_Any : Image = result : EndIf
StartDrawing(ImageOutput(Image))
pf = DrawingBufferPixelFormat() : flags = #TJFLAG_ACCURATEDCT
If pf & #PB_PixelFormat_ReversedY : flags | #TJFLAG_BOTTOMUP : EndIf
r = tjDecompress2(tjD, *buffer, size, DrawingBuffer(), w, DrawingBufferPitch(), h, TJPF_(pf), flags)
StopDrawing()
If r
FreeImage(Image) : result = 0
EndIf
EndIf
EndIf
FreeMemory(*buffer)
EndIf
EndIf
EndIf
CloseFile(fhandle)
EndIf
ProcedureReturn result
EndProcedure
Procedure SaveJPEG(Image, FileName.s, quality = 95)
Protected.i result, pf, flags, r, size, fhandle, *buffer
If IsImage(Image) And StartDrawing(ImageOutput(Image))
pf = DrawingBufferPixelFormat()
If quality < 1 : quality = 1 : ElseIf quality > 100 : quality = 100 : EndIf
If quality > 80 : flags = #TJFLAG_ACCURATEDCT : Else : flags = #TJFLAG_FASTDCT : EndIf
If pf & #PB_PixelFormat_ReversedY : flags | #TJFLAG_BOTTOMUP : EndIf
r = tjCompress2(tjC, DrawingBuffer(), OutputWidth(), DrawingBufferPitch(), OutputHeight(), TJPF_(pf), @*buffer, @size, #TJSAMP_444, quality, flags)
StopDrawing()
If r = 0
fhandle = CreateFile(#PB_Any, FileName)
If fhandle
If WriteData(fhandle, *buffer, size) = size
result = #True
EndIf
CloseFile(fhandle)
EndIf
EndIf
tjFree(*buffer)
EndIf
ProcedureReturn result
EndProcedure