home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2008 January / Mac_easy_01_08.iso / Software / Online / Chat / macam.0.9.1.dmg / macam source / utilities / LookUpTable.m < prev    next >
Encoding:
Text File  |  2006-07-25  |  6.3 KB  |  242 lines

  1. //
  2. //  LookUpTable.m
  3. //
  4. //  macam - webcam app and QuickTime driver component
  5. //
  6. //  Created by hxr on 6/20/06.
  7. //  Copyright (C) 2006 HXR (hxr@users.sourceforge.net). 
  8. //
  9. //  This program is free software; you can redistribute it and/or modify
  10. //  it under the terms of the GNU General Public License as published by
  11. //  the Free Software Foundation; either version 2 of the License, or
  12. //  (at your option) any later version.
  13. //
  14. //  This program is distributed in the hope that it will be useful,
  15. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. //  GNU General Public License for more details.
  18. //
  19. //  You should have received a copy of the GNU General Public License
  20. //  along with this program; if not, write to the Free Software
  21. //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
  22. //
  23.  
  24.  
  25. #import "LookUpTable.h"
  26.  
  27. #include "GlobalDefs.h"
  28.  
  29.  
  30. @implementation LookUpTable
  31.  
  32. - (id) init 
  33. {
  34.     self = [super init];
  35.     
  36.     brightness=0.0f;
  37.     contrast=1.0f;
  38.     gamma=1.0f;
  39.     saturation=65536;
  40.     redGain=1.0f;
  41.     greenGain=1.0f;
  42.     blueGain=1.0f;
  43.     [self recalcTransferLookup];
  44.     
  45.     return self;
  46. }
  47.  
  48. - (float) brightness { return brightness; }
  49.  
  50. - (void) setBrightness:(float)newBrightness 
  51. {
  52.     brightness=CLAMP(newBrightness,-1.0f,1.0f);
  53.     [self recalcTransferLookup];
  54. }
  55.  
  56. - (float) contrast { return contrast; }
  57.  
  58. - (void) setContrast:(float)newContrast 
  59. {
  60.     contrast=CLAMP(newContrast,0.0f,2.0f);
  61.     [self recalcTransferLookup];
  62. }
  63.  
  64. - (float) gamma { return gamma; }
  65.  
  66. - (void) setGamma:(float)newGamma {
  67.     gamma=CLAMP(newGamma,0.0f,2.0f);
  68.     [self recalcTransferLookup];
  69. }
  70.  
  71. - (float) saturation { return ((float)saturation)/65536.0f; }
  72.  
  73. - (void) setSaturation:(float)newSaturation 
  74. {
  75.     saturation=65536.0f*CLAMP(newSaturation,0.0f,2.0f);
  76. }
  77.  
  78.  
  79. - (void) setGainsRed:(float)r green:(float)g blue:(float)b 
  80. {
  81.     redGain=r;
  82.     greenGain=g;
  83.     blueGain=b;
  84.     [self recalcTransferLookup];
  85. }
  86.  
  87.  
  88. - (UInt8) red: (UInt8) r  green: (int) g
  89. {
  90.     int rr = (((r - g) * saturation) / 65536) + g;
  91.     return redTransferLookup[CLAMP(rr,0,255)];
  92. }
  93.  
  94.  
  95. - (UInt8) green: (UInt8) g
  96. {
  97.     return greenTransferLookup[g];
  98. }
  99.  
  100.  
  101. - (UInt8) blue: (UInt8) b  green: (int) g
  102. {
  103.     int bb = (((b - g) * saturation) / 65536) + g;
  104.     return blueTransferLookup[CLAMP(bb,0,255)];
  105. }
  106.  
  107.  
  108. - (void) processTriplet: (UInt8 *) tripletIn toHere: (UInt8 *) tripletOut
  109. {
  110.     int g =    tripletIn[1];
  111.     int r = (((tripletIn[0] - g) * saturation) / 65536) + g;
  112.     int b = (((tripletIn[2] - g) * saturation) / 65536) + g;
  113.     
  114.     tripletOut[0] = redTransferLookup[CLAMP(r,0,255)];
  115.     tripletOut[1] = greenTransferLookup[CLAMP(g,0,255)];
  116.     tripletOut[2] = blueTransferLookup[CLAMP(b,0,255)];
  117. }
  118.  
  119.  
  120. - (void) processImage: (UInt8 *) buffer numRows: (long) numRows rowBytes: (long) rowBytes bpp: (short) bpp invert: (BOOL) invert
  121. {
  122.     UInt8 * ptr;
  123.     long  w, h;
  124.     
  125.     if (invert) 
  126.     {
  127.         UInt8 swap[3];
  128.         UInt8 * opposite;
  129.         
  130.         for (h = 0; h < numRows / 2; h++) 
  131.         {
  132.             ptr = buffer + h * rowBytes;
  133.             opposite = buffer + (numRows - h - 1) * rowBytes;
  134.             
  135.             if (bpp == 4) 
  136.             {
  137.                 ptr++;
  138.                 opposite++;
  139.             }
  140.             
  141.             for (w = 0; w < rowBytes; w += bpp, ptr += bpp, opposite += bpp) 
  142.             {
  143.                 swap[0] = ptr[0];
  144.                 swap[1] = ptr[1];
  145.                 swap[2] = ptr[2];
  146.                 
  147.                 if (needsTransferLookup) 
  148.                 {
  149.                     [self processTriplet:opposite toHere:ptr];
  150.                     [self processTriplet:swap toHere:opposite];
  151.                 }
  152.                 else 
  153.                 {
  154.                     ptr[0] = opposite[0];
  155.                     ptr[1] = opposite[1];
  156.                     ptr[2] = opposite[2];
  157.                     
  158.                     opposite[0] = swap[0];
  159.                     opposite[1] = swap[1];
  160.                     opposite[2] = swap[2];
  161.                 }
  162.             }
  163.         }
  164.     }
  165.     else if (needsTransferLookup) 
  166.     {
  167.         for (h = 0; h < numRows; h++) 
  168.         {
  169.             ptr = buffer + h * rowBytes;
  170.             
  171.             if (bpp == 4) 
  172.                 ptr++;
  173.             
  174.             for (w = 0; w < rowBytes; w += bpp, ptr += bpp) 
  175.                 [self processTriplet:ptr toHere:ptr];
  176.         }
  177.     }
  178. }
  179.  
  180.  
  181. - (void) processImageRep: (NSBitmapImageRep *) imageRep buffer: (UInt8 *) dstBuffer numRows: (long) numRows rowBytes: (long) dstRowBytes bpp: (short) dstBpp
  182. {
  183.     long  w, h;
  184.     UInt8 * src, * dst;
  185.     UInt8 * srcBuffer = [imageRep bitmapData];
  186.     int srcBpp = [imageRep samplesPerPixel];
  187.     int srcRowBytes = [imageRep bytesPerRow];
  188.     int numColumns = dstRowBytes / dstBpp;
  189.     
  190.     for (h = 0; h < numRows; h++) 
  191.     {
  192.         src = srcBuffer + h * srcRowBytes;
  193.         dst = dstBuffer + h * dstRowBytes;
  194.         
  195.         for (w = 0; w < numColumns; w++) 
  196.         {
  197.             if (needsTransferLookup) 
  198.                 [self processTriplet:src toHere:dst];
  199.             else
  200.             {
  201.                 dst[0] = src[0];
  202.                 dst[1] = src[1];
  203.                 dst[2] = src[2];
  204.             }
  205.             
  206.             if (dstBpp == 4 && srcBpp == 4) 
  207.                 dst[3] = src[3];
  208.             
  209.             src += srcBpp;
  210.             dst += dstBpp;
  211.         }
  212.     }
  213. }
  214.  
  215.  
  216. - (void) recalcTransferLookup 
  217. {
  218.     float f,r,g,b;
  219.     short i;
  220.     float sat=((float)saturation)/65536.0f;
  221.     
  222.     for (i=0;i<256;i++) 
  223.     {
  224.         f=((float)i)/255;
  225.         f=pow(f,gamma);                    //Bend to gamma
  226.         f+=brightness;                    //Offset brightness
  227.         f=((f-0.5f)*contrast)+0.5f;            //Scale around 0.5
  228.         f*=255.0f;                    //Scale to [0..255]
  229.         r=f*(sat*redGain+(1.0f-sat));            //Scale to red gain (itself scaled by saturation)
  230.         g=f*(sat*greenGain+(1.0f-sat));            //Scale to green gain (itself scaled by saturation)
  231.         b=f*(sat*blueGain+(1.0f-sat));            //Scale to blue gain (itself scaled by saturation)
  232.         redTransferLookup[i]=CLAMP(r,0.0f,255.0f);    //Clamp and set
  233.         greenTransferLookup[i]=CLAMP(g,0.0f,255.0f);    //Clamp and set
  234.         blueTransferLookup[i]=CLAMP(b,0.0f,255.0f);;    //Clamp and set
  235.     }
  236.     
  237.     needsTransferLookup=(gamma!=1.0f)||(brightness!=0.0f)||(contrast!=1.0f)
  238.         ||(saturation!=65536)||(redGain!=1.0f)||(greenGain!=1.0f)||(blueGain!=1.0f);
  239. }
  240.  
  241. @end
  242.