| 
      1
     | 
    
      // ***************************************************************** -*- C++ -*-
 
     | 
  
  
    | 
      2
     | 
    
      /*
 
     | 
  
  
    | 
      3
     | 
    
       * Copyright (C) 2004, 2005 Brad Schick <brad@robotbattle.com>
 
     | 
  
  
    | 
      4
     | 
    
       * 
 
     | 
  
  
    | 
      5
     | 
    
       * This program is part of the PSAUtils distribution.
 
     | 
  
  
    | 
      6
     | 
    
       *
 
     | 
  
  
    | 
      7
     | 
    
       * This program is free software; you can redistribute it and/or
 
     | 
  
  
    | 
      8
     | 
    
       * modify it under the terms of the GNU General Public License
 
     | 
  
  
    | 
      9
     | 
    
       * as published by the Free Software Foundation; either version 2
 
     | 
  
  
    | 
      10
     | 
    
       * of the License, or (at your option) any later version.
 
     | 
  
  
    | 
      11
     | 
    
       * 
 
     | 
  
  
    | 
      12
     | 
    
       * This program is distributed in the hope that it will be useful,
 
     | 
  
  
    | 
      13
     | 
    
       * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
     | 
  
  
    | 
      14
     | 
    
       * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
     | 
  
  
    | 
      15
     | 
    
       * GNU General Public License for more details.
 
     | 
  
  
    | 
      16
     | 
    
       * 
 
     | 
  
  
    | 
      17
     | 
    
       * You should have received a copy of the GNU General Public License
 
     | 
  
  
    | 
      18
     | 
    
       * along with this program; if not, write to the Free Software
 
     | 
  
  
    | 
      19
     | 
    
       * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
     | 
  
  
    | 
      20
     | 
    
       */
 
     | 
  
  
    | 
      21
     | 
    
      /*
 
     | 
  
  
    | 
      22
     | 
    
        File:      metadata.cs
 
     | 
  
  
    | 
      23
     | 
    
        Version:   $Rev$
 
     | 
  
  
    | 
      24
     | 
    
        Author(s): Brad Schick <brad@robotbattle.com>
 
     | 
  
  
    | 
      25
     | 
    
        History:   12-Nov-04, brad: created
 
     | 
  
  
    | 
      26
     | 
    
       */
 
     | 
  
  
    | 
      27
     | 
    
      // *****************************************************************************
 
     | 
  
  
    | 
      28
     | 
    
      
 
     | 
  
  
    | 
      29
     | 
    
      
 
     | 
  
  
    | 
      30
     | 
    
      using System;
 
     | 
  
  
    | 
      31
     | 
    
      using System.ComponentModel;
 
     | 
  
  
    | 
      32
     | 
    
      using System.Collections;
 
     | 
  
  
    | 
      33
     | 
    
      using System.Diagnostics;
 
     | 
  
  
    | 
      34
     | 
    
      using System.Runtime.InteropServices;
 
     | 
  
  
    | 
      35
     | 
    
      using System.Text;
 
     | 
  
  
    | 
      36
     | 
    
      
 
     | 
  
  
    | 
      37
     | 
    
      
 
     | 
  
  
    | 
      38
     | 
    
      namespace PSAUtils
 
     | 
  
  
    | 
      39
     | 
    
      {
     | 
  
  
    | 
      40
     | 
    
          /// <summary>
 
     | 
  
  
    | 
      41
     | 
    
          /// Summary description for MetaData.
 
     | 
  
  
    | 
      42
     | 
    
          /// This object is not thread safe.
 
     | 
  
  
    | 
      43
     | 
    
          /// Users of this class should call the Dispose() method
 
     | 
  
  
    | 
      44
     | 
    
          /// when complete to free unmanaged resources.
 
     | 
  
  
    | 
      45
     | 
    
          /// </summary>
 
     | 
  
  
    | 
      46
     | 
    
          public class MetaData : System.ComponentModel.Component
 
     | 
  
  
    | 
      47
     | 
    
          {
     | 
  
  
    | 
      48
     | 
    
              // Data
 
     | 
  
  
    | 
      49
     | 
    
              private IntPtr image;
 
     | 
  
  
    | 
      50
     | 
    
              private string imageName;
 
     | 
  
  
    | 
      51
     | 
    
              private Object userData;
 
     | 
  
  
    | 
      52
     | 
    
              private EnumMetaDelegate userDelegate;
 
     | 
  
  
    | 
      53
     | 
    
      
 
     | 
  
  
    | 
      54
     | 
    
              // Public constants
 
     | 
  
  
    | 
      55
     | 
    
              public enum TypeId  {
     | 
  
  
    | 
      56
     | 
    
                  invalidTypeId, unsignedByte, asciiString, unsignedShort, 
 
     | 
  
  
    | 
      57
     | 
    
                  unsignedLong, unsignedRational, invalid6, undefined, 
 
     | 
  
  
    | 
      58
     | 
    
                  signedShort, signedLong, signedRational, 
 
     | 
  
  
    | 
      59
     | 
    
                  stringId, date, time, lastTypeId 
 
     | 
  
  
    | 
      60
     | 
    
              };
 
     | 
  
  
    | 
      61
     | 
    
      
 
     | 
  
  
    | 
      62
     | 
    
      
 
     | 
  
  
    | 
      63
     | 
    
              // Public methods
 
     | 
  
  
    | 
      64
     | 
    
              public delegate bool EnumMetaDelegate( string key, string val, Object user );
 
     | 
  
  
    | 
      65
     | 
    
      
 
     | 
  
  
    | 
      66
     | 
    
              public void EnumMeta(EnumMetaDelegate del, Object data)
 
     | 
  
  
    | 
      67
     | 
    
              {
     | 
  
  
    | 
      68
     | 
    
                  userData = data;
 
     | 
  
  
    | 
      69
     | 
    
                  userDelegate = del;
 
     | 
  
  
    | 
      70
     | 
    
                  EnumMetaDelegateExt localDel = new EnumMetaDelegateExt( EnumProcMapper );
 
     | 
  
  
    | 
      71
     | 
    
                  EnumMetaExt( image, localDel, IntPtr.Zero );
 
     | 
  
  
    | 
      72
     | 
    
              }
 
     | 
  
  
    | 
      73
     | 
    
      
 
     | 
  
  
    | 
      74
     | 
    
              // If not called all changes are thrown out
 
     | 
  
  
    | 
      75
     | 
    
              public void SaveChanges( )
 
     | 
  
  
    | 
      76
     | 
    
              {
     | 
  
  
    | 
      77
     | 
    
                  if( SaveImageExt(image) != 0 )
 
     | 
  
  
    | 
      78
     | 
    
                      throw new ApplicationException( "Could not save image: " + imageName );
 
     | 
  
  
    | 
      79
     | 
    
              }
 
     | 
  
  
    | 
      80
     | 
    
      
 
     | 
  
  
    | 
      81
     | 
    
              public string ReadMeta( string key )
 
     | 
  
  
    | 
      82
     | 
    
              {
     | 
  
  
    | 
      83
     | 
    
                  // limit of 2k for now
 
     | 
  
  
    | 
      84
     | 
    
                  StringBuilder buff = new StringBuilder(2048);
 
     | 
  
  
    | 
      85
     | 
    
                  if( ReadMetaExt(image, key, buff, buff.Capacity) != 0 )
 
     | 
  
  
    | 
      86
     | 
    
                      throw new ApplicationException( "Could not read " + key + " in image: "  + imageName );
 
     | 
  
  
    | 
      87
     | 
    
      
 
     | 
  
  
    | 
      88
     | 
    
                  return buff.ToString();
 
     | 
  
  
    | 
      89
     | 
    
              }
 
     | 
  
  
    | 
      90
     | 
    
      
 
     | 
  
  
    | 
      91
     | 
    
              public void ModifyMeta( string key, string val )
 
     | 
  
  
    | 
      92
     | 
    
              {
     | 
  
  
    | 
      93
     | 
    
                  // invalidTypeId causes type guess
 
     | 
  
  
    | 
      94
     | 
    
                  ModifyMeta( key, val, TypeId.invalidTypeId );
 
     | 
  
  
    | 
      95
     | 
    
              }
 
     | 
  
  
    | 
      96
     | 
    
      
 
     | 
  
  
    | 
      97
     | 
    
              public void ModifyMeta( string key, string val, TypeId type )
 
     | 
  
  
    | 
      98
     | 
    
              {
     | 
  
  
    | 
      99
     | 
    
                  if( ModifyMetaExt(image, key, val, (int)type) != 0)
 
     | 
  
  
    | 
      100
     | 
    
                      throw new ApplicationException( "Could not modify " + key + " in image: "  + imageName );
 
     | 
  
  
    | 
      101
     | 
    
              }
 
     | 
  
  
    | 
      102
     | 
    
      
 
     | 
  
  
    | 
      103
     | 
    
              public void AddMeta( string key, string val )
 
     | 
  
  
    | 
      104
     | 
    
              {
     | 
  
  
    | 
      105
     | 
    
                  // invalidTypeId causes type guess
 
     | 
  
  
    | 
      106
     | 
    
                  AddMeta( key, val, TypeId.invalidTypeId );
 
     | 
  
  
    | 
      107
     | 
    
              }
 
     | 
  
  
    | 
      108
     | 
    
      
 
     | 
  
  
    | 
      109
     | 
    
              public void AddMeta( string key, string val, TypeId type )
 
     | 
  
  
    | 
      110
     | 
    
              {
     | 
  
  
    | 
      111
     | 
    
                  if( AddMetaExt(image, key, val, (int)type) != 0)
 
     | 
  
  
    | 
      112
     | 
    
                      throw new ApplicationException( "Could not add " + key + " to image: "  + imageName );
 
     | 
  
  
    | 
      113
     | 
    
              }
 
     | 
  
  
    | 
      114
     | 
    
      
 
     | 
  
  
    | 
      115
     | 
    
              public void RemoveMeta( string key )
 
     | 
  
  
    | 
      116
     | 
    
              {
     | 
  
  
    | 
      117
     | 
    
                  if( RemoveMetaExt(image, key) != 0)
 
     | 
  
  
    | 
      118
     | 
    
                      throw new ApplicationException( "Could not remove " + key + " from image: "  + imageName );
 
     | 
  
  
    | 
      119
     | 
    
              }
 
     | 
  
  
    | 
      120
     | 
    
      
 
     | 
  
  
    | 
      121
     | 
    
          
 
     | 
  
  
    | 
      122
     | 
    
              /// <summary>
 
     | 
  
  
    | 
      123
     | 
    
              /// exivsimple.dll imports
 
     | 
  
  
    | 
      124
     | 
    
              /// </summary>
 
     | 
  
  
    | 
      125
     | 
    
              [DllImport("exivsimple.dll", EntryPoint="OpenFileImage")]
     | 
  
  
    | 
      126
     | 
    
              private static extern IntPtr OpenFileImageExt(string fileName);
 
     | 
  
  
    | 
      127
     | 
    
      
 
     | 
  
  
    | 
      128
     | 
    
              [DllImport("exivsimple.dll", EntryPoint="OpenMemImage")]
     | 
  
  
    | 
      129
     | 
    
              private static extern IntPtr OpenMemImageExt( [Out] byte[] data, int size);
 
     | 
  
  
    | 
      130
     | 
    
      
 
     | 
  
  
    | 
      131
     | 
    
              [DllImport("exivsimple.dll", EntryPoint="FreeImage")]
     | 
  
  
    | 
      132
     | 
    
              private static extern void FreeImageExt(IntPtr img);
 
     | 
  
  
    | 
      133
     | 
    
      
 
     | 
  
  
    | 
      134
     | 
    
              private delegate bool EnumMetaDelegateExt( string key, string val, IntPtr user );
 
     | 
  
  
    | 
      135
     | 
    
      
 
     | 
  
  
    | 
      136
     | 
    
              [DllImport("exivsimple.dll", EntryPoint="EnumMeta")]
     | 
  
  
    | 
      137
     | 
    
              private static extern void EnumMetaExt(IntPtr img, EnumMetaDelegateExt del, IntPtr user);
 
     | 
  
  
    | 
      138
     | 
    
      
 
     | 
  
  
    | 
      139
     | 
    
              [DllImport("exivsimple.dll", EntryPoint="SaveImage")]
     | 
  
  
    | 
      140
     | 
    
              private static extern int SaveImageExt(IntPtr img);
 
     | 
  
  
    | 
      141
     | 
    
      
 
     | 
  
  
    | 
      142
     | 
    
              [DllImport("exivsimple.dll", EntryPoint="ImageSize")]
     | 
  
  
    | 
      143
     | 
    
              private static extern int ImageSizeExt(IntPtr img);
 
     | 
  
  
    | 
      144
     | 
    
      
 
     | 
  
  
    | 
      145
     | 
    
              [DllImport("exivsimple.dll", EntryPoint="ImageData")]
     | 
  
  
    | 
      146
     | 
    
              private static extern int ImageDataExt(IntPtr img, [In] byte[] data, int size);
 
     | 
  
  
    | 
      147
     | 
    
      
 
     | 
  
  
    | 
      148
     | 
    
              [DllImport("exivsimple.dll", EntryPoint="ReadMeta")]
     | 
  
  
    | 
      149
     | 
    
              private static extern int ReadMetaExt(IntPtr img, string key, StringBuilder buff, int buffsize);
 
     | 
  
  
    | 
      150
     | 
    
      
 
     | 
  
  
    | 
      151
     | 
    
              [DllImport("exivsimple.dll", EntryPoint="ModifyMeta")]
     | 
  
  
    | 
      152
     | 
    
              private static extern int ModifyMetaExt(IntPtr img, string key, string val, int type);
 
     | 
  
  
    | 
      153
     | 
    
      
 
     | 
  
  
    | 
      154
     | 
    
              [DllImport("exivsimple.dll", EntryPoint="AddMeta")]
     | 
  
  
    | 
      155
     | 
    
              private static extern int AddMetaExt(IntPtr img, string key, string val, int type);
 
     | 
  
  
    | 
      156
     | 
    
      
 
     | 
  
  
    | 
      157
     | 
    
              [DllImport("exivsimple.dll", EntryPoint="RemoveMeta")]
     | 
  
  
    | 
      158
     | 
    
              private static extern int RemoveMetaExt(IntPtr img, string key);
 
     | 
  
  
    | 
      159
     | 
    
      
 
     | 
  
  
    | 
      160
     | 
    
              private bool EnumProcMapper( string key, string val, IntPtr user )
 
     | 
  
  
    | 
      161
     | 
    
              {
     | 
  
  
    | 
      162
     | 
    
                  return userDelegate( key, val, userData );
 
     | 
  
  
    | 
      163
     | 
    
              }
 
     | 
  
  
    | 
      164
     | 
    
      
 
     | 
  
  
    | 
      165
     | 
    
              /// <summary>
 
     | 
  
  
    | 
      166
     | 
    
              /// Required designer variable.
 
     | 
  
  
    | 
      167
     | 
    
              /// </summary>
 
     | 
  
  
    | 
      168
     | 
    
              private System.ComponentModel.Container components = null;
 
     | 
  
  
    | 
      169
     | 
    
      
 
     | 
  
  
    | 
      170
     | 
    
              public MetaData(System.ComponentModel.IContainer container, string file)
 
     | 
  
  
    | 
      171
     | 
    
              {
     | 
  
  
    | 
      172
     | 
    
                  ///
 
     | 
  
  
    | 
      173
     | 
    
                  /// Required for Windows.Forms Class Composition Designer support
 
     | 
  
  
    | 
      174
     | 
    
                  ///
 
     | 
  
  
    | 
      175
     | 
    
                  container.Add(this);
 
     | 
  
  
    | 
      176
     | 
    
                  InitializeComponent();
 
     | 
  
  
    | 
      177
     | 
    
      
 
     | 
  
  
    | 
      178
     | 
    
                  this.imageName = file;
 
     | 
  
  
    | 
      179
     | 
    
                  image = OpenFileImageExt( file );
 
     | 
  
  
    | 
      180
     | 
    
                  if( image == IntPtr.Zero )
 
     | 
  
  
    | 
      181
     | 
    
                      throw new ApplicationException( "Could not open file: " + file );
 
     | 
  
  
    | 
      182
     | 
    
              }
 
     | 
  
  
    | 
      183
     | 
    
      
 
     | 
  
  
    | 
      184
     | 
    
              public MetaData( string file )
 
     | 
  
  
    | 
      185
     | 
    
              {
     | 
  
  
    | 
      186
     | 
    
                  ///
 
     | 
  
  
    | 
      187
     | 
    
                  /// Required for Windows.Forms Class Composition Designer support
 
     | 
  
  
    | 
      188
     | 
    
                  ///
 
     | 
  
  
    | 
      189
     | 
    
                  InitializeComponent();
 
     | 
  
  
    | 
      190
     | 
    
      
 
     | 
  
  
    | 
      191
     | 
    
                  this.imageName = file;
 
     | 
  
  
    | 
      192
     | 
    
                  image = OpenFileImageExt( file );
 
     | 
  
  
    | 
      193
     | 
    
                  if( image == IntPtr.Zero )
 
     | 
  
  
    | 
      194
     | 
    
                      throw new ApplicationException( "Could not open file: " + file );
 
     | 
  
  
    | 
      195
     | 
    
              }
 
     | 
  
  
    | 
      196
     | 
    
      
 
     | 
  
  
    | 
      197
     | 
    
              public MetaData( byte[] data )
 
     | 
  
  
    | 
      198
     | 
    
              {
     | 
  
  
    | 
      199
     | 
    
                  ///
 
     | 
  
  
    | 
      200
     | 
    
                  /// Required for Windows.Forms Class Composition Designer support
 
     | 
  
  
    | 
      201
     | 
    
                  ///
 
     | 
  
  
    | 
      202
     | 
    
                  InitializeComponent();
 
     | 
  
  
    | 
      203
     | 
    
      
 
     | 
  
  
    | 
      204
     | 
    
                  this.imageName = "In Memory";
 
     | 
  
  
    | 
      205
     | 
    
                  image = OpenMemImageExt( data, data.Length );
 
     | 
  
  
    | 
      206
     | 
    
                  if( image == IntPtr.Zero )
 
     | 
  
  
    | 
      207
     | 
    
                      throw new ApplicationException( "Could not open in memory image" );
 
     | 
  
  
    | 
      208
     | 
    
              }
 
     | 
  
  
    | 
      209
     | 
    
      
 
     | 
  
  
    | 
      210
     | 
    
              public byte[] ImageData()
 
     | 
  
  
    | 
      211
     | 
    
              {
     | 
  
  
    | 
      212
     | 
    
                  int size = ImageSizeExt( image );
 
     | 
  
  
    | 
      213
     | 
    
      
 
     | 
  
  
    | 
      214
     | 
    
                  if( size <= 0 )
 
     | 
  
  
    | 
      215
     | 
    
                      throw new ApplicationException( "Could not get image size" );
 
     | 
  
  
    | 
      216
     | 
    
      
 
     | 
  
  
    | 
      217
     | 
    
                  byte[] data = new byte[size];
 
     | 
  
  
    | 
      218
     | 
    
                  if( ImageDataExt( image, data, size ) > 0 )
 
     | 
  
  
    | 
      219
     | 
    
                      return data;
 
     | 
  
  
    | 
      220
     | 
    
                  else
 
     | 
  
  
    | 
      221
     | 
    
                      throw new ApplicationException( "Could not read image data" );
 
     | 
  
  
    | 
      222
     | 
    
              }
 
     | 
  
  
    | 
      223
     | 
    
      
 
     | 
  
  
    | 
      224
     | 
    
              /// <summary> 
 
     | 
  
  
    | 
      225
     | 
    
              /// Clean up any resources being used.
 
     | 
  
  
    | 
      226
     | 
    
              /// </summary>
 
     | 
  
  
    | 
      227
     | 
    
              protected override void Dispose( bool disposing )
 
     | 
  
  
    | 
      228
     | 
    
              {
     | 
  
  
    | 
      229
     | 
    
                  try
 
     | 
  
  
    | 
      230
     | 
    
                  {
     | 
  
  
    | 
      231
     | 
    
                      if( disposing )
 
     | 
  
  
    | 
      232
     | 
    
                      {
     | 
  
  
    | 
      233
     | 
    
                          if(components != null)
 
     | 
  
  
    | 
      234
     | 
    
                          {
     | 
  
  
    | 
      235
     | 
    
                              components.Dispose();
 
     | 
  
  
    | 
      236
     | 
    
                          }
 
     | 
  
  
    | 
      237
     | 
    
                      }
 
     | 
  
  
    | 
      238
     | 
    
      
 
     | 
  
  
    | 
      239
     | 
    
                      // Release unmanaged resources. If disposing is false, 
 
     | 
  
  
    | 
      240
     | 
    
                      // only the following code is executed.
 
     | 
  
  
    | 
      241
     | 
    
                      if( image != IntPtr.Zero )
 
     | 
  
  
    | 
      242
     | 
    
                      {
     | 
  
  
    | 
      243
     | 
    
                          FreeImageExt( image );
 
     | 
  
  
    | 
      244
     | 
    
                          image = IntPtr.Zero;
 
     | 
  
  
    | 
      245
     | 
    
                      }
 
     | 
  
  
    | 
      246
     | 
    
                  }
 
     | 
  
  
    | 
      247
     | 
    
                  finally
 
     | 
  
  
    | 
      248
     | 
    
                  {
     | 
  
  
    | 
      249
     | 
    
                      base.Dispose( disposing );
 
     | 
  
  
    | 
      250
     | 
    
                  }
 
     | 
  
  
    | 
      251
     | 
    
              }
 
     | 
  
  
    | 
      252
     | 
    
      
 
     | 
  
  
    | 
      253
     | 
    
      
 
     | 
  
  
    | 
      254
     | 
    
              #region Component Designer generated code
 
     | 
  
  
    | 
      255
     | 
    
              /// <summary>
 
     | 
  
  
    | 
      256
     | 
    
              /// Required method for Designer support - do not modify
 
     | 
  
  
    | 
      257
     | 
    
              /// the contents of this method with the code editor.
 
     | 
  
  
    | 
      258
     | 
    
              /// </summary>
 
     | 
  
  
    | 
      259
     | 
    
              private void InitializeComponent()
 
     | 
  
  
    | 
      260
     | 
    
              {
     | 
  
  
    | 
      261
     | 
    
                  components = new System.ComponentModel.Container();
 
     | 
  
  
    | 
      262
     | 
    
              }
 
     | 
  
  
    | 
      263
     | 
    
              #endregion
 
     | 
  
  
    | 
      264
     | 
    
          }
 
     | 
  
  
    | 
      265
     | 
    
      }
 
     |