Segmentation Violation signal displaying JPEGs

Windows specific questions.
Post Reply
azbluevw
Posts: 16
Joined: Dec 31, 2011 21:54

Segmentation Violation signal displaying JPEGs

Post by azbluevw »

I'm trying to run the test program for displaying JPEGs; I've modified it slightly (I hardcoded the file name rather than getting it from the commandline and, after getting an error added some prints and sleeps and error checking)

I'm getting error 12, "segmentation violation signal," which I have no idea what that means. I ran it in a debugger and got "Exception Access Violation at ADR (dec/hex) 24/18"

In both cases it seems to be at:

jpeg_create_decompress( @jinfo )


which I think is a call to the jpeg DLL... which I doubt has any bugs?

I'm running FB 1.09.0 under Windows XP in a virtual machine (which I don't think should matter?)

Is there anyone who can shed some light on what exactly a "segmentation violation signal" is or what an ADR is? I'm pretty lost here; I have no idea what to do next.

Here is the code:

Code: Select all

on error goto jpegErr
#include "fbgfx.bi"
#include "jpeglib.bi"

const SCR_W = 640
const SCR_H = 480
const SCR_BPP = 32


dim e as long
dim el as long

declare function imageread_jpg( byval filename as zstring ptr, _
                                byval bpp as integer ) as FB.IMAGE ptr

	print "setting screen"
    sleep
    
    screenres SCR_W, SCR_H, SCR_BPP
	print "gfx screen"
    sleep
    
	dim as FB.IMAGE ptr img = imageread_jpg( "jump.JPG", SCR_BPP )
	
    print "reading file"
    sleep
    
	if( img = 0 ) then
		end 1
	end if
	
	put (0,0), img, pset
	
	sleep
	
	imagedestroy( img )
end	

'':::::
function imageread_jpg( byval filename as zstring ptr, _
                        byval bpp as integer ) as FB.IMAGE ptr
	print "in read"
    dim as FILE ptr fp = fopen( filename, "rb" )
	if( fp = NULL ) then
		return NULL
	end if
	print "opened file"
    sleep
	dim jinfo as jpeg_decompress_struct
	print "jinfo struc"
    sleep
	
    jpeg_create_decompress( @jinfo )
	print "decompressed"
    sleep
	
	dim jerr as jpeg_error_mgr
	jinfo.err = jpeg_std_error( @jerr )
    print "jinfo, err"
    sleep
	jpeg_stdio_src( @jinfo, fp )
	jpeg_read_header( @jinfo, TRUE )
	jpeg_start_decompress( @jinfo )
	print "about to dim row"
    sleep
	dim row as JSAMPARRAY
	row = jinfo.mem->alloc_sarray( cast( j_common_ptr, @jinfo ), _
	                               JPOOL_IMAGE, _
	                               jinfo.output_width * jinfo.output_components, _
	                               1 )
	
	dim img as FB.IMAGE ptr
	img = imagecreate( jinfo.output_width, jinfo.output_height )
	
	dim as byte ptr dst = cast( byte ptr, img + 1 )
	print "goint to while loop"
    sleep
	do while jinfo.output_scanline < jinfo.output_height
		jpeg_read_scanlines( @jinfo, row, 1 )
		
		'' !!!FIXME!!! no grayscale support
		imageconvertrow( *row, 24, dst, bpp, jinfo.output_width )
		
		dst += img->pitch
	loop
	print "done loop"
    sleep
	jinfo.mem->free_pool( cast( j_common_ptr, @jinfo ), JPOOL_IMAGE )
	
	jpeg_finish_decompress( @jinfo )
    print "almost done"
    sleep
	jpeg_destroy_decompress( @jinfo )
	fclose( fp )
	print "closed file"
    sleep
    
	function = img
	
end function

jpegErr:
e = err
el = erl
print "error #";e; " on line" ;el
sleep
angros47
Posts: 2326
Joined: Jun 21, 2005 19:04

Re: Segmentation Violation signal displaying JPEGs

Post by angros47 »

Likely you are using a recent version of libjpeg, that uses a larger struct for jpeg_decompress_struct. As result, FreeBasic doesn't allocate enough memory for it, and the library crashes for that reason
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: Segmentation Violation signal displaying JPEGs

Post by UEZ »

As you have posted in Windows, you can use GDIPlus to decode the JPG file without any ext. lib dependencies - just Windows built-in.

Example:

Code: Select all

'Example code by UEZ build 2023-08-11
#include "fbgfx.bi"
#Ifdef __Fb_64bit__
   #Inclib "gdiplus"
   #Include Once "win/gdiplus-c.bi"
#Else
   #Include Once "win/gdiplus.bi"
   Using Gdiplus
#Endif

Dim Shared gdipToken As ULONG_PTR
Dim Shared GDIp As GdiplusStartupInput 


Function _GDIPlus_Startup() As Bool
	GDIp.GdiplusVersion = 1
	If GdiplusStartup(@gdipToken, @GDIp, NULL) <> 0 Then
		Error 1
		Return False
	Endif
	Return True
End Function

Sub _GDIPlus_Shutdown()
	GdiplusShutdown(gdipToken)
End Sub

Dim As Any Ptr hImage, hCanvas
Dim As Single iW_Img, iH_Img
Dim As String sFile2Load = "YOUR FILE TO LOAD" 'GDIPlus supported formats: JPG, PNG, GIF, BMP, TIFF

_GDIPlus_Startup()	'init GDIPlus

GdipLoadImageFromFile(sFile2Load, @hImage)
GdipGetImageDimension(hImage, @iW_Img, @iH_Img)

ScreenControl FB.SET_DRIVER_NAME, "GDI"
ScreenRes iW_Img, iH_Img, 32

WindowTitle "Load and Display GDIPlus Supported Images Example by UEZ"

Dim as HWND hHWND
ScreenControl(FB.GET_WINDOW_HANDLE, Cast(Integer, hHWND))

GdipCreateFromHWND(hHWND, @hCanvas)
	
Do
    GdipDrawImageRect(hCanvas, hImage, 0, 0, iW_Img, iH_Img)
	Sleep(20)
Loop Until Len(Inkey())

'release GDIPlus resources
GdipDisposeImage(hImage)
GdipDeleteGraphics(hCanvas)
_GDIPlus_Shutdown()
azbluevw
Posts: 16
Joined: Dec 31, 2011 21:54

Re: Segmentation Violation signal displaying JPEGs

Post by azbluevw »

Thank for your responses guys, sorry for the long delay in responding!

UEZ, your solution worked.

angros47, I downloaded the oldest version of jpeg62.dll I could find, then I re-read your post and realized that's not what you said. This sounds like a stupid question, but where do I find older versions of the library?
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Segmentation Violation signal displaying JPEGs

Post by srvaldez »

@azbluevw
you need jpeg version 9 or better, not some old version, here's version 9e https://u.pcloud.link/publink/show?code ... 56Wf5L449y
Post Reply