@@ -42,12 +42,14 @@ static void DetermineType(const char *picfilePath, bool &isJPEG, bool &isPNG) {
42
42
}
43
43
}
44
44
45
- static char *
46
- DeriveNewPath (const char *filePath, PicPrefs myPicPrefs, char *newpath) {
45
+ static char *DeriveNewPath (const char *filePath,
46
+ PicPrefs myPicPrefs,
47
+ char *newpath,
48
+ size_t newpath_len) {
47
49
const char *suffix = strrchr (filePath, ' .' );
48
50
49
51
size_t filepath_len = strlen (filePath);
50
- memset (newpath, 0 , MAXPATHLEN + 1 );
52
+ memset (newpath, 0 , newpath_len );
51
53
size_t base_len = filepath_len - strlen (suffix);
52
54
memcpy (newpath, filePath, base_len);
53
55
memcpy (newpath + base_len, " -resized-" , 9 );
@@ -74,11 +76,44 @@ static void DetermineType(const char *picfilePath, bool &isJPEG, bool &isPNG) {
74
76
return newpath;
75
77
}
76
78
79
+ static NSImage *DoResize (NSImage *sourceImage, NSSize newSize) {
80
+ if (!sourceImage.isValid ) {
81
+ return nil ;
82
+ }
83
+
84
+ NSBitmapImageRep *rep = [[NSBitmapImageRep alloc ]
85
+ initWithBitmapDataPlanes: NULL
86
+ pixelsWide: newSize.width
87
+ pixelsHigh: newSize.height
88
+ bitsPerSample: 8
89
+ samplesPerPixel: 4
90
+ hasAlpha: YES
91
+ isPlanar: NO
92
+ colorSpaceName: NSCalibratedRGBColorSpace
93
+ bytesPerRow: 0
94
+ bitsPerPixel: 0 ];
95
+ rep.size = newSize;
96
+
97
+ [NSGraphicsContext saveGraphicsState ];
98
+ [NSGraphicsContext
99
+ setCurrentContext: [NSGraphicsContext
100
+ graphicsContextWithBitmapImageRep: rep]];
101
+ [sourceImage drawInRect: NSMakeRect (0 , 0 , newSize.width, newSize.height)
102
+ fromRect: NSZeroRect
103
+ operation: NSCompositingOperationCopy
104
+ fraction: 1.0 ];
105
+ [NSGraphicsContext restoreGraphicsState ];
106
+
107
+ NSImage *newImage = [[NSImage alloc ] initWithSize: newSize];
108
+ [newImage addRepresentation: rep];
109
+ return newImage;
110
+ }
111
+
77
112
bool ResizeGivenImage (const char *filePath,
78
113
PicPrefs myPicPrefs,
79
- char *resized_path) {
114
+ char *resized_path,
115
+ size_t resized_path_len) {
80
116
bool resize = false ;
81
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc ] init ];
82
117
83
118
NSImage *source = [[NSImage alloc ]
84
119
initWithContentsOfFile: [NSString stringWithUTF8String: filePath]];
@@ -179,37 +214,23 @@ bool ResizeGivenImage(const char *filePath,
179
214
NSSize size = NSMakeSize (hmax, vmax);
180
215
181
216
if (resize) {
182
- [NSApplication sharedApplication ];
183
- [[NSGraphicsContext currentContext ]
184
- setImageInterpolation: NSImageInterpolationHigh];
185
-
186
- [source setSize: size];
217
+ NSImage *image = DoResize (source, size);
187
218
188
- NSImage *image = [[ NSImage alloc ] initWithSize: size ];
189
- [image lockFocus ];
219
+ NSData *imageData = [image TIFFRepresentation ];
220
+ NSBitmapImageRep *bitmap = [ NSBitmapImageRep imageRepWithData: imageData ];
190
221
191
- NSEraseRect (destinationRect);
192
- [source drawInRect: destinationRect
193
- fromRect: destinationRect
194
- operation: NSCompositingOperationCopy
195
- fraction: 1.0 ];
196
-
197
- NSBitmapImageRep *bitmap =
198
- [[NSBitmapImageRep alloc ] initWithFocusedViewRect: destinationRect];
199
222
NSBitmapImageFileType filetype;
200
223
NSDictionary *props;
201
224
202
225
if ((isPNG && !myPicPrefs.allJPEG ) || myPicPrefs.allPNG ) {
203
226
filetype = NSBitmapImageFileTypePNG;
204
227
props = nil ;
205
-
206
228
} else {
207
229
filetype = NSBitmapImageFileTypeJPEG;
208
230
props = [NSDictionary dictionaryWithObject: [NSNumber numberWithFloat: 0.7 ]
209
231
forKey: NSImageCompressionFactor ];
210
232
}
211
233
NSData *data = [bitmap representationUsingType: filetype properties: props];
212
-
213
234
unsigned dataLength = [data length ]; // holds the file length
214
235
215
236
int iter = 0 ;
@@ -227,21 +248,21 @@ bool ResizeGivenImage(const char *filePath,
227
248
}
228
249
}
229
250
230
- [bitmap release ];
231
- NSString *outFile = [NSString
232
- stringWithUTF8String: DeriveNewPath (filePath, myPicPrefs, resized_path)];
233
- // NSLog(outFile);
251
+ NSString *outFile =
252
+ [NSString stringWithUTF8String: DeriveNewPath (filePath,
253
+ myPicPrefs,
254
+ resized_path,
255
+ resized_path_len)];
234
256
[[NSFileManager defaultManager ] createFileAtPath: outFile
235
257
contents: data
236
258
attributes: nil ];
237
259
238
- [image unlockFocus ];
239
260
[image release ];
261
+ [bitmap release ];
240
262
memcpy (resized_path,
241
263
[outFile cStringUsingEncoding: NSUTF8StringEncoding],
242
264
[outFile lengthOfBytesUsingEncoding: NSUTF8StringEncoding]);
243
265
}
244
266
[source release ];
245
- [pool release ];
246
267
return resize;
247
268
}
0 commit comments