Advertisement
armega

Deleting a tiff tag using the LibTiff.Net Library

Feb 25th, 2012
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 26.84 KB | None | 0 0
  1. // Module: TIFFTAGS.cs - Developed By: Arvo Bowen III - Version: 2012.02.25
  2. // References: BitMiracle.LibTiff.NET (BitMiracle.LibTiff.NET.dll)
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using BitMiracle.LibTiff.Classic;
  9. using System.Diagnostics;
  10. using System.IO;
  11.  
  12. namespace LibNetDotNetSupport
  13. {
  14.     #region TIFFTAGS_TAG Class
  15.     public enum TIFFTAGS_TAG_DATATYPE { UNKNOWN = 0, ASCII = 2, SHORT = 3, LONG = 4, RATIONAL = 5 };
  16.     public class TIFFTAGS_TAG
  17.     {
  18.         private TIFFTAGS_TAG_DATATYPE m_dtTagType;
  19.         private Object m_oValue = null;
  20.         private int m_iTagNumber = -1;
  21.         private string m_sFileName = null;
  22.         private int m_iPageNumber = -1;
  23.         private string m_sTagName = null;
  24.  
  25.         //Constructor
  26.         public TIFFTAGS_TAG()
  27.         {
  28.             TIFFTAGS_TAG_CONSTRUCTOR(null, -1, null);
  29.         }
  30.         public TIFFTAGS_TAG(string sFileName, int iTagNumber, Object oValue)
  31.         {
  32.             TIFFTAGS_TAG_CONSTRUCTOR(sFileName, iTagNumber, oValue);
  33.         }
  34.         private void TIFFTAGS_TAG_CONSTRUCTOR(string sFileName, int iTagNumber, Object oValue)
  35.         {
  36.             m_sFileName = sFileName;
  37.             m_iTagNumber = iTagNumber;
  38.             m_oValue = oValue;
  39.         }
  40.  
  41.         //TagType
  42.         public TIFFTAGS_TAG_DATATYPE TagType
  43.         {
  44.             get
  45.             {
  46.                 //Determine the data type
  47.                 string sDataType = "";
  48.                 if (m_oValue.GetType().ToString() == "BitMiracle.LibTiff.Classic.FieldValue[]")
  49.                 {
  50.                     //Data type is a field value
  51.                     FieldValue[] fvValue = (FieldValue[])m_oValue;
  52.                     sDataType = fvValue[0].Value.GetType().ToString();
  53.                 }
  54.                 else
  55.                 {
  56.                     //Try to check a normal system data type because this one is not a field value
  57.                     sDataType = m_oValue.GetType().ToString();
  58.                 }
  59.                 switch (sDataType)
  60.                 {
  61.                     case "System.Int32":
  62.                     case "System.UInt32[]":
  63.                     case "BitMiracle.LibTiff.Classic.FileType":
  64.                         m_dtTagType = TIFFTAGS_TAG_DATATYPE.LONG;
  65.  
  66.                         break;
  67.                     case "System.Int16":
  68.                     case "BitMiracle.LibTiff.Classic.Compression":
  69.                     case "BitMiracle.LibTiff.Classic.Photometric":
  70.                     case "BitMiracle.LibTiff.Classic.Threshold":
  71.                     case "BitMiracle.LibTiff.Classic.FillOrder":
  72.                     case "BitMiracle.LibTiff.Classic.Orientation":
  73.                     case "BitMiracle.LibTiff.Classic.PlanarConfig":
  74.                     case "BitMiracle.LibTiff.Classic.ResUnit":
  75.                         m_dtTagType = TIFFTAGS_TAG_DATATYPE.SHORT;
  76.  
  77.                         break;
  78.                     case "System.String":
  79.                     case "System.Byte[]":
  80.                         m_dtTagType = TIFFTAGS_TAG_DATATYPE.ASCII;
  81.  
  82.                         break;
  83.                     case "System.Single":
  84.                         m_dtTagType = TIFFTAGS_TAG_DATATYPE.RATIONAL;
  85.  
  86.                         break;
  87.                     default:
  88.                         m_dtTagType = TIFFTAGS_TAG_DATATYPE.UNKNOWN;
  89.                         //Debug.WriteLine("Unknown TAG data type:" + fvValue[0].Value.GetType().ToString());
  90.  
  91.                         break;
  92.                 }
  93.  
  94.                 return m_dtTagType;
  95.             }
  96.         }
  97.  
  98.         //Value
  99.         public Object Value
  100.         {
  101.             get
  102.             {
  103.                 return m_oValue;
  104.             }
  105.             set
  106.             {
  107.                 m_oValue = value;
  108.             }
  109.         }
  110.  
  111.         //TagNumber
  112.         public int TagNumber
  113.         {
  114.             get
  115.             {
  116.                 return m_iTagNumber;
  117.             }
  118.             set
  119.             {
  120.                 m_iTagNumber = value;
  121.             }
  122.         }
  123.  
  124.         //FileName
  125.         public string FileName
  126.         {
  127.             get
  128.             {
  129.                 return m_sFileName;
  130.             }
  131.             set
  132.             {
  133.                 m_sFileName = value;
  134.             }
  135.         }
  136.  
  137.         //PageNumber
  138.         public int PageNumber
  139.         {
  140.             get
  141.             {
  142.                 return m_iPageNumber;
  143.             }
  144.             set
  145.             {
  146.                 m_iPageNumber = value;
  147.             }
  148.         }
  149.  
  150.         //TagName
  151.         public string TagName
  152.         {
  153.             get
  154.             {
  155.                 if (m_iTagNumber >= 0)
  156.                 {
  157.                     switch (m_iTagNumber)
  158.                     {
  159.                         case 37572:
  160.                             //GSCCCA Previous Page Tag
  161.                             m_sTagName = "GSCCCA_PREVIOUS_PAGE";
  162.  
  163.                             break;
  164.                         case 37574:
  165.                             //GSCCCA Next Page Tag
  166.                             m_sTagName = "GSCCCA_NEXT_PAGE";
  167.  
  168.                             break;
  169.                         default:
  170.                             //Convert the tag number to a tag name
  171.                             TiffTag tTag = (TiffTag)m_iTagNumber;
  172.                             m_sTagName = Enum.GetName(typeof(TiffTag), tTag);
  173.  
  174.                             if (m_sTagName == null) m_sTagName = "UNKNOWN_TAG_NAME";
  175.  
  176.                             break;
  177.                     }
  178.                 }
  179.                 else
  180.                 {
  181.                     //No tag number has been set
  182.                     m_sTagName = "NO_TAG_NUMBER_SET";
  183.                 }
  184.  
  185.                 //Check if a tag name was not found
  186.                 if (m_sTagName == "") m_sTagName = "UNKNOWN_TAG_NAME";
  187.  
  188.                 return m_sTagName;
  189.             }
  190.         }
  191.  
  192.         public string ValueToFlatString()
  193.         {
  194.             string sValue = "";
  195.  
  196.             if (m_oValue != null)
  197.             {
  198.                 FieldValue[] fvValue = (FieldValue[])m_oValue;
  199.  
  200.                 //Determine what type of value is being unpacked
  201.                 string sValueType = "";
  202.                 switch (fvValue[0].Value.GetType().ToString())
  203.                 {
  204.                     case "System.Byte[]":
  205.                         sValueType = "ascii";
  206.                         break;
  207.                     case "BitMiracle.LibTiff.Classic.FileType":
  208.                     case "System.Int32":
  209.                         if (fvValue.Count() == 2)
  210.                         {
  211.                             if (fvValue[1].Value.GetType().ToString() == "System.Byte[]") sValueType = "ascii";
  212.                         }
  213.                         else sValueType = "long";
  214.                         break;
  215.                     case "System.Int16":
  216.                     case "BitMiracle.LibTiff.Classic.Photometric":
  217.                     case "BitMiracle.LibTiff.Classic.Threshold":
  218.                     case "BitMiracle.LibTiff.Classic.FillOrder":
  219.                     case "BitMiracle.LibTiff.Classic.Orientation":
  220.                     case "BitMiracle.LibTiff.Classic.PlanarConfig":
  221.                     case "BitMiracle.LibTiff.Classic.ResUnit":
  222.                         sValueType = "short";
  223.                         break;
  224.                     case "BitMiracle.LibTiff.Classic.Compression":
  225.                         sValueType = "compression";
  226.                         break;
  227.                     case "System.UInt32[]":
  228.                         sValueType = "to_long";
  229.                         break;
  230.                     case "System.Single":
  231.                         sValueType = "rational";
  232.                         break;
  233.                     default:
  234.                         sValueType = fvValue[0].Value.GetType().ToString();
  235.                         break;
  236.                 }
  237.  
  238.                 if (fvValue[0].Value != null)
  239.                 {
  240.                     switch (sValueType)
  241.                     {
  242.                         case "short":
  243.                             sValue = fvValue[0].ToShort().ToString();
  244.                             if (fvValue[0].ToShort().ToString() != fvValue[0].Value.ToString())
  245.                             {
  246.                                 sValue += " [" + fvValue[0].Value.ToString() + "]";
  247.                             }
  248.  
  249.                             break;
  250.                         case "long":
  251.                             sValue = fvValue[0].ToUInt().ToString();
  252.                             if (fvValue[0].ToUInt().ToString() != fvValue[0].Value.ToString())
  253.                             {
  254.                                 sValue += " [" + fvValue[0].Value.ToString() + "]";
  255.                             }
  256.  
  257.                             break;
  258.                         case "compression":
  259.                             //Known short value
  260.                             switch (fvValue[0].Value.ToString())
  261.                             {
  262.                                 case "CCITT_T4":
  263.                                     //Tiff G3 custom string
  264.                                     sValue = "Group 3 Fax (CCITT T.4) [" + fvValue[0].ToShort() + "]";
  265.  
  266.                                     break;
  267.                                 case "CCITT_T6":
  268.                                     //Tiff G4 custom string
  269.                                     sValue = "Group 4 Fax (CCITT T.6) [" + fvValue[0].ToShort() + "]";
  270.  
  271.                                     break;
  272.                                 default:
  273.                                     //All other values
  274.                                     sValue = fvValue[0].ToShort().ToString();
  275.                                     if (fvValue[0].ToShort().ToString() != fvValue[0].Value.ToString())
  276.                                     {
  277.                                         sValue += " [" + fvValue[0].Value.ToString() + "]";
  278.                                     }
  279.  
  280.                                     break;
  281.                             }
  282.  
  283.                             break;
  284.                         case "to_long":
  285.                             //This will be a long value when all parts of the array are combined
  286.                             foreach (UInt32 oValue in (UInt32[])fvValue[0].Value)
  287.                             {
  288.                                 sValue += (oValue.ToString() != "" ? oValue.ToString() : "0");
  289.                             }
  290.  
  291.                             break;
  292.                         case "ascii":
  293.                             //Byte array to be converted to a string (make sure to trim all the null characters off the end)
  294.                             sValue = System.Text.ASCIIEncoding.ASCII.GetString((Byte[])fvValue[(fvValue.Count() - 1)].Value).TrimEnd('\0');
  295.  
  296.                             break;
  297.                         case "rational":
  298.                             sValue = ToFraction(Convert.ToDecimal(fvValue[0].Value.ToString()));
  299.  
  300.                             break;
  301.                         default:
  302.                             //All other values are assumed to be available using the ToString() methods
  303.                             Debug.WriteLine(fvValue[0].Value.GetType().ToString());
  304.                             sValue = "TYPE NOT SUPPORTED" + fvValue[0].Value.ToString();
  305.  
  306.                             break;
  307.                     }
  308.  
  309.                 }
  310.             }
  311.  
  312.             return sValue;
  313.         }
  314.  
  315.         private static string ToFraction(decimal value)
  316.         {
  317.             // get the whole value of the fraction
  318.             decimal mWhole = Math.Truncate(value);
  319.  
  320.             // get the fractional value
  321.             decimal mFraction = value - mWhole;
  322.  
  323.             // initialize a numerator and denomintar
  324.             uint mNumerator = 0;
  325.             uint mDenomenator = 1;
  326.  
  327.             // ensure that there is actual a fraction
  328.             if (mFraction > 0m)
  329.             {
  330.                 // convert the value to a string so that you can count the number of decimal places there are
  331.                 string strFraction = mFraction.ToString().Remove(0, 2);
  332.  
  333.                 // store teh number of decimal places
  334.                 uint intFractLength = (uint)strFraction.Length;
  335.  
  336.                 // set the numerator to have the proper amount of zeros
  337.                 mNumerator = (uint)Math.Pow(10, intFractLength);
  338.  
  339.                 // parse the fraction value to an integer that equals [fraction value] * 10^[number of decimal places]
  340.                 uint.TryParse(strFraction, out mDenomenator);
  341.  
  342.                 // get the greatest common divisor for both numbers
  343.                 uint gcd = GreatestCommonDivisor(mDenomenator, mNumerator);
  344.  
  345.                 // divide the numerator and the denominator by the gratest common divisor
  346.                 mNumerator = mNumerator / gcd;
  347.                 mDenomenator = mDenomenator / gcd;
  348.             }
  349.  
  350.             // create a string builder
  351.             StringBuilder mBuilder = new StringBuilder();
  352.  
  353.             // add the whole number if it's greater than 0
  354.             if (mWhole > 0m)
  355.             {
  356.                 mBuilder.Append(mWhole);
  357.             }
  358.  
  359.             // add the fraction if it's greater than 0m
  360.             if (mFraction > 0m)
  361.             {
  362.                 if (mBuilder.Length > 0)
  363.                 {
  364.                     mBuilder.Append(" ");
  365.                 }
  366.  
  367.                 mBuilder.Append(mDenomenator);
  368.                 mBuilder.Append("/");
  369.                 mBuilder.Append(mNumerator);
  370.             }
  371.  
  372.             // add the whole number over 1 if the number is not a decimal
  373.             if (!mBuilder.ToString().Contains('/')) mBuilder.Append("/1");
  374.  
  375.             return mBuilder.ToString();
  376.         }
  377.         private static decimal ToDecimal(string value)
  378.         {
  379.             String[] sSplit = value.Split('/');
  380.  
  381.             if (sSplit.Length != 2) return -1;
  382.  
  383.             try
  384.             {
  385.                 decimal dNumerator = System.Convert.ToInt32(sSplit[0]);
  386.                 decimal dDenominator = System.Convert.ToInt32(sSplit[1]);
  387.  
  388.                 return (dNumerator / dDenominator);
  389.             }
  390.             catch
  391.             {
  392.                 //Error
  393.                 return -1;
  394.             }
  395.         }
  396.         private static uint GreatestCommonDivisor(uint valA, uint valB)
  397.         {
  398.             // return 0 if both values are 0 (no GSD)
  399.             if (valA == 0 &&
  400.               valB == 0)
  401.             {
  402.                 return 0;
  403.             }
  404.             // return value b if only a == 0
  405.             else if (valA == 0 &&
  406.                   valB != 0)
  407.             {
  408.                 return valB;
  409.             }
  410.             // return value a if only b == 0
  411.             else if (valA != 0 && valB == 0)
  412.             {
  413.                 return valA;
  414.             }
  415.             // actually find the GSD
  416.             else
  417.             {
  418.                 uint first = valA;
  419.                 uint second = valB;
  420.  
  421.                 while (first != second)
  422.                 {
  423.                     if (first > second)
  424.                     {
  425.                         first = first - second;
  426.                     }
  427.                     else
  428.                     {
  429.                         second = second - first;
  430.                     }
  431.                 }
  432.  
  433.                 return first;
  434.             }
  435.  
  436.         }
  437.     }
  438.     #endregion
  439.  
  440.     public static class TIFFTAGS
  441.     {
  442.         #region TIFFTAGS_PAGE Class
  443.         private class TIFFTAGS_PAGE
  444.         {
  445.             private int m_iHeight;
  446.             private Dictionary<ushort, FieldValue[]> m_dTags;
  447.             private byte[] m_bytePageData;
  448.             private bool m_bEncoded;
  449.             private int m_iStripSize;
  450.             private int m_iStripOffset;
  451.  
  452.             //Constructor
  453.             public TIFFTAGS_PAGE()
  454.             {
  455.                 m_iHeight = 0;
  456.                 m_dTags = new Dictionary<ushort, FieldValue[]>();
  457.                 m_bytePageData = null;
  458.                 m_bEncoded = false;
  459.                 m_iStripSize = 0;
  460.             }
  461.  
  462.             //Height
  463.             public int Height
  464.             {
  465.                 get
  466.                 {
  467.                     return m_iHeight;
  468.                 }
  469.                 set
  470.                 {
  471.                     m_iHeight = value;
  472.                 }
  473.             }
  474.  
  475.             //Tags
  476.             public Dictionary<ushort, FieldValue[]> Tags
  477.             {
  478.                 get
  479.                 {
  480.                     return m_dTags;
  481.                 }
  482.                 set
  483.                 {
  484.                     m_dTags = value;
  485.                 }
  486.             }
  487.  
  488.             //PageData
  489.             public byte[] PageData
  490.             {
  491.                 get
  492.                 {
  493.                     return m_bytePageData;
  494.                 }
  495.                 set
  496.                 {
  497.                     m_bytePageData = value;
  498.                 }
  499.             }
  500.  
  501.             //Encoded
  502.             public bool Encoded
  503.             {
  504.                 get
  505.                 {
  506.                     return m_bEncoded;
  507.                 }
  508.                 set
  509.                 {
  510.                     m_bEncoded = value;
  511.                 }
  512.             }
  513.  
  514.             //StripSize
  515.             public int StripSize
  516.             {
  517.                 get
  518.                 {
  519.                     return m_iStripSize;
  520.                 }
  521.                 set
  522.                 {
  523.                     m_iStripSize = value;
  524.                 }
  525.             }
  526.  
  527.             //StripOffset
  528.             public int StripOffset
  529.             {
  530.                 get
  531.                 {
  532.                     return m_iStripOffset;
  533.                 }
  534.                 set
  535.                 {
  536.                     m_iStripOffset = value;
  537.                 }
  538.             }
  539.         }
  540.         #endregion
  541.  
  542.         private static Tiff.TiffExtendProc m_parentExtender;
  543.         private static List<TIFFTAGS_TAG> m_lTagsToWrite;
  544.  
  545.         private static void TagExtender(Tiff tif)
  546.         {
  547.             List<TiffFieldInfo> tfInfo = new List<TiffFieldInfo>();
  548.             Int16 readCount = 0;
  549.             Int16 writeCount = 0;
  550.             bool okToChange = false;
  551.             bool passCount = false;
  552.  
  553.             //Make sure to create a new tiff field info for all the tags to be written
  554.             foreach (TIFFTAGS_TAG ttTag in m_lTagsToWrite)
  555.             {
  556.                 switch (ttTag.TagType)
  557.                 {
  558.                     case TIFFTAGS_TAG_DATATYPE.ASCII:
  559.                         readCount = -1;
  560.                         writeCount = -1;
  561.                         okToChange = true;
  562.                         passCount = false;
  563.  
  564.                         break;
  565.                     case TIFFTAGS_TAG_DATATYPE.LONG:
  566.                     case TIFFTAGS_TAG_DATATYPE.RATIONAL:
  567.                     case TIFFTAGS_TAG_DATATYPE.SHORT:
  568.                         readCount = 2;
  569.                         writeCount = 2;
  570.                         okToChange = false;
  571.                         passCount = true;
  572.  
  573.                         break;
  574.                 }
  575.  
  576.                 if (ttTag.TagType != TIFFTAGS_TAG_DATATYPE.UNKNOWN)
  577.                 {
  578.                     //Add the new item to the list
  579.                     tfInfo.Add(new TiffFieldInfo((TiffTag)ttTag.TagNumber, readCount, writeCount, (TiffType)ttTag.TagType, FieldBit.Custom, okToChange, passCount, ttTag.TagName));
  580.                 }
  581.             }
  582.  
  583.             //Turn the list into an array again
  584.             TiffFieldInfo[] tfInfoArray = tfInfo.ToArray();
  585.  
  586.             //Add the fields to the main field info area
  587.             tif.MergeFieldInfo(tfInfoArray, tfInfoArray.Length);
  588.  
  589.             //Propergate the chain if needed
  590.             if (m_parentExtender != null)
  591.                 m_parentExtender(tif);
  592.         }
  593.  
  594.         public static bool DeleteTiffTag(string sFileName, ushort ushortTagNumber)
  595.         {
  596.             //Deletes a tiff tag from the given image
  597.             //Returns true if successful or false if error occured
  598.  
  599.             try
  600.             {
  601.                 //Reuse the main DeleteTiffTags function
  602.                 return DeleteTiffTags(sFileName, new List<ushort>(new ushort[] { ushortTagNumber }));
  603.             }
  604.             catch { return false; }
  605.         }
  606.  
  607.         public static bool DeleteTiffTags(string sFileName, List<ushort> ushortTagNumbers)
  608.         {
  609.             //Deletes a list of tiff tag from the given image
  610.             //Returns true if successful or false if error occured
  611.  
  612.             //Define variables
  613.             List<TIFFTAGS_PAGE> ttPage = new List<TIFFTAGS_PAGE>();
  614.  
  615.             //Check for empty list
  616.             if (ushortTagNumbers.Count == 0) return false;
  617.  
  618.             try
  619.             {
  620.                 //ADDED
  621.                 m_lTagsToWrite = new List<TIFFTAGS_TAG>();
  622.                 m_lTagsToWrite.Add(new TIFFTAGS_TAG("", 38001, Convert.ToString("")));
  623.                 m_lTagsToWrite.Add(new TIFFTAGS_TAG("", 38002, Convert.ToString("")));
  624.                 m_parentExtender = Tiff.SetTagExtender(TagExtender);
  625.  
  626.                 //Open the file for reading
  627.                 using (Tiff input = Tiff.Open(sFileName, "r"))
  628.                 {
  629.                     if (input == null) return false;
  630.  
  631.                     //Get page count
  632.                     int numberOfDirectories = input.NumberOfDirectories();
  633.  
  634.                     //Go through all the pages
  635.                     for (short i = 0; i < numberOfDirectories; ++i)
  636.                     {
  637.                         //Set the page
  638.                         input.SetDirectory(i);
  639.  
  640.                         //Create a new tags dictionary to store all my tags
  641.                         Dictionary<ushort, FieldValue[]> dTags = new Dictionary<ushort, FieldValue[]>();
  642.  
  643.                         //Get all the tags for the page
  644.                         for (ushort t = ushort.MinValue; t < ushort.MaxValue; ++t)
  645.                         {
  646.                             TiffTag tag = (TiffTag)t;
  647.                             FieldValue[] tagValue = input.GetField(tag);
  648.                             if (tagValue != null)
  649.                             {
  650.                                 dTags.Add(t, tagValue);
  651.                             }
  652.                         }
  653.  
  654.                         //Check if the page is encoded
  655.                         bool encoded = false;
  656.                         FieldValue[] compressionTagValue = input.GetField(TiffTag.COMPRESSION);
  657.                         if (compressionTagValue != null)
  658.                             encoded = (compressionTagValue[0].ToInt() != (int)Compression.NONE);
  659.                        
  660.                         //Create a new byte array to store all my image data
  661.                         int numberOfStrips = input.NumberOfStrips();
  662.                         byte[] byteImageData = new byte[numberOfStrips * input.StripSize()];
  663.                         int offset = 0;
  664.  
  665.                         //Get all the image data for the page
  666.                         for (int n = 0; n < numberOfStrips; ++n)
  667.                         {
  668.                             int bytesRead;
  669.                             if (encoded)
  670.                                 bytesRead = input.ReadEncodedStrip(n, byteImageData, offset, byteImageData.Length - offset);
  671.                             else
  672.                                 bytesRead = input.ReadRawStrip(n, byteImageData, offset, byteImageData.Length - offset);
  673.  
  674.                             //Add to the offset keeping up with where we are
  675.                             offset += bytesRead;
  676.                         }
  677.  
  678.                         //Save all the tags, image data, and height, etc for the page
  679.                         TIFFTAGS_PAGE tiffPage = new TIFFTAGS_PAGE();
  680.                         tiffPage.Height = input.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
  681.                         tiffPage.Tags = dTags;
  682.                         tiffPage.PageData = byteImageData;
  683.                         tiffPage.Encoded = encoded;
  684.                         tiffPage.StripSize = input.StripSize();
  685.                         tiffPage.StripOffset = input.GetField(TiffTag.STRIPOFFSETS)[0].ToIntArray()[0];
  686.                         ttPage.Add(tiffPage);
  687.                     }
  688.                 }
  689.  
  690.                 //Open the file for writing
  691.                 using (Tiff output = Tiff.Open(sFileName + "-new.tif", "w"))
  692.                 {
  693.                     if (output == null) return false;
  694.  
  695.                     //Go through all the pages
  696.                     for (short i = 0; i < ttPage.Count(); ++i)
  697.                     {
  698.                         //Write all the tags for the page
  699.                         foreach (KeyValuePair<ushort, FieldValue[]> tagValue in ttPage[i].Tags)
  700.                         {
  701.                             //Write all the tags except the one's needing to be deleted
  702.                             if (!ushortTagNumbers.Contains(tagValue.Key))
  703.                             {
  704.                                 TiffTag tag = (TiffTag)tagValue.Key;
  705.                                 output.GetTagMethods().SetField(output, tag, tagValue.Value);
  706.                             }
  707.                         }
  708.  
  709.                         //Set the height for the page
  710.                         output.SetField(TiffTag.ROWSPERSTRIP, ttPage[i].Height);
  711.  
  712.                         //Set the offset for the page
  713.                         output.SetField(TiffTag.STRIPOFFSETS, ttPage[i].StripOffset);
  714.  
  715.                         //Save page data along with tags
  716.                         output.CheckpointDirectory();
  717.  
  718.                         //Write each strip one at a time using the same orginal strip size
  719.                         int numberOfStrips = ttPage[i].PageData.Length / ttPage[i].StripSize;
  720.                         int offset = 0;
  721.                         for (int n = 0; n < numberOfStrips; ++n)
  722.                         {
  723.                             //Write all the image data (strips) for the page
  724.                             if (ttPage[i].Encoded)
  725.                                 //output.WriteEncodedStrip(n, byteStrip, offset, byteStrip.Length - offset);
  726.                                 output.WriteEncodedStrip(0, ttPage[i].PageData, offset, ttPage[i].StripSize - offset);
  727.                             else
  728.                                 output.WriteRawStrip(n, ttPage[i].PageData, offset, ttPage[i].StripSize - offset);
  729.  
  730.                             //Add to the offset keeping up with where we are
  731.                             offset += ttPage[i].StripOffset;
  732.                         }
  733.  
  734.                         //Save the image page
  735.                         output.WriteDirectory();
  736.                     }
  737.                 }
  738.  
  739.                 //ADDED
  740.                 Tiff.SetTagExtender(m_parentExtender);
  741.             }
  742.             catch
  743.             {
  744.                 //ADDED
  745.                 Tiff.SetTagExtender(m_parentExtender);
  746.                
  747.                 //Error occured
  748.                 return false;
  749.             }
  750.  
  751.             //Return success
  752.             return true;
  753.         }
  754.     }
  755. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement