Post by tsh73 on Dec 9, 2022 13:05:36 GMT
function getJpegProps$(fileName$)
Reads file in binary, returns size in pixels, number of color components so you can tell greyscale ones
I found non-API way on stackOverflow and translated it to native JB.
Here it is (put in DATA section some real Jpegs, I just saved some from Paint):
Reads file in binary, returns size in pixels, number of color components so you can tell greyscale ones
I found non-API way on stackOverflow and translated it to native JB.
Here it is (put in DATA section some real Jpegs, I just saved some from Paint):
1000x10.jpg 1000 x 10 pixels
10x1000.jpg 10 x 1000 pixels
10x20.bmp Likely not a Jpeg file!
10x20.jpg 10 x 20 pixels
40000x10.jpg 40000 x 10 pixels
photo355x474.jpg 355 x 474 pixels
photo355x474bw.jpg 355 x 474 pixels, grayscale
'reading jpeg size from jpeg file, without library
'translation of C code from
' https://stackoverflow.com/questions/2517854/getting-image-size-of-jpeg-from-its-binary
data "1000x10.jpg"
data "10x1000.jpg"
data "10x20.bmp"
data "10x20.jpg"
data "40000x10.jpg"
data "photo355x474.jpg"
data "photo355x474bw.jpg"
data "XYZZY"
read fileName$
do
print fileName$,
res$=getJpegProps$(fileName$)
'print res$
if word$(res$, 4)="0" then
print "Likely not a Jpeg file!"
else
print word$(res$, 1) ;" x "; word$(res$, 2) ; " pixels";
if word$(res$, 4) ="1" then print ", grayscale";
print 'finally break that line
end if
read fileName$
loop until fileName$="XYZZY"
'----------------------------------------------------
function getJpegProps$(fileName$)
' ret$= w;" "; h;" "; bpc;" ";cps
' so word$(getJpegProps$(fileName$),1) , width in pixels, (,2) height, (,3) is bits per channel,
' (,4) is color components (3 is color, 1 is grayscale)
open fileName$ for binary as #1
dataLength = lof(#1)
data$=input$(#1, dataLength)
close #1
getJpegProps$ ="0 0 0 0"
if mid$(data$,7,4)<>"JFIF" then exit function 'likely not a Jpeg file!
off = 1 'BASIC numbers characters from 1
'some constants
Oxff=hexdec("ff")
Oxd0=hexdec("d0")
Oxd7=hexdec("d7")
Oxd8=hexdec("d8")
Oxd9=hexdec("d9")
Ox01=hexdec("01")
Oxc0=hexdec("c0")
while (off<=dataLength)
while(asc(mid$(data$,off,1))=Oxff)
off=off+1
wend
mrkr = asc(mid$(data$,off,1)):off=off+1
if(mrkr=Oxd8) then [continue] '// SOI
if(mrkr=Oxd9) then exit while '// EOI
if(Oxd0<=mrkr AND mrkr<=Oxd7) then [continue]
if(mrkr=Ox01) then [continue] '// TEM
secLen = asc(mid$(data$,off,1))*256 + asc(mid$(data$,off+1,1))
off=off+2
if(mrkr=Oxc0) then
bpc = asc(mid$(data$,off,1)) '// precission (bits per channel)
h = asc(mid$(data$,off+1,1))*256 + asc(mid$(data$,off+2,1))
w = asc(mid$(data$,off+3,1))*256 + asc(mid$(data$,off+4,1))
cps = asc(mid$(data$,off+5,1)) '// number of color components
'ret$=bpc;" "; h;" "; w;" "; cps
ret$= w;" "; h;" "; bpc;" ";cps
'print ret$
getJpegProps$ = ret$
'end
exit function
end if
off=off+secLen-2
[continue]
wend
end function
'sorce C code
' https://stackoverflow.com/questions/2517854/getting-image-size-of-jpeg-from-its-binary
' function jpegProps(data$) { // data$ is an array of bytes
' var off = 0;
' while(off<data$.length) {
' while(data$[off]==0xff) off++;
' var mrkr = data$[off]; off++;
' if(mrkr==0xd8) continue; // SOI
' if(mrkr==0xd9) break; // EOI
' if(0xd0<=mrkr && mrkr<=0xd7) continue;
' if(mrkr==0x01) continue; // TEM
' var len = (data$[off]<<8) | data$[off+1]; off+=2;
' if(mrkr==0xc0) return {
' bpc : data$[off], // precission (bits per channel)
' h : (data$[off+1]<<8) | data$[off+2],
' w : (data$[off+3]<<8) | data$[off+4],
' cps : data$[off+5] // number of color components
' }
' off+=len-2;
' }
' }