]> code.delx.au - gnu-emacs/blobdiff - src/nsimage.m
Merge changes from emacs-23 branch.
[gnu-emacs] / src / nsimage.m
index 42b818441517393b90db0ac6d844752e8bbea3b3..a42950d1f5206ec96fb6f9c5b616c1233cf07594 100644 (file)
@@ -1,13 +1,13 @@
 /* Image support for the NeXT/Open/GNUstep and MacOSX window system.
-   Copyright (C) 1989, 1992, 1993, 1994, 2005, 2006, 2008,
-   Free Software Foundation, Inc.
+   Copyright (C) 1989, 1992, 1993, 1994, 2005, 2006, 2008, 2009, 2010
+     Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
-GNU Emacs is free software; you can redistribute it and/or modify
+GNU Emacs is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3, or (at your option)
-any later version.
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -15,19 +15,21 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
+/*
 Originally by Carl Edman
 Updated by Christian Limpach (chris@nice.ch)
 OpenStep/Rhapsody port by Scott Bender (sbender@harmony-ds.com)
 MacOSX/Aqua port by Christophe de Dinechin (descubes@earthlink.net)
 GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu)
-
 */
 
-#include "config.h"
+/* This should be the first include, as it may set up #defines affecting
+   interpretation of even the system includes. */
+#include <config.h>
+#include <setjmp.h>
+
 #include "lisp.h"
 #include "dispextern.h"
 #include "nsterm.h"
@@ -81,19 +83,21 @@ int
 ns_load_image (struct frame *f, struct image *img,
                Lisp_Object spec_file, Lisp_Object spec_data)
 {
-  NSTRACE (ns_load_image);
-
-  EmacsImage *eImg;
+  EmacsImage *eImg = nil;
   NSSize size;
 
-  if (NILP (spec_data))
+  NSTRACE (ns_load_image);
+
+  if (STRINGP (spec_file))
     {
       eImg = [EmacsImage allocInitFromFile: spec_file];
     }
-  else
+  else if (STRINGP (spec_data))
     {
-      NSData *data = [NSData dataWithBytes: XSTRING (spec_data)->data
-                                    length: SBYTES (spec_data)];
+      NSData *data;
+
+      data = [NSData dataWithBytes: SDATA (spec_data)
+                           length: SBYTES (spec_data)];
       eImg = [[EmacsImage alloc] initWithData: data];
       [eImg setPixmapData];
     }
@@ -162,6 +166,7 @@ static EmacsImage *ImageList = nil;
 + allocInitFromFile: (Lisp_Object)file
 {
   EmacsImage *image = ImageList;
+  NSImageRep *imgRep;
   Lisp_Object found;
 
   /* look for an existing image of the same name */
@@ -184,12 +189,18 @@ static EmacsImage *ImageList = nil;
   image = [[EmacsImage alloc] initByReferencingFile:
                      [NSString stringWithUTF8String: SDATA (found)]];
 
-  if ([image bestRepresentationForDevice: nil] == nil)
+  imgRep = [image bestRepresentationForDevice: nil];
+  if (imgRep == nil)
     {
       [image release];
       return nil;
     }
 
+  /* The next two lines cause the DPI of the image to be ignored.
+     This seems to be the behavior users expect. */
+  [image setScalesWhenResized: YES];
+  [image setSize: NSMakeSize([imgRep pixelsWide], [imgRep pixelsHigh])];
+
   [image setName: [NSString stringWithUTF8String: SDATA (file)]];
   [image reference];
   ImageList = [image imageListSetNext: ImageList];
@@ -306,9 +317,9 @@ static EmacsImage *ImageList = nil;
 
   [self addRepresentation: bmRep];
 
-  bzero (planes[0], w*h);
-  bzero (planes[1], w*h);
-  bzero (planes[2], w*h);
+  memset (planes[0], 0, w*h);
+  memset (planes[1], 0, w*h);
+  memset (planes[2], 0, w*h);
   [self setXBMColor: [NSColor blackColor]];
   return self;
 }
@@ -321,7 +332,7 @@ static EmacsImage *ImageList = nil;
   NSSize s = [self size];
   int len = (int) s.width * s.height;
   unsigned char *planes[5];
-  float r, g, b, a;
+  CGFloat r, g, b, a;
   NSColor *rgbColor;
 
   if (bmRep == nil || color == nil)
@@ -370,7 +381,7 @@ static EmacsImage *ImageList = nil;
 
   [bmRep getBitmapDataPlanes: pixmapData];
   for (i =0; i<4; i++)
-    bzero (pixmapData[i], width*height);
+    memset (pixmapData[i], 0, width*height);
   [self addRepresentation: bmRep];
   return self;
 }
@@ -392,6 +403,12 @@ static EmacsImage *ImageList = nil;
 
           if ([bmRep numberOfPlanes] >= 3)
               [bmRep getBitmapDataPlanes: pixmapData];
+
+          /* The next two lines cause the DPI of the image to be ignored.
+             This seems to be the behavior users expect. */
+          [self setScalesWhenResized: YES];
+          [self setSize: NSMakeSize([bmRep pixelsWide], [bmRep pixelsHigh])];
+
           break;
         }
     }
@@ -417,7 +434,7 @@ static EmacsImage *ImageList = nil;
   else if (onTiger)
     {
       NSColor *color = [bmRep colorAtX: x y: y];
-      float r, g, b, a;
+      CGFloat r, g, b, a;
       [color getRed: &r green: &g blue: &b alpha: &a];
       return ((int)(a * 255.0) << 24)
         | ((int)(r * 255.0) << 16) | ((int)(g * 255.0) << 8)
@@ -445,7 +462,8 @@ static EmacsImage *ImageList = nil;
   else if (onTiger)
     {
       [bmRep setColor:
-               [NSColor colorWithCalibratedRed: r green: g blue: b alpha: a]
+               [NSColor colorWithCalibratedRed: (r/255.0) green: (g/255.0)
+                                          blue: (b/255.0) alpha: (a/255.0)]
                   atX: x y: y];
     }
 }