Huff0:
Huff1:
Huff2:
DATA 5,&H14,&H32,&H81,&H91,&HA1 DATA 5,&H08,&H23,&H42,&HB1,&HC1 DATA 4,&H15,&H52,&HD1,&HF0 DATA 4,&H24,&H33,&H62,&H72 DATA 125,&H09,&H0A,&H16,&H17,&H18,&H19,&H1A,&H25,&H26,&H27,&H28,&H29,&H2A,&H34,&H35,&H36 DATA &H37,&H38,&H39,&H3A,&H43,&H44,&H45,&H46,&H47,&H48,&H49,&H4A,&H53,&H54,&H55,&H56 DATA &H57,&H58,&H59,&H5A,&H63,&H64,&H65,&H66,&H67,&H68,&H69,&H6A,&H73,&H74,&H75,&H76 DATA &H77,&H78,&H79,&H7A,&H83,&H84,&H85,&H86,&H87,&H88,&H89,&H8A,&H92,&H93,&H94,&H95 DATA &H96,&H97,&H98,&H99,&H9A,&HA2,&HA3,&HA4,&HA5,&HA6,&HA7,&HA8,&HA9,&HAA,&HB2,&HB3 DATA &HB4,&HB5,&HB6,&HB7,&HB8,&HB9,&HBA,&HC2,&HC3,&HC4,&HC5,&HC6,&HC7,&HC8,&HC9,&HCA DATA &HD2,&HD3,&HD4,&HD5,&HD6,&HD7,&HD8,&HD9,&HDA,&HE1,&HE2,&HE3,&HE4,&HE5,&HE6,&HE7 DATA &HE8,&HE9,&HEA,&HF1,&HF2,&HF3,&HF4,&HF5,&HF6,&HF7,&HF8,&HF9,&HFA
Huff3:
DATA 4,&H13,&H22,&H32,&H81 DATA 7,8,&H14,&H42,&H91,&HA1,&HB1,&HC1 DATA 5,9,&H23,&H33,&H52,&HF0 DATA 4,&H15,&H62,&H72,&HD1 DATA 4,&HA,&H16,&H24,&H34 DATA 119,&H17,&H18,&H19,&H1A,&H26,&H27,&H28,&H29,&H2A,&H35,&H36,&H37,&H38,&H39,&H3A,&H43 DATA &H44,&H45,&H46,&H47,&H48,&H49,&H4A,&H53,&H54,&H55,&H56,&H57,&H58,&H59,&H5A,&H63 DATA &H64,&H65,&H66,&H67,&H68,&H69,&H6A,&H73,&H74,&H75,&H76,&H77,&H78,&H79,&H7A,&H82 DATA &H83,&H84,&H85,&H86,&H87,&H88,&H89,&H8A,&H92,&H93,&H94,&H95,&H96,&H97,&H98,&H99 DATA &H9A,&HA2,&HA3,&HA4,&HA5,&HA6,&HA7,&HA8,&HA9,&HAA,&HB2,&HB3,&HB4,&HB5,&HB6,&HB7 DATA &HB8,&HB9,&HBA,&HC2,&HC3,&HC4,&HC5,&HC6,&HC7,&HC8,&HC9,&HCA,&HD2,&HD3,&HD4,&HD5 DATA &HD6,&HD7,&HD8,&HD9,&HDA,&HE2,&HE3,&HE4,&HE5,&HE6,&HE7,&HE8,&HE9,&HEA,&HF2,&HF3 DATA &HF4,&HF5,&HF6,&HF7,&HF8,&HF9,&HFA
StandardQT:
DATA 16,11,10,16,24,40,51,61 DATA 12,12,14,19,26,58,60,55 DATA 14,13,16,24,40,57,69,56 DATA 14,17,22,29,51,87,80,62 DATA 18,22,37,56,68,109,103,77 DATA 24,35,55,64,81,104,113,92 DATA 49,64,78,87,103,121,120,101 DATA 72,92,95,98,112,100,103,99
DATA 17,18,24,47,99,99,99,99 DATA 18,24,26,66,99,99,99,99 DATA 24,26,56,99,99,99,99,99 DATA 47,66,99,99,99,99,99,99 DATA 99,99,99,99,99,99,99,99 DATA 99,99,99,99,99,99,99,99 DATA 99,99,99,99,99,99,99,99 DATA 99,99,99,99,99,99,99,99
JPEG.Precalc
Sampling(0, 0) = 2 'Sampling factor (x then y) for luminance
Sampling(0, 1) = 2
Sampling(1, 0) = 1 'Sampling factor for "blue" chrominance
Sampling(1, 1) = 1
Sampling(2, 0) = 1 'Sampling factor for "red" chrominance
Sampling(2, 1) = 1
'***************************
' LIBRARY START BEFORE HERE
'***************************
'Delete file then open for binary
LINE (100, 100)-(200, 200), &HFFFFFF00, BF
SaveJPG "temp.JPG", 0, 100, 100, 500, 400
'***************************
' LIBRARY END AFTER HERE
'***************************
SUB SaveJPG
(file$
, image&
, startx
, starty
, finishx
, finishy
)
tempimage
= _NEWIMAGE(finishx
- startx
+ 1, finishy
- starty
+ 1, 32) _PUTIMAGE (0, 0)-(genx
, geny
), image&
, tempimage&
, (startx
, starty
)-(finishx
, finishy
) outfilename = file$
'Set quality tables
'The smaller the paramter, the higher the quality
'0.01 is 100% quality
JPEG.StandardQT 0.05, QT()
'Start image
JPEG.Begin 1, genx, geny, Sampling(), State, QT(), Huff()
FOR SuperX
= 0 TO genx
- 1 STEP 16:
REM vstupni velikost obrazku
'Output the luminance blocks
X! = OffX + BlockX + SuperX: REM- 63.5
Y!
= OffY
+ BlockY
+ SuperY:
REM - 63.5
B(OffX, OffY) = JPEG.Y(R, G, B)
JPEG.Block.
Output B
(), State
, QT
(), Huff
()
'Output the blue chrominance block
X! = OffX * 2 + SuperX
Y! = OffY * 2 + SuperY
B(OffX, OffY) = JPEG.Cb(R, G, B)
JPEG.Block.
Output B
(), State
, QT
(), Huff
()
'Output the red chrominance block
X! = OffX * 2 + SuperX
Y! = OffY * 2 + SuperY
B(OffX, OffY) = JPEG.Cr(R, G, B)
JPEG.Block.
Output B
(), State
, QT
(), Huff
()
JPEG.Finish State
C = JPEG.Category(AC)
X = RLE * 16 + C
JPEG.PutBinString Huff(X, 1, A, 0), Huff(X, 1, A, 1), State
JPEG.PutRightBinString AC + (AC < 0), C, State
State.FileNo = FileNo
JPEG.GenerateHuffmanTable Huff(), 0, 0
JPEG.GenerateHuffmanTable Huff(), 0, 1
JPEG.GenerateHuffmanTable Huff(), 1, 0
JPEG.GenerateHuffmanTable Huff(), 1, 1
State.YCount = Sampling(0, 0) * Sampling(0, 1)
State.CbCount = Sampling(1, 0) * Sampling(1, 1)
State.CrCount = Sampling(2, 0) * Sampling(2, 1)
State.YDC = 0
State.CbDC = 0
State.CrDC = 0
State.Position = 0
State.Leftover = 0
State.LeftoverBits = 0
'SOI
PutChar FileNo, 255
PutChar FileNo, 216
'APP0
PutChar FileNo, 255
PutChar FileNo, 224
JPEG.PutWord FileNo, 16
S$
= "JFIF" + CHR$(0):
PUT FileNo
, , S$
PutChar FileNo, 1
PutChar FileNo, 2
PutChar FileNo, 0
PutChar FileNo, 0
PutChar FileNo, 1
PutChar FileNo, 0
PutChar FileNo, 1
PutChar FileNo, 0
PutChar FileNo, 0
'DQT
PutChar FileNo, 255
PutChar FileNo, 219
JPEG.PutWord FileNo, 132
PutChar FileNo, 0
PutChar FileNo, QT(ZigZagX(I), ZigZagY(I), 0)
PutChar FileNo, 1
PutChar FileNo, QT(ZigZagX(I), ZigZagY(I), 1)
'DHT
PutChar FileNo, 255
PutChar FileNo, 196
T = 2 + 4 * (16 + 1)
T = T + 1
JPEG.PutWord FileNo, T
PutChar FileNo, 0
PutChar FileNo, X
PutChar FileNo, Y
PutChar FileNo, 1
PutChar FileNo, X
PutChar FileNo, Y
PutChar FileNo, 16
PutChar FileNo, X
PutChar FileNo, Y
PutChar FileNo, 17
PutChar FileNo, X
PutChar FileNo, Y
'SOF0
PutChar FileNo, 255
PutChar FileNo, 192
JPEG.PutWord FileNo, 8 + 9
PutChar FileNo, 8
JPEG.PutWord FileNo, H
JPEG.PutWord FileNo, W
PutChar FileNo, 3
PutChar FileNo, 1
PutChar FileNo, Sampling(0, 0) * 16 + Sampling(0, 1)
PutChar FileNo, 0
PutChar FileNo, 2
PutChar FileNo, Sampling(1, 0) * 16 + Sampling(1, 1)
PutChar FileNo, 1
PutChar FileNo, 3
PutChar FileNo, Sampling(2, 0) * 16 + Sampling(2, 1)
PutChar FileNo, 1
'SOS
PutChar FileNo, 255
PutChar FileNo, 218
JPEG.PutWord FileNo, 12
PutChar FileNo, 3
PutChar FileNo, 1
PutChar FileNo, &H0
PutChar FileNo, 2
PutChar FileNo, &H11
PutChar FileNo, 3
PutChar FileNo, &H11
PutChar FileNo, 0
PutChar FileNo, 63
PutChar FileNo, 0
DC = B(0) - LastDC
JPEG.DCHuff DC, Huff(), A, State
B(64) = -1
I = 1
C = 0
I = I + 1
C = C + 1
JPEG.PutBinString Huff(0, 1, A, 0), Huff(0, 1, A, 1), State
JPEG.PutBinString Huff(&HF0, 1, A, 0), Huff(&HF0, 1, A, 1), State
C = C - 16
JPEG.ACHuff C, B(I), Huff(), A, State
I = I + 1
State.Position = State.Position + 1
IF State.Position
> State.YCount
+ State.CbCount
+ State.CrCount
THEN State.Position
= 1 IF State.Position
<= State.YCount
THEN JPEG.Block.Transform B(), O(), QT(), 0
JPEG.Block.Huffman O(), State.YDC, Huff(), 0, State
State.YDC = O(0)
JPEG.Block.Transform B(), O(), QT(), 1
IF State.Position
<= State.YCount
+ State.CbCount
THEN JPEG.Block.Huffman O(), State.CbDC, Huff(), 1, State
State.CbDC = O(0)
JPEG.Block.Huffman O(), State.CrDC, Huff(), 1, State
State.CrDC = O(0)
T = 0
T = T + B(X, V) * Cosine(X, U)
B2(U, V) = T
T = 0
T = T + B2(U, Y) * Cosine(Y, V)
T = T / 4
B
(U
, V
) = CINT(T
/ QT
(U
, V
, A
))
O(U) = B(ZigZagX(U), ZigZagY(U))
T = T \ 2
I = I + 1
JPEG.Category = I
JPEG.Cb = -.1687 * R - .3313 * G + .5 * B
JPEG.Cr = .5 * R - .4187 * G - .0813 * B
C = JPEG.Category(DC)
JPEG.PutBinString Huff(C, 0, A, 0), Huff(C, 0, A, 1), State
JPEG.PutRightBinString DC + (DC < 0), C, State
SUB JPEG.Finish
(State
AS JPEGState
)
IF State.LeftoverBits
> 8 THEN JPEG.PutByte State.FileNo
, PEEK(VARPTR(State.Leftover
) + 1) State.LeftoverBits = State.LeftoverBits - 8
JPEG.PutByte State.FileNo
, PEEK(VARPTR(State.Leftover
) + 1) OR (Pow2
(8 - State.LeftoverBits
) - 1)
'EOF marker
PutChar State.FileNo, 255
PutChar State.FileNo, 217
S = -1
S = 0
S = S + Pow2(T)
Huff(Y, A, B, 1) = I
T = 16 - I
L = 1
Pow2(X) = L
L = L + L
Cosine
(X
, Y
) = COS((2 * X
+ 1) * Y
* .1963495)
X = 0: Y = 0
T = 0
Dir = 0
ZigZagX(T) = X
ZigZagY(T) = Y
T = T + 1
X = X + 1
Dir = 0
Y = Y + 1
Dir = 0
X = X - 1
Y = Y + 1
X = X + 1
Dir = 1
Y = Y + 1
Dir = 1
X = X + 1
Y = Y - 1
Temp = BS
State.Leftover
= State.Leftover
OR JPEG.Shift
(Temp
, State.LeftoverBits
) State.LeftoverBits = State.LeftoverBits + Length
IF State.LeftoverBits
>= 16 THEN JPEG.PutByte State.FileNo
, PEEK(VARPTR(State.Leftover
) + 1) JPEG.PutByte State.FileNo
, State.Leftover
AND 255 State.LeftoverBits = State.LeftoverBits - 16
State.Leftover = Temp
Temp
= (CLNG(BS
) AND Pow2
(Length
) - 1) * Pow2
(16 - Length
) JPEG.PutBinString
CINT(Temp
), Length
, State
JPEG.Shift = I
I = 0
JPEG.Shift = T \ Pow2(N)
T
= (T
AND (Pow2
(N
) - 1)) * Pow2
((16 - N
) AND 15)
QT(X, Y, I) = T * quality
IF QT
(X
, Y
, I
) = 0 THEN QT
(X
, Y
, I
) = 1
JPEG.Y = .299 * R + .587 * G + .114 * B - 128