Colored Artifacts while transforming PDF to NSImageRep (png)

123 Views Asked by At

I want to convert different PDF pages to an png. After that I iterate through all pixels to search colored pixel. The main goal is to get the colored pages of the PDF. For the most pages it runs great. But on some pages I have colored artifacts in red (left beside a letter) and blue (right beside a letter).
This is the original from the PDF: The original in the PDF
(source: brebook.de)


And this is the converted letter in the png:

The original in the PDF

How can I prevent this ugly artifacts. I can't use my whole idea with this colored pixel.
This is the code which is converting the single page to a png:

//Path for saving colored Pages
NSURL *savePath = [self.homePath URLByAppendingPathComponent:@"brebook_temp/"];

//PDFDocument *thePDF = [[PDFDocument alloc] initWithURL:self.filename];
NSPDFImageRep *img = [NSPDFImageRep imageRepWithContentsOfURL:self.filename];

int coloredPages = 0;

for(int i = 0; i < self.getNumberOfPages; i++){
    @autoreleasepool {
        //set current page label to current page
        self.currentPage = i + 1;
        
        //set current page to i
        [img setCurrentPage:i];
        
        //create a new NSImage instance
        NSImage *singlePage = [NSImage new];
        
        //set actuall page
        [singlePage addRepresentation:img];
        
        //get "old" size
        NSSize oldSize = [singlePage size];
        
        //edit the size to 72dpi
        NSSize newSize;
        newSize.width = oldSize.width * 150/72;
        newSize.height = oldSize.height * 150/72;
        
        //make new image
        NSImage *resizedImage = [[NSImage alloc] initWithSize: NSMakeSize(newSize.width, newSize.height)];
        
        //write into new image
        [resizedImage lockFocus];
        [singlePage drawInRect: NSMakeRect(0, 0, newSize.width, newSize.height) fromRect: NSMakeRect(0, 0, oldSize.width, oldSize.height) operation: NSCompositeSourceOver fraction: 1.0];
        [resizedImage unlockFocus];
        
        //Set URL for single pages
        NSURL *pageFilename = [savePath URLByAppendingPathComponent: [NSString stringWithFormat:@"Seite_%d.png", i+1]];
        
        
        [[NSFileManager defaultManager] createFileAtPath: [pageFilename path]
                                                contents:[[NSBitmapImageRep imageRepWithData:[singlePage TIFFRepresentation]]
                                                          representationUsingType:NSPNGFileType properties:nil]
                                              attributes:nil];
        
        
        if([self getColoredPixelOfImageFromURL:pageFilename] > 0){
            coloredPages++;
            NSLog(@"SEITE %d -----------------------------------------------------------------------------------------------", i+1);
            _coloredPages = coloredPages;
            [self.coloredPagesList appendString:[NSString stringWithFormat:@"%d,",i+1]];
        }else{
            NSError *error = nil;
            [[NSFileManager defaultManager] removeItemAtURL:pageFilename error:&error];
            if(error){
                NSLog(@"%@", error);
            }
        }
        
        resizedImage = nil;
        singlePage = nil;
        
    }
}
[self.appTimer invalidate];

Thank you so much for helping!!!

0

There are 0 best solutions below