Load JPG, JPEG, JFIF, MJPG from file or memory.

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
creek23
Posts: 260
Joined: Sep 09, 2007 1:57
Location: Philippines
Contact:

Postby creek23 » Apr 04, 2010 17:46

I wonder if this could be of any help for my app. How does it differ with jpeg4fb and other already existing JPG loader for FB?
roook_ph
Posts: 402
Joined: Apr 01, 2006 20:50
Location: philippines
Contact:

Postby roook_ph » Apr 06, 2010 11:23

Nice work joshy do you translate it manually or using swig etc.. ? Cause I know a lot of c codes online --- Dr. dobbs
antarman
Posts: 66
Joined: Jun 12, 2006 9:27
Location: Russia, Ural, Chelyabinsk
Contact:

Postby antarman » Apr 22, 2010 15:53

may be i found bug with color...
change it:

Code: Select all

#define float2fixed(x) (Int((x) * 65536 + 0.5))
Sub YCbCr_to_RGB_row(ou  As UByte Ptr, _
                     y   As UByte Ptr, _
                     pcb As UByte Ptr, _
                     pcr As UByte Ptr, _
                     count As Integer, _
                     psize As Integer)
  Dim As Integer i

  For i=0 To count-1
    Dim As Integer y_fixed = (y[i] Shl 15) + 32768 ' rounding
    Dim As Integer r,g,b
    Dim As Integer cr = pcr[i] - 128
    Dim As Integer cb = pcb[i] - 128
    r = y_fixed + cr*float2fixed(1.40200f)
    g = y_fixed - cr*float2fixed(0.71414f) - cb*float2fixed(0.34414f)
    b = y_fixed                            + cb*float2fixed(1.77200f)
    r Shr= 15
    g Shr= 15
    b Shr= 15

    ou[0]= IIf(r<0,0,IIf(r>255,255,r))
    ou[1]= IIf(g<0,0,IIf(g>255,255,g))
    ou[2]= IIf(b<0,0,IIf(b>255,255,b))
    ou[3]= 255
    ou+=psize
   Next
End Sub
D.J.Peters
Posts: 7825
Joined: May 28, 2005 3:28

Postby D.J.Peters » Apr 23, 2010 15:05

hello antarman
do you compared your changes with paint or gimp?

Joshy

fb_versus_paint.png
Image
Last edited by D.J.Peters on Sep 25, 2017 16:45, edited 1 time in total.
antarman
Posts: 66
Joined: Jun 12, 2006 9:27
Location: Russia, Ural, Chelyabinsk
Contact:

Postby antarman » Apr 26, 2010 9:08

please test it

Code: Select all


''' i change only this line:
#define float2fixed(x) (cint(x * 100000 + 0.1))

''' and restore this part of code:
Sub YCbCr_to_RGB_row(ou  As Ubyte Ptr, _
                     y   As Ubyte Ptr, _
                     pcb As Ubyte Ptr, _
                     pcr As Ubyte Ptr, _
                     count As Integer, _
                     psize As Integer)
  Dim As Integer i
  For i=0 To count-1
    Dim As Integer y_fixed = (y[i] Shl 16) + 32768 ' rounding
    Dim As Integer r,g,b
    Dim As Integer cr = pcr[i] - 128
    Dim As Integer cb = pcb[i] - 128
    r = y_fixed + cr*float2fixed(1.40200f)
    g = y_fixed - cr*float2fixed(0.71414f) - cb*float2fixed(0.34414f)
    b = y_fixed                            + cb*float2fixed(1.77200f)
    r Shr= 16
    g Shr= 16
    b Shr= 16
    ou[0]= iif(r<0,0,iif(r>255,255,r))
    ou[1]= iif(g<0,0,iif(g>255,255,g))
    ou[2]= iif(b<0,0,iif(b>255,255,b))
    ou[3]= 255
    ou+=psize
   Next
End Sub
FXG861
Posts: 86
Joined: Feb 01, 2009 17:10
Location: Canada
Contact:

Re: Load JPG, JPEG, JFIF, MJPG from file or memory.

Postby FXG861 » Aug 01, 2012 3:10

Hi,

I don't see what is the command to load Jpeg from memory ...

Is there a way to make this library compatible with these Macro ?

http://www.freebasic.net/forum/viewtopic.php?f=7&t=8504

I would like to display a Jpeg image loaded directly into memory from those macro
Pritchard
Posts: 5492
Joined: Sep 12, 2005 20:06
Location: Ohio, USA

Re: Load JPG, JPEG, JFIF, MJPG from file or memory.

Postby Pritchard » Aug 06, 2012 2:21

@FXG861:

Example usage is included at the bottom of the code posted by D.J.Peters

Code: Select all

' simple test
dim as integer   w,h,BytesPerPixel
dim as ubyte ptr pImageBuffer

pImageBuffer=LoadJPG("test320x240.jpg",w,h,BytesPerPixel)

if pImageBuffer then
  screenres w,h,BytesPerPixel*8
  dim as ubyte ptr pRGB=pImageBuffer
  screenlock
  for y as integer=0 to h-1
    for x as integer=0 to w-1
      pset(x,y),rgb(pRGB[0],pRGB[1],pRGB[2])
      pRGB+=BytesPerPixel
    next
  next
  screenunlock
  if pImageBuffer then deallocate(pImageBuffer)
end if
sleep


As for the macros you linked to, I've never used them and have never had a need to perform that functionality in FreeBASIC.
FXG861
Posts: 86
Joined: Feb 01, 2009 17:10
Location: Canada
Contact:

Re: Load JPG, JPEG, JFIF, MJPG from file or memory.

Postby FXG861 » Aug 06, 2012 12:52

Thank you for the reply Pritchard.

Your example is for an image that i load from a file and i already found that command no problem.

I needed a command to access the file that is include in the EXE via IncFile

I was able to resolve my problem with: "jpeg4fb"
frisian
Posts: 249
Joined: Oct 08, 2009 17:25

Re: Load JPG, JPEG, JFIF, MJPG from file or memory.

Postby frisian » Aug 15, 2012 17:51

1.
Since FXG861 posted his question and moved your posting of this program to the top of the topic list, I was intrigued by the fact it did not display ed the correct colors. I think I have found the problem. My knowledge of C is what you can call basic but I think that the part

@z->dequant(z->img_comp(n).tq))

should be

@z->dequant(z->img_comp(n).tq*64))

I would appreciate it if someone could tell me if this is correct.

2.
#define f2f(x) int( (((x) * 4096 + 0.5)) )
#define float2fixed(x) (int((x) * 65536 + 0.5))

There are two defines that use Int() to convert floats to integers and store the precalculated numbers as floats and as a result use FPU instructions to do the calculations in the program. Better replace the Int() by Cint().

Code: Select all

#define float2fixed(x) (int((x) * 65536 + 0.5))

##        r = y_fixed + cr*float2fixed(1.40200f) [Macro Expansion: (int((1.40200f) * 65536 + 0.5)) ]
   fild dword ptr [ebp-12]
   fild dword ptr [ebp-28]
   fmul qword ptr [_Lt_056E]
   fxch st(1)
   faddp
   fistp dword ptr [ebp-16]
.stabn 68,0,972,.Lt_057E-_YCBCR_TO_RGB_ROW@24
.Lt_057F:
##        g = y_fixed - cr*float2fixed(0.71414f) [Macro Expansion: (int((0.71414f) * 65536 + 0.5)) ]  - cb*float2fixed(0.34414f) [Macro Expansion: (int((0.34414f) * 65536 + 0.5)) ]
   fild dword ptr [ebp-12]
   fild dword ptr [ebp-28]
   fmul qword ptr [_Lt_056F]
   fxch st(1)
   fsubrp
   fild dword ptr [ebp-32]
   fmul qword ptr [_Lt_0570]
   fxch st(1)
   fsubrp
   fistp dword ptr [ebp-20]
.stabn 68,0,973,.Lt_057F-_YCBCR_TO_RGB_ROW@24
.Lt_0580:
##        b = y_fixed                            + cb*float2fixed(1.77200f) [Macro Expansion: (int((1.77200f) * 65536 + 0.5)) ]
   fild dword ptr [ebp-12]
   fild dword ptr [ebp-32]
   fmul qword ptr [_Lt_0571]
   fxch st(1)
   faddp
   fistp dword ptr [ebp-24]
   
_Lt_056E:   .double   91881

_Lt_056F:   .double   46802

_Lt_0570:   .double   22554

_Lt_0571:   .double   116130
==========================================================================================================================
int changed to Cint
#define float2fixed(x) (Cint((x) * 65536 + 0.5))

##        r = y_fixed + cr*float2fixed(1.40200f) [Macro Expansion: (cint((1.40200f) * 65536 + 0.5)) ]
   mov eax, dword ptr [ebp-28]
   imul eax, 91882
   add eax, dword ptr [ebp-12]
   mov dword ptr [ebp-16], eax
.stabn 68,0,972,.Lt_056E-_YCBCR_TO_RGB_ROW@24
.Lt_056F:
##        g = y_fixed - cr*float2fixed(0.71414f) [Macro Expansion: (cint((0.71414f) * 65536 + 0.5)) ]  - cb*float2fixed(0.34414f) [Macro Expansion: (cint((0.34414f) * 65536 + 0.5)) ]
   mov eax, dword ptr [ebp-28]
   imul eax, 46802
   mov ebx, dword ptr [ebp-12]
   sub ebx, eax
   mov eax, dword ptr [ebp-32]
   imul eax, 22554
   sub ebx, eax
   mov dword ptr [ebp-20], ebx
.stabn 68,0,973,.Lt_056F-_YCBCR_TO_RGB_ROW@24
.Lt_0570:
##        b = y_fixed                            + cb*float2fixed(1.77200f) [Macro Expansion: (cint((1.77200f) * 65536 + 0.5)) ]
   mov ebx, dword ptr [ebp-32]
   imul ebx, 116130
   add ebx, dword ptr [ebp-12]
   mov dword ptr [ebp-24], ebx


[28-8-2012] Found something after looking at the assembler listing.

In the function load_jpeg_image in the following lines the '/' can be replaced by a '\' (integer division).

r->hs = z->img_h_max / z->img_comp(k).h
r->vs = z->img_v_max / z->img_comp(k).v
r->w_lores = (z->s.w + r->hs-1) / r->hs
Makoto WATANABE
Posts: 118
Joined: Apr 10, 2010 11:41
Location: Japan
Contact:

Re: Load JPG, JPEG, JFIF, MJPG from file or memory.

Postby Makoto WATANABE » Jun 28, 2019 12:08

I tried to compile "fbJPEG.bas" in the following environment.
Windows 10
FreeBASIC Compiler - Version 1.06.0 (02-17-2019), built for win32 (32bit)

The compilation resulted in the following errors:
fbJPEG.bas(493) error 58: Type mismatch, at parameter 2 (aData) of DECODE_BLOCK() in 'if decode_block(z, aData(0), _'
fbJPEG.bas(501) error 58: Type mismatch, at parameter 3 (aData) of IDCT_BLOCK() in 'aData(0), _'
fbJPEG.bas(529) error 58: Type mismatch, at parameter 2 (aData) of DECODE_BLOCK() in 'if decode_block(z, aData(0), _'
fbJPEG.bas(538) error 58: Type mismatch, at parameter 3 (aData) of IDCT_BLOCK() in 'z->img_comp(n).w2, aData(0), @z->dequant(z->img_comp(n).tq))'

Please teach me how to fix it.
srvaldez
Posts: 2108
Joined: Sep 25, 2005 21:54

Re: Load JPG, JPEG, JFIF, MJPG from file or memory.

Postby srvaldez » Jun 28, 2019 12:26

hello Makoto WATANABE
in the lines where the error occurs, replace aData(0) with aData(), hope that helps
also, make sure that line 1209 has a valid file in pImageBuffer=LoadJPG("test320x240.jpg",w,h,BytesPerPixel)
Makoto WATANABE
Posts: 118
Joined: Apr 10, 2010 11:41
Location: Japan
Contact:

Re: Load JPG, JPEG, JFIF, MJPG from file or memory.

Postby Makoto WATANABE » Jun 29, 2019 13:36

Dear srvaldez;

Thanks for your quick reply.

> replace aData(0) with aData()

It compiled successfully without any errors.

> a valid file in pImageBuffer=LoadJPG("test320x240.jpg",w,h,BytesPerPixel)

Surely! Thank you for your polite advice.
Thanks to you, the JPG image has been displayed.

P.S.
By the way, on my Windows 10 PCs, the graphic display programs end after displaying a white window for a while.
On Windows 8.1 PC, those graphics are displayed correctly.
I hope this improves.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: MSN [Bot] and 16 guests