home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / warptlk3.zip / TOOLKIT / SAMPLES / MM / CODEC / SAMPENCO.C < prev    next >
C/C++ Source or Header  |  1995-08-24  |  10KB  |  221 lines

  1. /*****************************************************************************/
  2. /*                                                                           */
  3. /* SAMPENCO.C: Sample Codec Compression Function Definitions.                */
  4. /*             Your decompression procedure declatations will replace these. */
  5. /*                                                                           */
  6. /* Copyright (c) IBM Corporation 1991,1994             All Rights Reserved   */
  7. /*                                                                           */
  8. /*****************************************************************************/
  9.  
  10. #define  INCL_WIN
  11. #define  INCL_GPI
  12. #define  INCL_DOS
  13. #define  INCL_OS2MM
  14. #define  INCL_MMIO_CODEC
  15. #include <os2.h>
  16. #include <os2me.h>
  17. #include "sampinst.h"
  18.  
  19. /* Declarations to look-up tables defined at the bottom of the file.         */
  20. BYTE bLookYh[256], bLookYl[256];
  21.  
  22.  
  23.  
  24. /*****************************************************************************/
  25. /*                                                                           */
  26. /* SUBROUTINE NAME: CompressKeyFromRGB565                                    */
  27. /*     This routine does no real compression.  It converts from RGB565 to    */
  28. /*     luminance only (8-bits per pel).                                      */
  29. /*                                                                           */
  30. /*****************************************************************************/
  31.  
  32. ULONG CompressKeyFromRGB565 ( PBYTE pbSrc, PBYTE pbDst,
  33.                               ULONG ulWidth, ULONG ulHeight )
  34.    {
  35.    ULONG ulImageX, ulImageY;
  36.  
  37.    /* Loop though each pel of the image, converting each to luminance.       */
  38.    ulImageY = ulHeight;
  39.    while ( ulImageY-- )
  40.       {
  41.       ulImageX = ulWidth;
  42.       while ( ulImageX-- )
  43.          {
  44.          *pbDst++= * ( bLookYl + *pbSrc ) + * ( bLookYh + *(pbSrc+1) );
  45.          pbSrc += 2;
  46.          }
  47.       }
  48.  
  49.    /* Return 8 bits, or 1 byte per pixel final image size.                   */
  50.    return ( ulWidth * ulHeight );
  51.    }
  52.  
  53.  
  54.  
  55.  
  56.  
  57. /*****************************************************************************/
  58. /*                                                                           */
  59. /* SUBROUTINE NAME: CompressKeyFromYUV411                                    */
  60. /*     This routine is the same as the above compression excepting that      */
  61. /*     the source data format in this one is YUV411.                         */
  62. /*                                                                           */
  63. /*****************************************************************************/
  64.  
  65. ULONG CompressKeyFromYUV411 ( PBYTE pbSrc, PBYTE pbDst,
  66.                               ULONG ulWidth, ULONG ulHeight )
  67.    {
  68.    ULONG ulImageX, ulImageY;
  69.  
  70.    /* Loop though each pel of the image, converting each to luminance.       */
  71.    ulImageY = ulHeight;
  72.    while ( ulImageY-- )
  73.       {
  74.       ulImageX = ulWidth;
  75.       while ( ulImageX-- )
  76.          {
  77.          *pbDst++= *pbSrc;
  78.          pbSrc += 2;
  79.          }
  80.       }
  81.  
  82.    /* Return 8 bits, or 1 byte per pixel final image size.                   */
  83.    return ( ulWidth * ulHeight );
  84.    }
  85.  
  86.  
  87.  
  88.  
  89.  
  90. /*****************************************************************************/
  91. /*                                                                           */
  92. /* SUBROUTINE NAME: Compressor_CompressFrame                                 */
  93. /*     This routine is called from SAMPMAIN.C during a frame compression     */
  94. /*     call.  This example calls the appropriate compression routine based   */
  95. /*     on the type of frame (delta versus key) and the type of input         */
  96. /*     color space format.                                                   */
  97. /*                                                                           */
  98. /*****************************************************************************/
  99.  
  100. ULONG CompressFrame ( PMAIN_INST pMainInst,
  101.                       PBYTE      pbSrcBuffer,
  102.                       PBYTE      pbDstBuffer )
  103.    {
  104.    /* Check to see if we are compressing from RGB16 or YUV411.               */
  105.    if ( pMainInst->ulColorEncoding == MMIO_RGB_5_6_5 )
  106.  
  107.       /* Note that I'm actually calling the key frame compression            */
  108.       /* routine instead of the delta frame compression routine.  If you     */
  109.       /* are supporting delta frame compression, yours goes here.            */
  110.       return ( CompressKeyFromRGB565 ( pbSrcBuffer, pbDstBuffer,
  111.                       pMainInst->ulWidth, pMainInst->ulHeight ) );
  112.    else
  113.  
  114.       /* Ditto, this is in place of an actual delta frame compression.       */
  115.       return ( CompressKeyFromYUV411 ( pbSrcBuffer, pbDstBuffer,
  116.                       pMainInst->ulWidth, pMainInst->ulHeight ) );
  117.    }
  118.  
  119.  
  120.  
  121.  
  122. /* Following is the defines for the two most popular                         */
  123. /* source formats, RGB16 and YUV411.                                         */
  124.  
  125. #pragma data_seg (PROCESS_SHARED_DATA_SEGMENT)
  126.  
  127. /* RGB16 (RGB565) format:  loaded as a WORD, each pixel looks like:          */
  128. /*    R4:R3:R2:R1:R0:G5:G4:G3 G2:G1:G0:B4:B3:B2:B1:B0                        */
  129.  
  130. /* YUV411 format: loaded as four consecutive WORDs, each word looks like:    */
  131. /*    Cb6:Cb5:Cr6:Cr5:???:???:???:???  Ya6:Ya5:Ya4:Ya3:Ya2:Ya1:Ya0:???       */
  132. /*    Cb4:Cb3:Cr4:Cr3:???:???:???:???  Yb6:Yb5:Yb4:Yb3:Yb2:Yb1:Yb0:???       */
  133. /*    Cb2:Cb1:Cr2:Cr1:???:???:???:???  Yc6:Yc5:Yc4:Yc3:Yc2:Yc1:Yc0:???       */
  134. /*    Cb0:???:Cr0:???:???:???:???:???  Yd6:Yd5:Yd4:Yd3:Yd2:Yd1:Yd0:???       */
  135. /*                                            ┌───────────────────┐          */
  136. /*                                            │        Cr         │          */
  137. /*                                            ├───────────────────┤          */
  138. /* Where the four pixels in a row look like:  │        Cb         │          */
  139. /*                                            ├────┬────┬────┬────┤          */
  140. /*                                            │ Ya │ Yb │ Yc │ Yd │          */
  141. /*                                            └────┴────┴────┴────┘          */
  142.  
  143.  
  144.  
  145.  
  146.  
  147. /* Following is two tables that assist in the conversion of RGB565 to Y8.    */
  148. /* To get luminance, you look up the first byte RRRRRGGG in bLookYh to get   */
  149. /* part of the luminance information and add it to the second byte GGGBBBBB  */
  150. /* in bLookYl to get the complete luminance value in the range {0..255}.     */
  151.  
  152. BYTE bLookYh[256]= {
  153.              0,  19,  38,  57,  77,  96, 115, 134,
  154.              2,  22,  41,  60,  79,  98, 117, 136,
  155.              5,  24,  43,  62,  81, 101, 120, 139,
  156.              7,  26,  46,  65,  84, 103, 122, 141,
  157.             10,  29,  48,  67,  86, 105, 125, 144,
  158.             12,  31,  50,  70,  89, 108, 127, 146,
  159.             15,  34,  53,  72,  91, 110, 129, 149,
  160.             17,  36,  55,  74,  94, 113, 132, 151,
  161.             19,  39,  58,  77,  96, 115, 134, 153,
  162.             22,  41,  60,  79,  98, 118, 137, 156,
  163.             24,  43,  63,  82, 101, 120, 139, 158,
  164.             27,  46,  65,  84, 103, 122, 142, 161,
  165.             29,  48,  68,  87, 106, 125, 144, 163,
  166.             32,  51,  70,  89, 108, 127, 146, 166,
  167.             34,  53,  72,  92, 111, 130, 149, 168,
  168.             37,  56,  75,  94, 113, 132, 151, 170,
  169.             39,  58,  77,  96, 116, 135, 154, 173,
  170.             41,  61,  80,  99, 118, 137, 156, 175,
  171.             44,  63,  82, 101, 120, 140, 159, 178,
  172.             46,  65,  85, 104, 123, 142, 161, 180,
  173.             49,  68,  87, 106, 125, 144, 164, 183,
  174.             51,  70,  89, 109, 128, 147, 166, 185,
  175.             54,  73,  92, 111, 130, 149, 168, 188,
  176.             56,  75,  94, 113, 133, 152, 171, 190,
  177.             58,  78,  97, 116, 135, 154, 173, 192,
  178.             61,  80,  99, 118, 137, 157, 176, 195,
  179.             63,  82, 102, 121, 140, 159, 178, 197,
  180.             66,  85, 104, 123, 142, 161, 181, 200,
  181.             68,  87, 106, 126, 145, 164, 183, 202,
  182.             71,  90, 109, 128, 147, 166, 185, 205,
  183.             73,  92, 111, 130, 150, 169, 188, 207,
  184.             76,  95, 114, 133, 152, 171, 190, 209 };
  185.  
  186. BYTE bLookYl[256]= {
  187.              0,   1,   2,   3,   4,   5,   6,   7,
  188.              7,   8,   9,  10,  11,  12,  13,  14,
  189.             15,  16,  17,  18,  19,  20,  20,  21,
  190.             22,  23,  24,  25,  26,  27,  28,  29,
  191.              2,   3,   4,   5,   6,   7,   8,   9,
  192.             10,  11,  12,  13,  14,  14,  15,  16,
  193.             17,  18,  19,  20,  21,  22,  23,  24,
  194.             25,  26,  27,  27,  28,  29,  30,  31,
  195.              5,   6,   7,   8,   8,   9,  10,  11,
  196.             12,  13,  14,  15,  16,  17,  18,  19,
  197.             20,  21,  22,  22,  23,  24,  25,  26,
  198.             27,  28,  29,  30,  31,  32,  33,  34,
  199.              7,   8,   9,  10,  11,  12,  13,  14,
  200.             15,  16,  16,  17,  18,  19,  20,  21,
  201.             22,  23,  24,  25,  26,  27,  28,  29,
  202.             29,  30,  31,  32,  33,  34,  35,  36,
  203.             10,  10,  11,  12,  13,  14,  15,  16,
  204.             17,  18,  19,  20,  21,  22,  23,  24,
  205.             24,  25,  26,  27,  28,  29,  30,  31,
  206.             32,  33,  34,  35,  36,  37,  37,  38,
  207.             12,  13,  14,  15,  16,  17,  18,  18,
  208.             19,  20,  21,  22,  23,  24,  25,  26,
  209.             27,  28,  29,  30,  31,  31,  32,  33,
  210.             34,  35,  36,  37,  38,  39,  40,  41,
  211.             14,  15,  16,  17,  18,  19,  20,  21,
  212.             22,  23,  24,  25,  25,  26,  27,  28,
  213.             29,  30,  31,  32,  33,  34,  35,  36,
  214.             37,  38,  39,  39,  40,  41,  42,  43,
  215.             17,  18,  19,  20,  20,  21,  22,  23,
  216.             24,  25,  26,  27,  28,  29,  30,  31,
  217.             32,  33,  33,  34,  35,  36,  37,  38,
  218.             39,  40,  41,  42,  43,  44,  45,  46 };
  219.  
  220. #pragma data_seg ()
  221.