1/*
2 Project: Graphos
3 GRImage.m
4
5 Copyright (C) 2015-2018 GNUstep Application Project
6
7 Author: Ing. Riccardo Mottola
8
9 Created: 2015-01-16
10
11 This application is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
15
16 This application is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 Library General Public License for more details.
20
21 You should have received a copy of the GNU General Public
22 License along with this library; if not, write to the Free
23 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24 */
25
26#import "GRImage.h"
27#import "GRImageEditor.h"
28
29
30#import <AppKit/AppKit.h>
31#import "GRObjectEditor.h"
32#import "GRFunctions.h"
33
34@implementation GRImage
35
36- (GRObjectEditor *)allocEditor
37{
38  return [[GRImageEditor alloc] initEditor:self];
39}
40
41/** initializes by using the properties array as defaults */
42- (id)initInView:(GRDocView *)aView
43      zoomFactor:(CGFloat)zf
44      withProperties:(NSDictionary *)properties
45{
46  self = [super initInView:aView zoomFactor:zf withProperties:properties];
47  if(self)
48    {
49      NSData *imgRepData;
50
51      imgRepData = [properties objectForKey:@"imgrepdata"];
52      if (imgRepData)
53        {
54          NSImage *img;
55          NSLog(@"GRImage: we have an image representation");
56
57          img = [[NSImage alloc] initWithData:imgRepData];
58          [self setImage: img];
59          [img release];
60        }
61    }
62
63  return self;
64}
65
66
67/** initializes all parameters from a description dictionary */
68- (id)initFromData:(NSDictionary *)description
69            inView:(GRDocView *)aView
70        zoomFactor:(CGFloat)zf
71{
72  self = [super initFromData:description inView:aView zoomFactor:zf];
73  if(self)
74    {
75      NSData *imgRepData;
76      id obj;
77
78      pos = NSMakePoint([[description objectForKey: @"posx"]  floatValue],
79			[[description objectForKey: @"posy"]  floatValue]);
80      size = NSMakeSize([[description objectForKey: @"width"]  floatValue],
81			[[description objectForKey: @"height"]  floatValue]);
82      bounds = GRMakeBounds(pos.x, pos.y, size.width, size.height);
83
84      rotation = 0;
85      obj = [description objectForKey: @"rotation"];
86      if (obj)
87	rotation = [obj floatValue];
88
89      startControlPoint = [[GRObjectControlPoint alloc] initAtPoint: pos zoomFactor:zf];
90      endControlPoint = [[GRObjectControlPoint alloc] initAtPoint: NSMakePoint(pos.x + size.width, pos.y + size.height) zoomFactor:zf];
91      [self setZoomFactor: zf];
92
93      imgRepData = [description objectForKey:@"imgrepdata"];
94      if (imgRepData)
95        {
96          NSImage *img;
97          NSLog(@"GRImage: we have an image representation");
98
99          img = [[NSImage alloc] initWithData:imgRepData];
100          [self setImage: img];
101          [img release];
102        }
103    }
104  return self;
105}
106
107- (id)copyWithZone:(NSZone *)zone
108{
109  GRImage *objCopy;
110
111  objCopy = [super copyWithZone:zone];
112
113  objCopy->image = [image copy];
114  objCopy->name = [name copy];
115
116  return objCopy;
117}
118
119- (void)dealloc
120{
121  [image release];
122  [name release];
123  [super dealloc];
124}
125
126- (NSDictionary *)objectDescription
127{
128  NSMutableDictionary *dict;
129  NSData *imgData;
130
131  dict = (NSMutableDictionary *)[super objectDescription]; /* we know the superclass actually returns a mutable dict */
132  [dict retain];
133
134  [dict setObject: @"image" forKey: @"type"];
135
136  imgData = [image TIFFRepresentation];
137
138  [dict setObject:imgData forKey:@"imgrepdata"];
139  [dict autorelease];
140  return dict;
141}
142
143/** draws the object and calls the editor to draw itself afterwards */
144- (void)draw
145{
146  NSBezierPath *bzp;
147  CGFloat linew;
148  NSRect drawBounds;
149
150  drawBounds = bounds;
151  linew = linewidth;
152  if ([[NSGraphicsContext currentContext] isDrawingToScreen])
153    {
154      drawBounds = boundsZ;
155      linew = linewidth * zmFactor;
156    }
157
158  bzp = [NSBezierPath bezierPath];
159  [bzp appendBezierPathWithRect:drawBounds];
160  if(filled)
161    {
162      [NSGraphicsContext saveGraphicsState];
163      [fillColor set];
164      [bzp fill];
165      [NSGraphicsContext restoreGraphicsState];
166    }
167
168  [image drawInRect:drawBounds fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
169
170  if(stroked)
171    {
172      [NSGraphicsContext saveGraphicsState];
173      [bzp setLineJoinStyle:linejoin];
174      [bzp setLineCapStyle:linecap];
175      [bzp setLineWidth:linew];
176      [strokeColor set];
177      [bzp stroke];
178      [NSGraphicsContext restoreGraphicsState];
179    }
180
181  if ([[NSGraphicsContext currentContext] isDrawingToScreen])
182    [editor draw];
183}
184
185- (NSImage *)image
186{
187  return image;
188}
189
190- (void)setImage:(NSImage *)img
191{
192  if (img != image)
193    {
194      [image release];
195      image = img;
196      [image retain];
197      originalSize = [image size];
198      originalRatio = originalSize.width / originalSize.height;
199    }
200}
201
202- (float)originalRatio
203{
204  return originalRatio;
205}
206
207@end
208