|
!十進 BASIC で、JPG ファイルを作る。
!
!先に投稿した、デコーダーと対を成すものですが、此方はベースラインのみです。
!DCT 変換、ハフマンコード化 …JPG ファイルまでを、見えるプログラムで、実行。
!ハフマン・コードについては、標準テーブルを使わず、
!ランレングス・頻度の測定と、ハフマン・ツリーの作成を行い、それによる
!専用ハフマン・テーブルで、コード化します。(本来のハフマン・コード。)
DEBUG ON
!------------------
!JPG.BAS 09.10.13
!------------------
!テキスト・ウィンドウの、左上位置(x0,y0)と、幅(xw,yw)。
CALL SetWindowPos( WinHandle("TEXT" ),0, 15,172,500,520, 0)
SUB SetWindowPos( handle,C2, x0,y0,xw,yw, nFLG) !nFLG: 0=x0y0xwyw 1=x0y0 2=xwyw
ASSIGN "user32.dll","SetWindowPos"
END SUB
!----------------------------------------------------------
LET FL$="baseline.jpg" ! 削除すると、ダイアログ・ボックス入力。
SET ECHO "OFF"
ASK DIRECTORY s$
IF FL$>"" THEN PRINT "カレント DIR:"& s$ ELSE file getname FL$, "jpg"
PRINT "出力ファイル:"& FL$
IF FL$>"" THEN PRINT "上書き。又は作成されます。…"& "Ok?[Enter]"
IF FL$>"" THEN CHARACTER INPUT k$
IF FL$="" OR k$<>CHR$(13) THEN
PRINT "中止"
STOP
END IF
OPTION ARITHMETIC NATIVE
OPTION BASE 0
OPTION CHARACTER byte
SET TEXT background "OPAQUE"
ASK BITMAP SIZE bmx,bmy
SET WINDOW 0,bmx, bmy,0
!
DIM D8(1000,1000) !sample picture
DIM D2(1000,1000,2) !Y=D2(,,0) Cb=D2(,,1) Cr=D2(,,2)
DIM MH(2),MV(2) !MCU.Ybr.H()V()
DIM HDC(2),HAC(2) !hT.table selection
DIM QS(2),CoID(255) !qT.table selection
!
DIM U(63),V(63) !zigzag
DIM DQ(7,7,3) !DQT
DIM DH(16,7),DV(255,7) !DHT
DIM B(255+1,7),L(255,7) !huffman.code & length ( MAKE_H2 )
DIM B2(2) !Ybr D.C.成分 starting
DIM T(7,7),X(7),XO(7) !DDCT8X8
!
!---encorder
DIM SV(255,3) !ZFRE0 頻度SV( 座標)
DIM S_(255,3) !MAKE_DHT 頻度SV( 座標)--> 頻度S_( 降順No.) 座標DV( 降順No.)
DIM F_(255),Tr(16,255,3) !TREE3
!---lister
LET crlf$=CHR$(13)& CHR$(10)
DIM W_$(7)
LET W_$(0)="Y.DC"
LET W_$(1)="Y.AC"
LET W_$(2)="C.DC"
LET W_$(3)="C.AC"
!
SET VIEWPORT 0,0.4,0.6,1
CALL sample(DX$,DY$) !サンプル画像。
LET DX=VAL(DX$)
LET DY=VAL(DY$)
MAT D8=ZER(DX-1,DY-1)
SET VIEWPORT 0, 1, 0, 1
SET WINDOW 0,bmx, bmy,0
SET COLOR MODE "NATIVE"
ASK PIXEL ARRAY (0,0) D8
! MAT PLOT CELLS,IN 250,250; 450,450: D8 ! check
!
LET CMO=2 !CMO=0(mono.) CMO=2(color)
LET SD=1 !encoder 量子化テーブル調整 1/SD
CALL DQTINI !DQ(,,0)~DQ(,,1)~{zigzag U()V()},MH(),MV()
LET DU =CEIL(DX/(8*MH(0)))*8*MH(0) !Uwidth= (8X8)*2 bound by MCU size
LET DV_=CEIL(DY/(8*MV(0)))*8*MV(0) !Vwidth= (8X8)*1
MAT D2=ZER(DU-1,DV_-1,2) !Y=D2(,,0) Cb=D2(,,1) Cr=D2(,,2)
!
CALL YbrRGB ! Ybr D2()<--RGB D8()
LET MHT=1 ! flag. uncondition making huff.table
CALL DDCT8X8 ! D2() -->DCT -->Quantization
CALL ZZRL0 ! encoder
!---
PRINT "-------------------------"
PRINT "Encoder huffman Code"
FOR J=0 TO CMO+1
CALL list_HT(W_$(J),J) ! value Sort !H2.LST
NEXT J
beep
PRINT "終了"
!---------
SUB YbrRGB
!--------- JPG 色空間 -------------------------
! | Y | | 0.2990 +0.5870 +0.1140 | | R |
! |B-Y| = |-0.1687 -0.3313 +0.5000 | | G |
! |R-Y| | 0.5000 -0.4187 -0.0813 | | B |
!
! | R | | 1 0 +1.40200 | | Y |
! | G | = | 1 -0.34414 -0.71414 | |B-Y|
! | B | | 1 +1.77200 0 | |R-Y|
!----------------------------------------------
FOR V0=0 TO DY-1
FOR U0=0 TO DX-1
LET w1= MOD(D8(U0,V0),256) !R
LET w2=MOD(IP(D8(U0,V0)/256),256) !G
LET w3= IP(D8(U0,V0)/65536) !B
LET D2(U0,V0,0)= 0.2990*w1+0.5870*w2+0.1140*w3 !Y
LET D2(U0,V0,1)=-0.1687*w1-0.3313*w2+0.5000*w3 !Cb
LET D2(U0,V0,2)= 0.5000*w1-0.4187*w2-0.0813*w3 !Cr
NEXT U0
NEXT V0
END SUB
!-----------
SUB DQTINI
RESTORE
!---DQT quantization
FOR j=0 TO 1
FOR V_=0 TO 7
FOR U_=0 TO 7
READ W
LET DQ(U_,V_,j)=CEIL(W/SD) ! inhibit 0
NEXT U_
NEXT V_
NEXT j
!---zigzag-U()V()
FOR V_=0 TO 7
FOR U_=0 TO 7
READ i
LET U(i)=U_
LET V(i)=V_
NEXT U_
NEXT V_
!---HT selection
MAT READ HDC !Y Cb Cr
MAT READ HAC !Y Cb Cr
!---QT selection
MAT READ QS !Y Cb Cr
!---MCU size
MAT MH=CON !Y Cb Cr =1
MAT MV=CON !Y Cb Cr =1
IF 0< CMO THEN
LET MH(0)=2 !Y
LET MV(0)=2 !Y
END IF
END SUB
!---quantization table
!輝度( SMPTE 370M ).Y
!*DQTY
DATA 32, 16, 17, 18, 18, 19, 42, 44
DATA 16, 17, 18, 18, 19, 38, 43, 45
DATA 17, 18, 19, 19, 40, 41, 45, 48
DATA 18, 18, 19, 40, 41, 42, 46, 49
DATA 18, 19, 40, 41, 42, 43, 48,101
DATA 19, 38, 41, 42, 43, 44, 98,104
DATA 42, 43, 45, 46, 48, 98,109,116
DATA 44, 45, 48, 49,101,104,116,123
!---quantization table
!色差( SMPTE 370M ).Cb Cr
!*DQTC
DATA 32, 16, 17, 25, 26, 26, 42, 44
DATA 16, 17, 25, 25, 26, 38, 43, 91
DATA 17, 25, 26, 27, 40, 41, 91, 96
DATA 25, 25, 27, 40, 41, 84, 93,197
DATA 26, 26, 40, 41, 84, 86,191,203
DATA 26, 38, 41, 84, 86,177,197,209
DATA 42, 43, 91, 93,191,197,219,232
DATA 44, 91, 96,197,203,209,232,246
!---Zigzag table
DATA 0, 1, 5, 6,14,15,27,28
DATA 2, 4, 7,13,16,26,29,42
DATA 3, 8,12,17,25,30,41,43
DATA 9,11,18,24,31,40,44,53
DATA 10,19,23,32,39,45,52,54
DATA 20,22,33,38,46,51,55,60
DATA 21,34,37,47,50,56,59,61
DATA 35,36,48,49,57,58,62,63
!---HT selection
DATA 0,2,2 !DC. Y Cb Cr
DATA 1,3,3 !AC. Y Cb Cr
!---QT selection
DATA 0,1,1 ! Y Cb Cr
!==========================================
! analizing frequency SV(nnnn,ssss) for DHT
SUB ZFRE0
MAT SV=ZER
LET B2(0)=0 ! Y.DC( start prediction)
LET B2(1)=0 ! Cb.DC
LET B2(2)=0 ! Cr.DC
!---
FOR V09=0 TO DV_-1 STEP 8*MV(0)
FOR U09=0 TO DU-1 STEP 8*MH(0)
!---MCU
FOR P=0 TO CMO ! ( 0=Y 1=Cb 2=Cr)
FOR V0=V09 TO V09+8*MV(P)-1 STEP 8
FOR U0=U09 TO U09+8*MH(P)-1 STEP 8
CALL F_BLK0
NEXT U0
NEXT V0
NEXT P
!---
NEXT U09
NEXT V09
!---
CALL MAKE_DHT !DH( ,J) DV( ,J) <--S_( ,J)
END SUB
!Page-2 へ続く
|
|