@@ -42,12 +42,14 @@ static void DetermineType(const char *picfilePath, bool &isJPEG, bool &isPNG) {
4242 }
4343}
4444
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) {
4749 const char *suffix = strrchr (filePath, ' .' );
4850
4951 size_t filepath_len = strlen (filePath);
50- memset (newpath, 0 , MAXPATHLEN + 1 );
52+ memset (newpath, 0 , newpath_len );
5153 size_t base_len = filepath_len - strlen (suffix);
5254 memcpy (newpath, filePath, base_len);
5355 memcpy (newpath + base_len, " -resized-" , 9 );
@@ -74,11 +76,44 @@ static void DetermineType(const char *picfilePath, bool &isJPEG, bool &isPNG) {
7476 return newpath;
7577}
7678
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+
77112bool ResizeGivenImage (const char *filePath,
78113 PicPrefs myPicPrefs,
79- char *resized_path) {
114+ char *resized_path,
115+ size_t resized_path_len) {
80116 bool resize = false ;
81- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc ] init ];
82117
83118 NSImage *source = [[NSImage alloc ]
84119 initWithContentsOfFile: [NSString stringWithUTF8String: filePath]];
@@ -179,37 +214,23 @@ bool ResizeGivenImage(const char *filePath,
179214 NSSize size = NSMakeSize (hmax, vmax);
180215
181216 if (resize) {
182- [NSApplication sharedApplication ];
183- [[NSGraphicsContext currentContext ]
184- setImageInterpolation: NSImageInterpolationHigh];
185-
186- [source setSize: size];
217+ NSImage *image = DoResize (source, size);
187218
188- NSImage *image = [[ NSImage alloc ] initWithSize: size ];
189- [image lockFocus ];
219+ NSData *imageData = [image TIFFRepresentation ];
220+ NSBitmapImageRep *bitmap = [ NSBitmapImageRep imageRepWithData: imageData ];
190221
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];
199222 NSBitmapImageFileType filetype;
200223 NSDictionary *props;
201224
202225 if ((isPNG && !myPicPrefs.allJPEG ) || myPicPrefs.allPNG ) {
203226 filetype = NSBitmapImageFileTypePNG;
204227 props = nil ;
205-
206228 } else {
207229 filetype = NSBitmapImageFileTypeJPEG;
208230 props = [NSDictionary dictionaryWithObject: [NSNumber numberWithFloat: 0.7 ]
209231 forKey: NSImageCompressionFactor ];
210232 }
211233 NSData *data = [bitmap representationUsingType: filetype properties: props];
212-
213234 unsigned dataLength = [data length ]; // holds the file length
214235
215236 int iter = 0 ;
@@ -227,21 +248,21 @@ bool ResizeGivenImage(const char *filePath,
227248 }
228249 }
229250
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)];
234256 [[NSFileManager defaultManager ] createFileAtPath: outFile
235257 contents: data
236258 attributes: nil ];
237259
238- [image unlockFocus ];
239260 [image release ];
261+ [bitmap release ];
240262 memcpy (resized_path,
241263 [outFile cStringUsingEncoding: NSUTF8StringEncoding],
242264 [outFile lengthOfBytesUsingEncoding: NSUTF8StringEncoding]);
243265 }
244266 [source release ];
245- [pool release ];
246267 return resize;
247268}
0 commit comments