home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 114 / macaddict114.cdr / Software / Utilities / macam.0.8.4.dmg / macam sources / utilities / RGBScaler.m < prev    next >
Encoding:
Text File  |  2005-08-14  |  6.3 KB  |  235 lines

  1. /*
  2.  RGBScaler.m - image blitter with linear interpolation scaling and RGB/RGBA conversion 
  3.  
  4.  Copyright (C) 2002 Matthias Krauss (macam@matthias-krauss.de)
  5.  
  6.  This program is free software; you can redistribute it and/or modify
  7.  it under the terms of the GNU General Public License as published by
  8.  the Free Software Foundation; either version 2 of the License, or
  9.  (at your option) any later version.
  10.  
  11.  This program is distributed in the hope that it will be useful,
  12.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  GNU General Public License for more details.
  15.  
  16.  You should have received a copy of the GNU General Public License
  17.  along with this program; if not, write to the Free Software
  18.  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20.  $Id: RGBScaler.m,v 1.2 2005/08/14 05:21:30 hxr Exp $
  21.  */
  22.  
  23. #import "RGBScaler.h"
  24.  
  25.  
  26. @interface RGBScaler (Private)
  27.  
  28. //Customized blitters
  29. - (void) convertRGB: (unsigned char*)src toRGB: (unsigned char*)dst;
  30. - (void) convertRGBA:(unsigned char*)src toRGB: (unsigned char*)dst;
  31. - (void) convertRGB: (unsigned char*)src toRGBA:(unsigned char*)dst;
  32. - (void) convertRGBA:(unsigned char*)src toRGBA:(unsigned char*)dst;
  33.  
  34. @end
  35.  
  36.  
  37. @implementation RGBScaler
  38.  
  39. - (id) init {
  40.     self=[super init];
  41.     internalDst=NULL;
  42.     tmpRow1=NULL;
  43.     tmpRow2=NULL;
  44.     if (![self setSourceWidth:1 height:1 bytesPerPixel:3 rowBytes:0]) {
  45.         [self dealloc];
  46.         return NULL;
  47.     }
  48.     if (![self setDestinationWidth:1 height:1 bytesPerPixel:3 rowBytes:0]) {
  49.         [self dealloc];
  50.         return NULL;
  51.     }
  52.     return self;
  53. }
  54.  
  55. - (void) dealloc 
  56. {
  57.     if (internalDst) 
  58.         free(internalDst); 
  59.     internalDst=NULL;
  60.     
  61.     if (tmpRow1) 
  62.         free(tmpRow1); 
  63.     tmpRow1=NULL; 
  64.     tmpRow2=NULL;
  65.     
  66.     [super dealloc];
  67. }
  68.  
  69. - (BOOL) setSourceWidth:(int)sw height:(int)sh bytesPerPixel:(int)sbpp rowBytes:(int)srb {
  70.     if ((sbpp<3)||(sbpp>4)) return NO;
  71.     if ((sw<1)||(sh<1)) return NO;
  72.     if (srb==0) srb=sw*sbpp;        //set default for srb=0
  73.     if (srb<sbpp*sw) return NO;
  74.     srcWidth=sw;
  75.     srcHeight=sh;
  76.     srcBPP=sbpp;
  77.     srcRB=srb;
  78.     return YES;
  79. }
  80.  
  81. - (int) sourceWidth { return srcWidth; }
  82. - (int) sourceHeight { return srcHeight; }
  83. - (int) sourceBytesPerPixel { return srcBPP; }
  84. - (int) sourceRowBytes { return srcRB; }
  85.  
  86. - (BOOL) setDestinationWidth:(int)dw height:(int)dh bytesPerPixel:(int)dbpp rowBytes:(int)drb {
  87.     if ((dbpp<3)||(dbpp>4)) return NO;
  88.     if ((dw<1)||(dh<1)) return NO;
  89.     if (drb==0) drb=dw*dbpp;        //set default for srb=0
  90.     if (drb<dbpp*dw) return NO;
  91.  
  92.     if (dstWidth!=dw) {    //row length has changed -> release buffers
  93.         if (tmpRow1) free(tmpRow1); tmpRow1=NULL; tmpRow2=NULL;
  94.     }
  95.     if (dstHeight*dstRB!=dh*drb) {    //image length has changed -> release buffer
  96.         if (internalDst) free(internalDst); internalDst=NULL;
  97.     }
  98.     dstWidth=dw;
  99.     dstHeight=dh;
  100.     dstBPP=dbpp;
  101.     dstRB=drb;
  102.     //Allocate new buffers
  103.     if (!tmpRow1) {
  104.         tmpRow1=malloc(dstWidth*4*2);
  105.         tmpRow2=tmpRow1+4*dstWidth;
  106.     }
  107.     if (!internalDst) internalDst=malloc(dstHeight*dstRB);
  108.     return ((tmpRow1)&&(internalDst));
  109. }
  110.  
  111. - (int) destinationWidth { return dstWidth; }
  112. - (int) destinationHeight { return dstHeight; }
  113. - (int) destinationBytesPerPixel { return dstBPP; }
  114. - (int) destinationRowBytes { return dstRB; }
  115.  
  116. - (unsigned char*) convert:(unsigned char*)src {
  117.     [self convert:src to:internalDst];
  118.     return internalDst;
  119. }
  120.  
  121. //This is where the heavy metal starts
  122. #define RGBSCALER_MACROS
  123. #include "RGBScalerIncluded.h"
  124. #undef RGBSCALER_MACROS
  125.  
  126. //Row blitting c functions
  127. inline void ScaleRowRGBToRGB (unsigned char* src, unsigned char* dst, int srcLength, int dstLength)
  128. #define SRC_RGB
  129. #define DST_RGB
  130. #define SCALE_ROW
  131. #include "RGBScalerIncluded.h"
  132. #undef SRC_RGB
  133. #undef DST_RGB
  134. #undef SCALE_ROW
  135.  
  136. inline void ScaleRowRGBAToRGB (unsigned char* src, unsigned char* dst, int srcLength, int dstLength)
  137. #define SRC_RGBA
  138. #define DST_RGB
  139. #define SCALE_ROW
  140. #include "RGBScalerIncluded.h"
  141. #undef SRC_RGBA
  142. #undef DST_RGB
  143. #undef SCALE_ROW
  144.  
  145. inline void ScaleRowRGBToRGBA (unsigned char* src, unsigned char* dst, int srcLength, int dstLength)
  146. #define SRC_RGB
  147. #define DST_RGBA
  148. #define SCALE_ROW
  149. #include "RGBScalerIncluded.h"
  150. #undef SRC_RGB
  151. #undef DST_RGBA
  152. #undef SCALE_ROW
  153.  
  154. inline void ScaleRowRGBAToRGBA (unsigned char* src, unsigned char* dst, int srcLength, int dstLength)
  155. #define SRC_RGBA
  156. #define DST_RGBA
  157. #define SCALE_ROW
  158. #include "RGBScalerIncluded.h"
  159. #undef SRC_RGBA
  160. #undef DST_RGBA
  161. #undef SCALE_ROW
  162.  
  163. //2-Row mixing functions
  164. inline void BlendRowsToRGB (unsigned char* r1,unsigned char* r2,int w1, int w2, int len, unsigned char* dst)
  165. #define DST_RGB
  166. #define BLEND_ROWS
  167. #include "RGBScalerIncluded.h"
  168. #undef DST_RGB
  169. #undef BLEND_ROWS
  170.  
  171.  
  172. //2-Row mixing functions
  173. inline void BlendRowsToRGBA (unsigned char* r1,unsigned char* r2,int w1, int w2, int len, unsigned char* dst)
  174. #define DST_RGBA
  175. #define BLEND_ROWS
  176. #include "RGBScalerIncluded.h"
  177. #undef DST_RGBA
  178. #undef BLEND_ROWS
  179.  
  180.  
  181. - (void) convert:(unsigned char*)src to:(unsigned char*)dst {
  182.     //Dispatch to optimized blitters
  183.     int choice=((srcBPP==4)?1:0)+((dstBPP==4)?2:0);
  184.     switch (choice) {
  185.         case 0: [self convertRGB:src  toRGB:dst ]; break;
  186.         case 1: [self convertRGBA:src toRGB:dst ]; break;
  187.         case 2: [self convertRGB:src  toRGBA:dst]; break;
  188.         case 3: [self convertRGBA:src toRGBA:dst]; break;
  189.         default: NSLog(@"MyRGBScaler: convert in invalid mode:%i",choice); break;
  190.     }
  191. }
  192.  
  193. - (void) convertRGB: (unsigned char*)src toRGB: (unsigned char*)dst
  194. #define SRC_RGB 1
  195. #define DST_RGB 1
  196. #define SCALE_IMAGE 1
  197. #include "RGBScalerIncluded.h"
  198. #undef SRC_RGB
  199. #undef DST_RGB
  200. #undef SCALE_IMAGE
  201.  
  202.  
  203. - (void) convertRGBA:(unsigned char*)src toRGB: (unsigned char*)dst
  204. #define SRC_RGBA 1
  205. #define DST_RGB 1
  206. #define SCALE_IMAGE 1
  207. #include "RGBScalerIncluded.h"
  208. #undef SRC_RGBA
  209. #undef DST_RGB
  210. #undef SCALE_IMAGE
  211.  
  212.     
  213. - (void) convertRGB: (unsigned char*)src toRGBA:(unsigned char*)dst
  214. #define SRC_RGB 1
  215. #define DST_RGBA 1
  216. #define SCALE_IMAGE 1
  217. #include "RGBScalerIncluded.h"
  218. #undef SRC_RGB
  219. #undef DST_RGBA
  220. #undef SCALE_IMAGE
  221.  
  222.  
  223. - (void) convertRGBA:(unsigned char*)src toRGBA:(unsigned char*)dst
  224. #define SRC_RGBA 1
  225. #define DST_RGBA 1
  226. #define SCALE_IMAGE 1
  227. #include "RGBScalerIncluded.h"
  228. #undef SRC_RGBA
  229. #undef DST_RGBA
  230. #undef SCALE_IMAGE
  231.  
  232.  
  233.  
  234. @end
  235.