diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c3e4fe..4fa2c99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,8 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() +option(ASAN "whether to enable ASAN" OFF) + find_program(GIT git) if(GIT) execute_process( @@ -92,3 +94,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") "-framework IOKit" ) endif() + +if (ASAN) + set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address") + set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address") +endif() diff --git a/src/AtomicParsley.h b/src/AtomicParsley.h index 37fa886..c049c28 100644 --- a/src/AtomicParsley.h +++ b/src/AtomicParsley.h @@ -430,7 +430,8 @@ void APar_SupplySelectiveTypeCreatorCodes(const char *inputPath, bool ResizeGivenImage(const char *filePath, PicPrefs myPicPrefs, - char *resized_path); + char *resized_path, + size_t resized_path_len); char *GenreIntToString(int genre); uint8_t StringGenreToInt(const char *genre_string); diff --git a/src/nsimage.mm b/src/nsimage.mm index 983d7cf..199f2c4 100644 --- a/src/nsimage.mm +++ b/src/nsimage.mm @@ -42,12 +42,14 @@ static void DetermineType(const char *picfilePath, bool &isJPEG, bool &isPNG) { } } -static char * -DeriveNewPath(const char *filePath, PicPrefs myPicPrefs, char *newpath) { +static char *DeriveNewPath(const char *filePath, + PicPrefs myPicPrefs, + char *newpath, + size_t newpath_len) { const char *suffix = strrchr(filePath, '.'); size_t filepath_len = strlen(filePath); - memset(newpath, 0, MAXPATHLEN + 1); + memset(newpath, 0, newpath_len); size_t base_len = filepath_len - strlen(suffix); memcpy(newpath, filePath, base_len); memcpy(newpath + base_len, "-resized-", 9); @@ -74,11 +76,44 @@ static void DetermineType(const char *picfilePath, bool &isJPEG, bool &isPNG) { return newpath; } +static NSImage *DoResize(NSImage *sourceImage, NSSize newSize) { + if (!sourceImage.isValid) { + return nil; + } + + NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] + initWithBitmapDataPlanes:NULL + pixelsWide:newSize.width + pixelsHigh:newSize.height + bitsPerSample:8 + samplesPerPixel:4 + hasAlpha:YES + isPlanar:NO + colorSpaceName:NSCalibratedRGBColorSpace + bytesPerRow:0 + bitsPerPixel:0]; + rep.size = newSize; + + [NSGraphicsContext saveGraphicsState]; + [NSGraphicsContext + setCurrentContext:[NSGraphicsContext + graphicsContextWithBitmapImageRep:rep]]; + [sourceImage drawInRect:NSMakeRect(0, 0, newSize.width, newSize.height) + fromRect:NSZeroRect + operation:NSCompositingOperationCopy + fraction:1.0]; + [NSGraphicsContext restoreGraphicsState]; + + NSImage *newImage = [[NSImage alloc] initWithSize:newSize]; + [newImage addRepresentation:rep]; + return newImage; +} + bool ResizeGivenImage(const char *filePath, PicPrefs myPicPrefs, - char *resized_path) { + char *resized_path, + size_t resized_path_len) { bool resize = false; - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSImage *source = [[NSImage alloc] initWithContentsOfFile:[NSString stringWithUTF8String:filePath]]; @@ -179,37 +214,23 @@ bool ResizeGivenImage(const char *filePath, NSSize size = NSMakeSize(hmax, vmax); if (resize) { - [NSApplication sharedApplication]; - [[NSGraphicsContext currentContext] - setImageInterpolation:NSImageInterpolationHigh]; - - [source setSize:size]; + NSImage *image = DoResize(source, size); - NSImage *image = [[NSImage alloc] initWithSize:size]; - [image lockFocus]; + NSData *imageData = [image TIFFRepresentation]; + NSBitmapImageRep *bitmap = [NSBitmapImageRep imageRepWithData:imageData]; - NSEraseRect(destinationRect); - [source drawInRect:destinationRect - fromRect:destinationRect - operation:NSCompositingOperationCopy - fraction:1.0]; - - NSBitmapImageRep *bitmap = - [[NSBitmapImageRep alloc] initWithFocusedViewRect:destinationRect]; NSBitmapImageFileType filetype; NSDictionary *props; if ((isPNG && !myPicPrefs.allJPEG) || myPicPrefs.allPNG) { filetype = NSBitmapImageFileTypePNG; props = nil; - } else { filetype = NSBitmapImageFileTypeJPEG; props = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.7] forKey:NSImageCompressionFactor]; } NSData *data = [bitmap representationUsingType:filetype properties:props]; - unsigned dataLength = [data length]; // holds the file length int iter = 0; @@ -227,21 +248,21 @@ bool ResizeGivenImage(const char *filePath, } } - [bitmap release]; - NSString *outFile = [NSString - stringWithUTF8String:DeriveNewPath(filePath, myPicPrefs, resized_path)]; - // NSLog(outFile); + NSString *outFile = + [NSString stringWithUTF8String:DeriveNewPath(filePath, + myPicPrefs, + resized_path, + resized_path_len)]; [[NSFileManager defaultManager] createFileAtPath:outFile contents:data attributes:nil]; - [image unlockFocus]; [image release]; + [bitmap release]; memcpy(resized_path, [outFile cStringUsingEncoding:NSUTF8StringEncoding], [outFile lengthOfBytesUsingEncoding:NSUTF8StringEncoding]); } [source release]; - [pool release]; return resize; } diff --git a/src/parsley.cpp b/src/parsley.cpp index b218f44..c42c80d 100644 --- a/src/parsley.cpp +++ b/src/parsley.cpp @@ -2938,9 +2938,13 @@ void APar_MetaData_atomArtwork_Set(const char *artworkPath, // any way myPicturePrefs = APar_ExtractPicPrefs(env_PicOptions); - char *resized_filepath = (char *)calloc(1, sizeof(char) * MAXPATHLEN + 1); + size_t resized_filepath_len = MAXPATHLEN + 1; + char *resized_filepath = (char *)calloc(1, resized_filepath_len); - if (ResizeGivenImage(artworkPath, myPicturePrefs, resized_filepath)) { + if (ResizeGivenImage(artworkPath, + myPicturePrefs, + resized_filepath, + resized_filepath_len)) { APar_MetaData_atomArtwork_Init(desiredAtom->AtomicNumber, resized_filepath);