Raster.m
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:8k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1. // Swarm library. Copyright © 2003 Swarm Development Group.
  2. //
  3. // Author: Scott Christley
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful, but
  11. // WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. // General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  18. // USA
  19. // 
  20. // The Swarm Development Group can be reached via our website at:
  21. // http://www.swarm.org/
  22. #include <AppKit/AppKit.h>
  23. #include "Raster.h"
  24. #include <space/space.h>
  25. @implementation Raster
  26. - initWithFrame:(NSRect)aRect pointSize:(NSSize)aSize
  27. {
  28.   fprintf(stderr, "Raster initWithFrame:pointSize:n");
  29.   [super initWithFrame:aRect];
  30.   image = nil;
  31.   imageRep = nil;
  32.   rasterOrigin.x = 0;
  33.   rasterOrigin.y = 0;
  34.   pointSize = aSize;
  35.   displayList = [NSMutableArray new];
  36.   backgroundColor = [[NSColor blackColor] retain];
  37.   fprintf(stderr, "done Raster initWithFrame:pointSize:n");
  38.   return self;
  39. }
  40. - initWithFrame:(NSRect)aRect
  41. {
  42.   // default size of points are 3 by 3, i.e. 1/4 inch square
  43.   return [self initWithFrame:aRect pointSize:NSMakeSize(3, 3)];
  44. }
  45. - (void)determineWindowMinMaxSize
  46. {
  47. #if 1
  48.   // We need to have displays attached
  49.   if ([displayList count] == 0)
  50.     return;
  51.   // If we are in a window and the content view for that window
  52.   // Then set the min and max size of the window.
  53.   // If we are not the content view then there isn't much we can do.
  54.   id w = [self window];
  55.   if (w)
  56.     {
  57.       id v = [w contentView];
  58.       if (v == self)
  59. {
  60.   // Just pick first one for now, currently assume same size
  61.   // grids under each display
  62.   id aDisplay = [displayList objectAtIndex: 0];
  63.   id g = [aDisplay discrete2d];
  64.   unsigned worldXsize = [g getSizeX];
  65.   unsigned worldYsize = [g getSizeY];
  66.   NSRect aRect = NSMakeRect(0, 0, worldXsize * pointSize.width,
  67.     worldYsize * pointSize.height);
  68.   NSRect r = [NSWindow frameRectForContentRect: aRect
  69.        styleMask: [w styleMask]];
  70.   // add 2 just to give a little border
  71.   r.size.width += 2;
  72.   r.size.height += 2;
  73.   [w setMaxSize: r.size];
  74.   // If the current size is larger, then resize the window
  75.   aRect = [w frame];
  76.   if ((aRect.size.width > r.size.width)
  77.       || (aRect.size.height > r.size.height))
  78.     [w setFrame: aRect display: NO];
  79. }
  80.     }
  81. #endif
  82. }
  83. - (void)setRasterOrigin:(NSPoint)aPoint
  84. {
  85.   rasterOrigin = aPoint;
  86.   [self determineWindowMinMaxSize];
  87.   // redisplay
  88. }
  89. - (void)setPointSize:(NSSize)aSize
  90. {
  91.   pointSize = aSize;
  92.   [self determineWindowMinMaxSize];
  93.   // redisplay
  94. }
  95. - (NSImage *)image
  96. {
  97.     return image;
  98. }
  99. - (void)releaseImage
  100. {
  101.   if (image)
  102.     {
  103.       [image release];
  104.       image = nil;
  105.     }
  106.   if (imageRep)
  107.     {
  108.       [imageRep release];
  109.       imageRep = nil;
  110.     }
  111. }
  112. - (void)createImage
  113. {
  114.   NSRect aRect = [self bounds];
  115.   [self releaseImage];
  116. #if 1
  117.   image = [[NSImage alloc] initWithSize:aRect.size];
  118.   imageRep = [[NSBitmapImageRep alloc]
  119.   initWithBitmapDataPlanes:NULL
  120.   pixelsWide:aRect.size.width
  121.   pixelsHigh:aRect.size.height
  122.   bitsPerSample:8
  123.   samplesPerPixel:3
  124.   hasAlpha:NO
  125.   isPlanar:NO
  126.   colorSpaceName:NSDeviceRGBColorSpace
  127.   bytesPerRow:aRect.size.width*3
  128.   bitsPerPixel:24];
  129.   [image addRepresentation:imageRep];
  130. #endif
  131. #if 0
  132.   image = [[NSImage alloc] initWithSize:aRect.size];
  133.   imageRep = [[NSBitmapImageRep alloc]
  134.   initWithBitmapDataPlanes:NULL
  135.   pixelsWide:240
  136.   pixelsHigh:240
  137.   bitsPerSample:8
  138.   samplesPerPixel:3
  139.   hasAlpha:NO
  140.   isPlanar:NO
  141.   colorSpaceName:NSDeviceRGBColorSpace
  142.   bytesPerRow:aRect.size.width*3
  143.   bitsPerPixel:24];
  144.   [image addRepresentation:imageRep];
  145. #endif
  146. }
  147. - (void) resizeWithOldSuperviewSize: (NSSize)oldSize
  148. {
  149.   fprintf(stderr, "RasterView got resizedn");
  150.   [super resizeWithOldSuperviewSize: oldSize];
  151.   [self determineWindowMinMaxSize];
  152.   // release the image if we get resized
  153.   [self releaseImage];
  154. }
  155. - (void)addDisplay: aDisplay
  156. {
  157.   if (aDisplay)
  158.     {
  159.       fprintf(stderr, "addDisplay:n");
  160.       [displayList addObject: aDisplay];
  161.       [self determineWindowMinMaxSize];
  162.     }
  163. }
  164. - (void)removeDisplay: aDisplay
  165. {
  166.   if (aDisplay)
  167.     {
  168.       fprintf(stderr, "removeDisplay:n");
  169.       [displayList removeObject: aDisplay];
  170.       [self determineWindowMinMaxSize];
  171.     }
  172. }
  173. - (void)mouseDown:(NSEvent *)theEvent
  174. {
  175.   fprintf(stderr, "mouseDown:n");
  176. }
  177. - (void)mouseUp:(NSEvent *)theEvent
  178. {
  179.   fprintf(stderr, "mouseUp:n");
  180. }
  181. - (void)rightMouseDown:(NSEvent *)theEvent
  182. {
  183.   fprintf(stderr, "rightMouseDown:n");
  184. }
  185. - (void)rightMouseUp:(NSEvent *)theEvent
  186. {
  187.   NSPoint p = [theEvent locationInWindow];
  188.   fprintf(stderr, "rightMouseUp: %f %fn", p.x, p.y);
  189. }
  190. - (void)drawRect:(NSRect)aRect
  191. {
  192. #if 0
  193.   if (!backgroundColor)
  194.     backgroundColor = [[NSColor blackColor] retain];
  195. #endif
  196.   [backgroundColor set];
  197.   PSrectfill(aRect.origin.x, aRect.origin.y,
  198.      aRect.size.width, aRect.size.height);
  199.   if (!image)
  200.     [self createImage];
  201. #if 1
  202.   if (image)
  203.     {
  204.       NSEnumerator *e = [displayList objectEnumerator];
  205.       id aDisplay;
  206.       [image lockFocus];
  207.       [backgroundColor set];
  208.       PSrectfill(aRect.origin.x, aRect.origin.y,
  209.  aRect.size.width, aRect.size.height);
  210.       while ((aDisplay = [e nextObject]))
  211. {
  212.   id g = [aDisplay discrete2d];
  213.   unsigned worldXsize = [g getSizeX];
  214.   unsigned worldYsize = [g getSizeY];
  215.   int x, y;
  216.   // Calculate the size and position of each discrete dot
  217.   // on the raster.  Deal in integer units instead of float.
  218.   NSRect b = [self bounds];
  219.   int gridXsize, gridYsize;
  220.   int szw = b.size.width;
  221.   int szh = b.size.height;
  222.   int rw = b.size.width / pointSize.width;
  223.   int rh = b.size.height / pointSize.height;
  224.   int xfo, yfo;
  225.   int newX, newY;
  226.   // Pick the smaller grid, either the world or the raster
  227.   if (worldXsize > rw)
  228.     gridXsize = rw;
  229.   else
  230.     gridXsize = worldXsize;
  231.   if (worldYsize > rh)
  232.     gridYsize = rh;
  233.   else
  234.     gridYsize = worldYsize;
  235.   // Get x, y offsets to center the grid in the view
  236.   xfo = szw % (gridXsize * (int)pointSize.width);
  237.   if (xfo != 0)
  238.     szw -= xfo;
  239.   yfo = szh % (gridYsize * (int)pointSize.height);
  240.   if (yfo != 0)
  241.     szh -= yfo;
  242.   xfo = xfo / 2;
  243.   yfo = yfo / 2;
  244. #if 1
  245.   for (x = 0; x < gridXsize; ++x)
  246.     for (y = 0; y < gridYsize; ++y)
  247.       {
  248. NSRect aRect;
  249. aRect.origin.x = x * pointSize.width + xfo;
  250. aRect.origin.y = y * pointSize.height + yfo;
  251. aRect.size.width = pointSize.width;
  252. aRect.size.height = pointSize.width;
  253. [aDisplay displayX:x Y:y inRect:aRect];
  254.       }
  255. #endif
  256. #if 0
  257.   [aDisplay displayOn: image];
  258. #endif
  259. }
  260.       [image unlockFocus];
  261.     }
  262. #endif
  263. #if 1
  264.     [image compositeToPoint:aRect.origin 
  265.    fromRect:aRect
  266.    operation:NSCompositeCopy];
  267. #endif
  268. }
  269. @end
  270. #if 0
  271. #define CALCULATE_FACTORS NSSize sz = [anImage size]; 
  272.   int szw = sz.width; 
  273.   int szh = sz.height; 
  274.   int xfo, yfo; 
  275.   int xf4, yf4, xf1, yf1, xf3, yf3; 
  276.   int newX, newY; 
  277.   xfo = szw % (worldXSize * 4); 
  278.   if (xfo != 0) 
  279.     szw -= xfo; 
  280.   yfo = szh % (worldYSize * 4); 
  281.   if (yfo != 0) 
  282.     szh -= yfo; 
  283.   xfo = xfo / 2; 
  284.   yfo = yfo / 2; 
  285.   xf4 = szw / worldXSize; 
  286.   yf4 = szh / worldYSize; 
  287.   xf1 = xf4 / 4; 
  288.   yf1 = yf4 / 4; 
  289.   xf3 = xf1 * 3; 
  290.   yf3 = yf1 * 3; 
  291.   newX = (xf4 * x) + xfo; 
  292.   newY = (yf4 * y) + yfo;
  293. #endif