Welcome Guest! To enable all features, please Login or Register.



Last Go to last post Unread Go to first unread post
#1 Posted : Thursday, December 23, 2021 12:10:04 PM(UTC)
George B.

Groups: Registered
Posts: 2

I want to:
1. Load a TIFF image.
2. Burn-in annotations from an LT annotations XML file.
3. Save the modified image in a different file.

All of this without displaying the image in a UI.

Loading and saving is no problem. It's the burning-in that is giving me headaches.
I can't seem to create an offscreen context to do the burn.

What am I missing/doing wrong? HELP!

    int rc = 0;
    NSError *error;
    // work from user/Downloads
    NSFileManager *fm = [NSFileManager defaultManager];
    NSURL *downloadsURL = [fm URLForDirectory:NSDownloadsDirectory
    NSURL *furl = [NSURL URLWithString:@"ocr1.tif" relativeToURL:downloadsURL]; // source image
    NSURL *aurl = [NSURL URLWithString:@"A_0.tif" relativeToURL:downloadsURL];  // saved image
    NSURL *xurl = [NSURL URLWithString:@"redact.ann.xml" relativeToURL:downloadsURL];   // anno xml
    // create annotation container from xml
    LTAnnCodecs *annCodecs = [[LTAnnCodecs alloc] init];
    NSString *xmlData = [NSString stringWithContentsOfURL:xurl usedEncoding:nil error:&error];
    NSLog(@"%@", xmlData);
    LTAnnContainer * container = [annCodecs loadXmlData:xmlData pageNumber:1];
    // list them
    for (int i = 0; i < container.children.count; i++)
        LTAnnObject *annObj = container.children[i];
        NSLog(@"%@\n", [annObj friendlyName]);
    LTRasterCodecs *codecs =[[LTRasterCodecs alloc] init];
    LTRasterImage *sourceImage = [codecs loadURL:furl error:&error];
    BOOL setResolution          = NO;
    int imageDpiX               = 0;
    int imageDpiY               = 0;
    if (sourceImage.xResolution != 72 || sourceImage.yResolution != 72)
        setResolution = YES;
        imageDpiX     = (uint)sourceImage.xResolution;
        imageDpiY     = (uint)sourceImage.yResolution;

    LTAnnContainerMapper *mapper = [container.mapper copy];
    container.mapper = [[LTAnnContainerMapper alloc] initWithSourceDpiX:sourceImage.xResolution
    [sourceImage.linkedImage lockFocus];
    CGImageRef imgRef = [LTRasterImageConverter convertToCGImage:sourceImage
    NSBitmapImageRep *refrep = [[NSBitmapImageRep alloc] initWithCGImage:imgRef];

    // this always returns nil. WHY! WHY! WHY!
    NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithBitmapImageRep:refrep];
    [NSGraphicsContext saveGraphicsState];
    CGContextRef contextRef = [context CGContext];
    CGContextTranslateCTM(contextRef, 0, sourceImage.height);
    CGContextScaleCTM(contextRef, 1, -1);
    // render annotations onto source image
    LTAnnUniversalRenderingEngine *renderingEngine = [[LTAnnUniversalRenderingEngine alloc] init];
    [renderingEngine attachContainer:container context:contextRef];
    //We will use the burnScaleFactor property to burn the objects with fixed states correctly
    LeadRectD testRect =   [container.mapper rectFromContainerCoordinates:LeadRectDMake(0, 0, 10, 10) operations:(LTAnnFixedStateOperationsNone)];
    LeadRectD scaledRect =  [mapper rectFromContainerCoordinates:LeadRectDMake(0, 0, 10, 10) operations:(LTAnnFixedStateOperationsNone)];
    double scaleFactor = scaledRect.width / testRect.width;
    container.mapper.burnScaleFactor /= scaleFactor;
    [renderingEngine burn];
    [NSGraphicsContext restoreGraphicsState];
    [sourceImage.linkedImage unlockFocus];
    [renderingEngine detach];
    // save rendered image into dest.
    LTCodecsImageInfo *info = [codecs imageInformationForURL:furl totalPages:YES error:&error];
    [codecs save:sourceImage url:aurl format:LTRasterImageFormatTif bitsPerPixel:info.bitsPerPixel error:&error];
    return rc;

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2023, Yet Another Forum.NET
This page was generated in 0.039 seconds.