home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / Real_Tree_20250610152006.psc / Tree2 / cJpeg.cls < prev   
Text File  |  2006-10-08  |  15KB  |  328 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "cJpeg"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15. Option Base 0
  16.  
  17. 'Class Name:   cJpeg.cls  "JPEG Encoder Class"
  18. 'Author:       John Korejwa  <korejwa@tiac.net>
  19. 'Version:      0.9 beta  [26 / November / 2003]
  20. '
  21. '
  22. 'Legal:
  23. '        This class is intended for and was uploaded to www.planetsourcecode.com
  24. '
  25. '        This product includes JPEG compression code developed by John Korejwa.  <korejwa@tiac.net>
  26. '        Source code, written in Visual Basic, is freely available for non-commercial,
  27. '        non-profit use at www.planetsourcecode.com.
  28. '
  29. '
  30. 'Credits:
  31. '        Special thanks to Barry G., a government research scientist who took an interest in my
  32. '        steganography software and research in late 1999.  I never met Barry in person, but he
  33. '        was kind enough to buy and mail me a book with the ISO DIS 10918-1 JPEG standard.
  34. '
  35. '
  36. 'Description:  This class contains code for compressing pictures, sampled via hDC, into
  37. '              baseline .JPG files.  Please report any errors or unusual behavior to the email
  38. '              address above.
  39. '
  40. 'Dependencies: None
  41. '
  42.  
  43.  
  44. 'JPEG Marker Constants                (Note: VB compiler does not compile unused constants)
  45.                                       'Non-Differential Huffman Coding
  46. Private Const SOF0    As Long = &HC0& 'Baseline DCT
  47. Private Const SOF1    As Long = &HC1& 'Extended sequential DCT
  48. Private Const SOF2    As Long = &HC2& 'Progressive DCT
  49. Private Const SOF3    As Long = &HC3& 'Spatial (sequential) lossless
  50.                                       'Differential Huffman coding
  51. Private Const SOF5    As Long = &HC5& 'Differential sequential DCT
  52. Private Const SOF6    As Long = &HC6& 'Differential progressive DCT
  53. Private Const SOF7    As Long = &HC7& 'Differential spatial
  54.                                       'Non-Differential arithmetic coding
  55. Private Const JPG     As Long = &HC8& 'Reserved for JPEG extentions
  56. Private Const SOF9    As Long = &HC9& 'Extended sequential DCT
  57. Private Const SOF10   As Long = &HCA& 'Progressive DCT
  58. Private Const SOF11   As Long = &HCB& 'Spatial (sequential) lossless
  59.                                       'Differential arithmetic coding
  60. Private Const SOF13   As Long = &HCD& 'Differential sequential DCT
  61. Private Const SOF14   As Long = &HCE& 'Differential progressive DCT
  62. Private Const SOF15   As Long = &HCF& 'Differential Spatial
  63.                                       'Other Markers
  64. Private Const DHT     As Long = &HC4& 'Define Huffman tables
  65. Private Const DAC     As Long = &HCC& 'Define arithmetic coding conditioning(s)
  66. Private Const RSTm    As Long = &HD0& 'Restart with modulo 8 count "m"
  67. Private Const RSTm2   As Long = &HD7& 'to 'Restart with modulo 8 count "m"
  68. Private Const SOI     As Long = &HD8& 'Start of image
  69. Private Const EOI     As Long = &HD9& 'End of image
  70. Private Const SOS     As Long = &HDA& 'Start of scan
  71. Private Const DQT     As Long = &HDB& 'Define quantization table(s)
  72. Private Const DNL     As Long = &HDC& 'Define number of lines
  73. Private Const DRI     As Long = &HDD& 'Define restart interval
  74. Private Const DHP     As Long = &HDE& 'Define hierarchical progression
  75. Private Const EXP     As Long = &HDF& 'Expand reference components
  76. Private Const APP0    As Long = &HE0& 'Reserved for application segments
  77. Private Const APPF    As Long = &HEF& '  to Reserved for application segments
  78. Private Const JPGn    As Long = &HF0& 'Reserved for JPEG Extentions
  79. Private Const JPGn2   As Long = &HFD& '  to Reserved for JPEG Extentions
  80. Private Const COM     As Long = &HFE& 'Comment
  81. Private Const RESm    As Long = &H2&  'Reserved
  82. Private Const RESm2   As Long = &HBF& '  to Reserved
  83. Private Const TEM     As Long = &H1&  'For temporary use in arithmetic coding
  84.  
  85. 'Consider these arrays of constants.
  86. 'They are initialized with the class and do not change.
  87. Private QLumin(63)    As Integer 'Standard Luminance   Quantum (for 50% quality)
  88. Private QChrom(63)    As Integer 'Standard Chrominance Quantum (for 50% quality)
  89. Private FDCTScale(7)  As Double  'Constants for scaling FDCT Coefficients
  90. Private IDCTScale(7)  As Double  'Constants for scaling IDCT Coefficients
  91. Private ZigZag(7, 7)  As Long    'Zig Zag order of 8X8 block of samples
  92.  
  93. 'API constants
  94. Private Const BLACKONWHITE    As Long = 1 'nStretchMode constants for
  95. Private Const COLORONCOLOR    As Long = 3 '  SetStretchBltMode() API function
  96. Private Const HALFTONE        As Long = 4 'HALFTONE not supported in Win 95, 98, ME
  97.  
  98. Private Const BI_RGB          As Long = 0
  99. Private Const DIB_RGB_COLORS  As Long = 0
  100.  
  101.  
  102. 'Variable types needed for DIBSections.
  103. Private Type SAFEARRAYBOUND
  104.     cElements         As Long
  105.     lLbound           As Long
  106. End Type
  107. Private Type SAFEARRAY2D
  108.     cDims             As Integer
  109.     fFeatures         As Integer
  110.     cbElements        As Long
  111.     cLocks            As Long
  112.     pvData            As Long
  113.     Bounds(0 To 1)    As SAFEARRAYBOUND
  114. End Type
  115. Private Type RGBQUAD
  116.     rgbBlue           As Byte
  117.     rgbGreen          As Byte
  118.     rgbRed            As Byte
  119.     rgbReserved       As Byte
  120. End Type
  121. Private Type BITMAPINFOHEADER
  122.     biSize            As Long
  123.     biWidth           As Long
  124.     biHeight          As Long
  125.     biPlanes          As Integer
  126.     biBitCount        As Integer
  127.     biCompression     As Long
  128.     biSizeImage       As Long
  129.     biXPelsPerMeter   As Long
  130.     biYPelsPerMeter   As Long
  131.     biClrUsed         As Long
  132.     biClrImportant    As Long
  133. End Type
  134. Private Type BITMAPINFO
  135.     bmiHeader         As BITMAPINFOHEADER
  136.     bmiColors         As RGBQUAD
  137. End Type
  138.  
  139. 'API needed for creating DIBSections for sampling and pixel access.
  140. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
  141. Private Declare Function CreateDIBSection2 Lib "gdi32" Alias "CreateDIBSection" (ByVal hDC As Long, pBitmapInfo As BITMAPINFO, ByVal un As Long, lplpVoid As Long, ByVal handle As Long, ByVal dw As Long) As Long   'lplpVoid changed to ByRef
  142. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  143. Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
  144. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  145. Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
  146. Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (Ptr() As Any) As Long
  147. Private Declare Function SetStretchBltMode Lib "gdi32" (ByVal hDC As Long, ByVal nStretchMode As Long) As Long
  148. Private Declare Function StretchBlt Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
  149. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
  150.  
  151.  
  152. 'Custom variable types used for this JPEG encoding implementation
  153. Private Type QUANTIZATIONTABLE
  154.     Qk(63)            As Integer 'Quantization Values
  155.     FScale(63)        As Single  'Multiplication values to scale and Quantize   FDCT output
  156.     IScale(63)        As Single  'Multiplication values to scale and DeQuantize IDCT input
  157. End Type
  158. Private Type HUFFMANTABLE
  159.     BITS(15)          As Byte    'Number of huffman codes of length i+1
  160.     HUFFVAL(255)      As Byte    'Huffman symbol values
  161.     EHUFSI(255)       As Long    'Huffman code size for symbol i
  162.     EHUFCO(255)       As Long    'Huffman code      for symbol i
  163.     MINCODE(15)       As Long    '
  164.     MAXCODE(15)       As Long    'Largest code value for length i+1
  165. End Type
  166. Private Type COMPONENT
  167.     Ci                As Long    'Component ID                       [0-255]
  168.     Hi                As Long    'Horizontal Sampling Factor         [1-4]
  169.     Vi                As Long    'Vertical   Sampling Factor         [1-4]
  170.     Tqi               As Long    'Quantization Table Select          [0-3]
  171.     data()            As Integer 'DCT Coefficients
  172. End Type
  173.  
  174. Private PP            As Long    'Sample Precision [8, 12]
  175. Private YY            As Long    'Number of lines             [Image Height]
  176. Private XX            As Long    'Number of samples per line  [Image Width]
  177. Private Nf            As Long    'Number of components in Frame
  178.  
  179. Private HMax          As Long    'Maximum horizontal sampling frequency
  180. Private VMax          As Long    'Maximum vertical   sampling frequency
  181.  
  182. Private m_Data()      As Byte    'JPEG File Data
  183. Private m_Chr         As Long    'Current Character in m_Data
  184. Private m_Ptr         As Long    'Byte index in m_Data
  185. Private m_Bit         As Long    'Bit  index in m_Chr
  186.  
  187. Private m_Block(7, 7) As Single  'Buffer for calculating DCT
  188.  
  189. Private QTable(3)     As QUANTIZATIONTABLE  '4 Quantization Tables
  190. Private HuffDC(3)     As HUFFMANTABLE       '4 DC Huffman Tables
  191. Private HuffAC(3)     As HUFFMANTABLE       '4 AC Huffman Tables
  192. Private Comp()        As COMPONENT          'Scan Components
  193.  
  194. Private m_Quality     As Long
  195. Private m_Comment     As String
  196.  
  197.  
  198.  
  199. '========================================================================================
  200. '              D I S C R E T E   C O S I N E   T R A N S F O R M A T I O N
  201. '========================================================================================
  202. Private Sub FDCT()
  203.     Static t0   As Single 'Given an 8X8 block of discretely sampled values [m_Block(0-7, 0-7)],
  204.     Static t1   As Single 'replace them with their (scaled) Forward Discrete Cosine Transformation values.
  205.     Static t2   As Single '80 (+64) multiplications and 464 additions are needed.
  206.     Static t3   As Single 'Values are scaled on output, meaning that each of the 64 elements must be
  207.     Static t4   As Single 'multiplied by constants for a final FDCT.  These final constants are combined
  208.     Static t5   As Single 'with Quantization constants, so a final 64 multiplications combine the
  209.     Static t6   As Single 'completion of the FDCT and Quantization in one step.
  210.     Static t7   As Single
  211.     Static t8   As Single
  212.     Static i    As Long
  213.  
  214.     For i = 0 To 7                  'Process 1D FDCT on each row
  215.         t0 = m_Block(i, 0) + m_Block(i, 7)
  216.         t1 = m_Block(i, 0) - m_Block(i, 7)
  217.         t2 = m_Block(i, 1) + m_Block(i, 6)
  218.         t3 = m_Block(i, 1) - m_Block(i, 6)
  219.         t4 = m_Block(i, 2) + m_Block(i, 5)
  220.         t5 = m_Block(i, 2) - m_Block(i, 5)
  221.         t6 = m_Block(i, 3) + m_Block(i, 4)
  222.         t7 = m_Block(i, 3) - m_Block(i, 4)
  223.  
  224.         t7 = t7 + t5
  225.         t8 = t0 - t6
  226.         t6 = t6 + t0
  227.         t0 = t2 + t4
  228.         t2 = (t2 - t4 + t8) * 0.707106781186548   'Cos(2# * PI / 8#)
  229.         t4 = t1 + t3
  230.         t3 = (t3 + t5) * 0.707106781186548        'Cos(2# * PI / 8#)
  231.         t5 = (t4 - t7) * 0.382683432365091        'Cos(3# * PI / 8#)
  232.         t7 = t7 * 0.541196100146196 - t5          'Cos(PI / 8#) - Cos(3# * PI / 8#)
  233.         t4 = t4 * 1.30656296487638 - t5           'Cos(PI / 8#) + Cos(3# * PI / 8#)
  234.         t5 = t1 + t3
  235.         t1 = t1 - t3
  236.  
  237.         m_Block(i, 0) = t6 + t0
  238.         m_Block(i, 4) = t6 - t0
  239.         m_Block(i, 1) = t5 + t4
  240.         m_Block(i, 7) = t5 - t4
  241.         m_Block(i, 2) = t8 + t2
  242.         m_Block(i, 6) = t8 - t2
  243.         m_Block(i, 5) = t1 + t7
  244.         m_Block(i, 3) = t1 - t7
  245.     Next i
  246.  
  247.     For i = 0 To 7                   'Process 1D FDCT on each column
  248.         t0 = m_Block(0, i) + m_Block(7, i)
  249.         t1 = m_Block(0, i) - m_Block(7, i)
  250.         t2 = m_Block(1, i) + m_Block(6, i)
  251.         t3 = m_Block(1, i) - m_Block(6, i)
  252.         t4 = m_Block(2, i) + m_Block(5, i)
  253.         t5 = m_Block(2, i) - m_Block(5, i)
  254.         t6 = m_Block(3, i) + m_Block(4, i)
  255.         t7 = m_Block(3, i) - m_Block(4, i)
  256.  
  257.         t7 = t7 + t5
  258.         t8 = t0 - t6
  259.         t6 = t6 + t0
  260.         t0 = t2 + t4
  261.         t2 = (t2 - t4 + t8) * 0.707106781186548   'Cos(2# * PI / 8#)
  262.         t4 = t1 + t3
  263.         t3 = (t3 + t5) * 0.707106781186548        'Cos(2# * PI / 8#)
  264.         t5 = (t4 - t7) * 0.382683432365091        'Cos(3# * PI / 8#)
  265.         t7 = t7 * 0.541196100146196 - t5          'Cos(PI / 8#) - Cos(3# * PI / 8#)
  266.         t4 = t4 * 1.30656296487638 - t5           'Cos(PI / 8#) + Cos(3# * PI / 8#)
  267.         t5 = t1 + t3
  268.         t1 = t1 - t3
  269.  
  270.         m_Block(0, i) = t6 + t0
  271.         m_Block(4, i) = t6 - t0
  272.         m_Block(1, i) = t5 + t4
  273.         m_Block(7, i) = t5 - t4
  274.         m_Block(2, i) = t8 + t2
  275.         m_Block(6, i) = t8 - t2
  276.         m_Block(5, i) = t1 + t7
  277.         m_Block(3, i) = t1 - t7
  278.     Next i
  279. End Sub
  280.  
  281.  
  282.  
  283.  
  284. '================================================================================
  285. '                 H U F F M A N   T A B L E   G E N E R A T I O N
  286. '================================================================================
  287. Private Sub OptimizeHuffman(TheHuff As HUFFMANTABLE, freq() As Long)
  288. 'Generate optimized values for BITS and HUFFVAL in a HUFFMANTABLE
  289. 'based on symbol frequency counts.  freq must be dimensioned freq(0-256)
  290. 'and contain counts of symbols 0-255.  freq is destroyed in this procedure.
  291.     Dim i              As Long
  292.     Dim j              As Long
  293.     Dim k              As Long
  294.     Dim n              As Long
  295.     Dim V1             As Long
  296.     Dim V2             As Long
  297.     Dim others(256)    As Long
  298.     Dim codesize(256)  As Long
  299.     Dim BITS(256)      As Long
  300.     Dim swp            As Long
  301.     Dim swp2           As Long
  302.  
  303.  
  304.     For i = 0 To 256  'Initialize others to -1, (this value terminates chain of indicies)
  305.         others(i) = -1
  306.     Next i
  307.     freq(256) = 1     'Add dummy symbol to guarantee no code will be all '1' bits
  308.  
  309.    'Generate BITS(256)Pii ers(    ts15MBpKai) - m_Blocm(   Dim V2     - m_BlockDim V2   be alsiw'Cos(P
  310.  
  311.   i)O
  312.   i)O
  313.   i)O
  314.   i)  'Am_BYu '5O
  315.   AesteNB================Dimji) = -1
  316.     NeF4e     As Long
  317.  
  318.  
  319.     rmAires) "f  rmAirohoD i)OTBlock(iValon Ta6 = m
  320.     fn)
  321. '6) = ynI O Nm'replace them with their miEcitiE   As Long
  322.  
  323.  
  324.         n= 0 =
  325. '        cP d,e     Al======= (NeF6(thly sampled values [m_Block(0-7, 0-7)],
  326.     Static t1   As Single 'replace them with their (s)
  327.  +(.
  328.       Al=====lace(6bmg===eFMALonf====2w     Rv+fs Single 'replacer