home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Examples / c11 / src / jfif.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-17  |  3.4 KB  |  114 lines

  1. //
  2. // Copyright (c) 1997,1998 Colosseum Builders, Inc.
  3. // All rights reserved.
  4. //
  5. // Colosseum Builders, Inc. makes no warranty, expressed or implied
  6. // with regards to this software. It is provided as is.
  7. //
  8. // See the README.TXT file that came with this software for restrictions
  9. // on the use and redistribution of this file or send E-mail to
  10. // info@colosseumbuilders.com
  11. //
  12.  
  13. //
  14. // JPEG Encoder Library.
  15. //
  16. // Title:   JFIF Implementation
  17. //
  18. // Author: John M. Miano  miano@colosseumbuilders.com
  19. //
  20. // Description:
  21. //
  22. //    This module contains the RGB/YcbCr conversion functions specified
  23. //    by the JFIF standard. This functions are implemented using scaled
  24. //    integers.
  25. //
  26. //    If you are using MS C++ you may wish to re-write this since that compile
  27. //    generates terrible code for these function. Borland C++ is great here,
  28. //    and G++ works well if you turn on optimizations.
  29. //
  30.  
  31. //
  32. #include "jfif.h"
  33.  
  34.  
  35. const int ScaleFactor = 12 ;
  36. const int ScaleValue = (1<<ScaleFactor) ;
  37. const int Rounding = (1<<(ScaleFactor-1)) ;
  38.  
  39. static inline int Scaled (double value)
  40. {
  41.   return (int) (ScaleValue * value) ;
  42. }
  43.  
  44. JPEGSAMPLE YCbCrToR (int yy, int /*cb*/, int cr)
  45. {
  46.   int result = yy + ((Scaled (1.402) * (cr - JpegMidpointSampleValue) + Rounding) >> ScaleFactor) ;
  47.   if (result < 0)
  48.     result = 0 ;
  49.   else if (result > JpegMaxSampleValue)
  50.     result = JpegMaxSampleValue ;
  51.   return (JPEGSAMPLE) result ;
  52. }
  53.  
  54. JPEGSAMPLE YCbCrToG (int yy, int cb, int cr)
  55. {
  56.   int result = yy - ((Scaled (0.34414) * (cb - JpegMidpointSampleValue)
  57.                      + Scaled (0.71414) * (cr - JpegMidpointSampleValue)
  58.                      + Rounding) >> ScaleFactor) ;
  59.  
  60.   if (result < 0)
  61.     result = 0 ;
  62.   else if (result > JpegMaxSampleValue)
  63.     result = JpegMaxSampleValue ;
  64.   return (JPEGSAMPLE) result ;
  65. }
  66.  
  67. JPEGSAMPLE YCbCrToB (int yy, int cb, int /*cr*/)
  68. {
  69.   int result = yy + ((Scaled (1.772) * (cb - JpegMidpointSampleValue)
  70.              + Rounding) >> ScaleFactor) ;
  71.   if (result < 0)
  72.     result = 0 ;
  73.   else if (result > JpegMaxSampleValue)
  74.     result = JpegMaxSampleValue ;
  75.   return (JPEGSAMPLE) result ;
  76. }
  77.  
  78. JPEGSAMPLE RGBToY (JPEGSAMPLE red, JPEGSAMPLE green, JPEGSAMPLE blue)
  79. {
  80.   int result = (Scaled (0.299) * red
  81.              + Scaled (0.587) * green + Scaled (0.114) * blue
  82.              + Rounding) >> ScaleFactor ;
  83.   if (result > JpegMaxSampleValue)
  84.       result = JpegMaxSampleValue ;
  85.   else if (result < JpegMinSampleValue)
  86.       result = JpegMinSampleValue ;
  87.   return result ;
  88. }
  89.  
  90. JPEGSAMPLE RGBToCb (JPEGSAMPLE red, JPEGSAMPLE green, JPEGSAMPLE blue)
  91. {
  92.   int result = ((JpegMidpointSampleValue<<ScaleFactor)
  93.              + Rounding - Scaled (0.1687) * red
  94.              - Scaled (0.3313) * green + Scaled (0.5) * blue) >> ScaleFactor  ;
  95.   if (result > JpegMaxSampleValue)
  96.       result = JpegMaxSampleValue ;
  97.   else if (result < JpegMinSampleValue)
  98.       result = JpegMinSampleValue ;
  99.   return result ;
  100. }
  101.  
  102. JPEGSAMPLE RGBToCr (JPEGSAMPLE red, JPEGSAMPLE green, JPEGSAMPLE blue)
  103. {
  104.   int result = ((JpegMidpointSampleValue<<ScaleFactor)
  105.              + Rounding + Scaled (0.5) * red
  106.              - Scaled (0.4187) * green - Scaled (0.0813) * blue) >> ScaleFactor ;
  107.   if (result > JpegMaxSampleValue)
  108.       result = JpegMaxSampleValue ;
  109.   else if (result < JpegMinSampleValue)
  110.       result = JpegMinSampleValue ;
  111.   return result ;
  112. }
  113.  
  114.