←Select platform

RetrieveDataFromImage Property

Summary
Gets or sets a value that indicates whether to automatically populate the scanline buffers in CodecsSaveImageEventArgs when using the RasterCodecs.SaveImage event.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public bool RetrieveDataFromImage { get; set; } 
@property (nonatomic, assign) BOOL retrieveDataFromImage; 
public boolean getRetrieveDataFromImage() 
public void setRetrieveDataFromImage(boolean value) 
public: 
property bool RetrieveDataFromImage { 
   bool get(); 
   void set (    bool ); 
} 
RetrieveDataFromImage # get and set (CodecsSaveOptions) 

Property Value

Value Description
true To automatically populate the scanline buffers in CodecsSaveImageEventArgs when using the RasterCodecs.SaveImage event.
false To make the user responsible for populating the scanline buffers. Default value is false.
Remarks

The Buffer property works as the input and output buffer containing the image data to save. If the value of RetrieveDataFromImage is set to false (the default), then the user is always responsible for providing the image data by setting in Buffer. If the value of RetrieveDataFromImage is set to true, then the RasterCodecs object will populate the Buffer prior to raising this event. The user can then inspect or modify the scanlines data or simple ignore it to save the original image data as is.

Notice that on either case, the user must provide the scanline data in the source image original format (stored in the Image property. The RasterCodecs object will then convert this data to the appropriate output format if needed, for example, if the user instructed the RasterCodecs object to save the image in a different file format than the original image.

Example

The following example shows how to use RetrieveDataFromImage to:

  1. Save the image without instructing RasterCodecs to automatically populate the data. Resulting in an output file with a blank image.
  2. Save the image while RasterCodecs to automatically populate the data. Resulting in a correct output image.
  3. Save the image with RasterCodecs automatically generating the data then manipulating it to generate an output file that is an invert of the original image.
C#
Java
using Leadtools; 
using Leadtools.Codecs; 
 
using Leadtools.ImageProcessing.Core; 
using Leadtools.Pdf; 
 
private int myMode; 
 
public void RetrieveDataFromImageExample() 
{ 
   using (RasterCodecs codecs = new RasterCodecs()) 
   { 
      string srcFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1.cmp"); 
      string blankFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1_Blank.bmp"); 
      string defaultFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1_Default.bmp"); 
      string invertedFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1_Inverted.bmp"); 
 
      // Load the source image 
      using (RasterImage image = codecs.Load(srcFile)) 
      { 
         // Subscribe to the SaveImage event 
         codecs.SaveImage += new EventHandler<CodecsSaveImageEventArgs>(codecs_SaveImage); 
 
         // First, set RetrieveDataFromImage to false and save 
         // This should generate a blank image 
         myMode = 0; 
         codecs.Options.Save.RetrieveDataFromImage = false; 
         codecs.Save(image, blankFile, RasterImageFormat.Bmp, 24); 
 
         // Next, set RetrieveDataFromImage to true but do not 
         // change the data, this should generate the correct image and is the equivalant 
         // to calling Save without subscribing to SaveImage 
         myMode = 1; 
         codecs.Options.Save.RetrieveDataFromImage = true; 
         codecs.Save(image, defaultFile, RasterImageFormat.Bmp, 24); 
 
         // Finally, leave RetrieveDataFromImage as true but change the data by inverting 
         // the scanlines, this should generate an inverted image 
         myMode = 2; 
         codecs.Save(image, invertedFile, RasterImageFormat.Bmp, 24); 
 
         codecs.SaveImage -= new EventHandler<CodecsSaveImageEventArgs>(codecs_SaveImage); 
      } 
   } 
} 
 
private void codecs_SaveImage(object sender, CodecsSaveImageEventArgs e) 
{ 
   // This example works with 24 bpp images only 
   Debug.Assert(e.Image.BitsPerPixel == 24); 
 
   switch (myMode) 
   { 
      case 0: 
         // Do not do anything 
         break; 
 
      case 1: 
         // Do not do anything 
         break; 
 
      case 2: 
         // Invert the image data 
         { 
            int scanlineLength = e.Image.BytesPerLine; 
            byte[] scanline = new byte[scanlineLength]; 
 
            // Loop through all the scanlines in the data 
            for (int y = 0; y < e.Lines; y++) 
            { 
               // Get this row 
               e.Buffer.GetData(y * scanlineLength, scanline, 0, scanlineLength); 
 
               // Process it by inverting every pixel data 
               for (int x = 0; x < scanlineLength; x++) 
               { 
                  scanline[x] = (byte)(255 - scanline[x]); 
               } 
 
               // Copy it back to the event buffer 
               e.Buffer.SetData(y * scanlineLength, scanline, 0, scanlineLength); 
            } 
         } 
         break; 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.File; 
import java.io.IOException; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.nio.file.Paths; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.imageprocessing.core.MinMaxBitsCommand; 
 
 
private int myMode; 
 
public void codecsRetrieveDataFromImageExample() { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   RasterCodecs codecs = new RasterCodecs(); 
   String srcFile = combine(LEAD_VARS_IMAGES_DIR, "Sample1.cmp"); 
   String blankFile = combine(LEAD_VARS_IMAGES_DIR, "Sample1_Blank.bmp"); 
   String defaultFile = combine(LEAD_VARS_IMAGES_DIR, "Sample1_Default.bmp"); 
   String invertedFile = combine(LEAD_VARS_IMAGES_DIR, "Sample1_Inverted.bmp"); 
 
   // Load the source image 
   RasterImage image = codecs.load(srcFile); 
 
   // Subscribe to the SaveImage event 
 
   codecs.addSaveImageListener(codecsSaveImage); 
   // First, set RetrieveDataFromImage to false and save 
   // This should generate a blank image 
   myMode = 0; 
   codecs.getOptions().getSave().setRetrieveDataFromImage(false); 
   codecs.save(image, blankFile, RasterImageFormat.BMP, 24); 
 
   // Next, set RetrieveDataFromImage to true but do not 
   // change the data, this should generate the correct image and is the equivalant 
   // to calling Save without subscribing to SaveImage 
   myMode = 1; 
   codecs.getOptions().getSave().setRetrieveDataFromImage(true); 
   codecs.save(image, defaultFile, RasterImageFormat.BMP, 24); 
 
   assertTrue("File saved to " + defaultFile, (new File(defaultFile)).exists()); 
   System.out.printf("File successfully saved to %s%n", defaultFile); 
 
   // Finally, leave RetrieveDataFromImage as true but change the data by inverting 
   // the scanlines, this should generate an inverted image 
   myMode = 2; 
   codecs.save(image, invertedFile, RasterImageFormat.BMP, 24); 
 
   assertTrue("File unsuccessfully saved to " + invertedFile, (new File(invertedFile)).exists()); 
   System.out.printf("File successfully saved to %s%n", invertedFile); 
 
   codecs.removeSaveImageListener(codecsSaveImage); 
   image = null; 
   codecs = null; 
} 
 
CodecsSaveImageListener codecsSaveImage = new CodecsSaveImageListener() { 
 
   @Override 
   public void onSaveImage(CodecsSaveImageEvent e) { 
      // This example works with 24 bpp images only 
      assertEquals(e.getImage().getBitsPerPixel(), 24); 
 
      switch (myMode) { 
         case 0: 
            // Do not do anything 
            break; 
 
         case 1: 
            // Do not do anything 
            break; 
 
         case 2: 
         // Invert the image data 
         { 
            int scanlineLength = e.getImage().getBytesPerLine(); 
            byte[] scanline = new byte[scanlineLength]; 
 
            // Loop through all the scanlines in the data 
            for (int y = 0; y < e.getLines(); y++) { 
               // Get this row 
               e.getBuffer().getData(y * scanlineLength, scanline, 0, scanlineLength); 
 
               // Process it by inverting every pixel data 
               for (int x = 0; x < scanlineLength; x++) { 
                  scanline[x] = (byte) (255 - scanline[x]); 
               } 
 
               // Copy it back to the event buffer 
               e.getBuffer().setData(y * scanlineLength, scanline, 0, scanlineLength); 
            } 
         } 
            break; 
      } 
   } 
 
}; 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Codecs Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.