summaryrefslogtreecommitdiff
path: root/im/test/im_info.cpp
blob: 00ff5ffe038f8b7c9824af9033cdde1912e424c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* IM 3 sample that returns information about a file.

  Needs "im.lib".

  Usage: im_info <file_name>

    Example: im_info test.tif
*/

#include <im.h>
#include <im_util.h>
#include <im_binfile.h>
#include <im_format_jp2.h>
#include <im_format_avi.h>
#include <im_format_wmv.h>

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

void PrintError(int error)
{
  switch (error)
  {
  case IM_ERR_OPEN:
    printf("Error Opening File.\n");
    break;
  case IM_ERR_MEM:
    printf("Insuficient memory.\n");
    break;
  case IM_ERR_ACCESS:
    printf("Error Accessing File.\n");
    break;
  case IM_ERR_DATA:
    printf("Image type not Suported.\n");
    break;
  case IM_ERR_FORMAT:
    printf("Invalid Format.\n");
    break;
  case IM_ERR_COMPRESS:
    printf("Invalid or unsupported compression.\n");
    break;
  default:
    printf("Unknown Error.\n");
  }
}

int FindZero(imbyte* data, int count)
{
  for (int i = 0; i < count; i++)
  {
    if (data[i] == 0)
      return 1;
  }
  return 0;
}

char* AttribData2Str(const void* data, int data_type)
{
  static char data_str[50] = "";

  switch(data_type)
  {
  case IM_BYTE:
    sprintf(data_str, "%3d", (int)(*((imbyte*)data)));
    break;
  case IM_USHORT:
    sprintf(data_str, "%5d", (int)(*((imushort*)data)));
    break;
  case IM_INT:  
    sprintf(data_str, "%5d", *((int*)data));
    break;
  case IM_FLOAT:
    sprintf(data_str, "%5.2f", (double)(*((float*)data)));
    break;
  case IM_CFLOAT:
    {
      float *c = (float*)data;
      sprintf(data_str, "%5.2g, %5.2f", (double)*c, (double)*(c+1));
    }
    break;
  }

  return data_str;
}

char* GetSizeDesc(double *size)
{
  char* size_desc;

  if (*size < 1024)
    size_desc = "b";
  else
  {
    *size /= 1024;

    if (*size < 1024)
      size_desc = "Kb";
    else
    {
      *size /= 1024;
      size_desc = "Mb";
    }
  }

  return size_desc;
}

unsigned long FileSize(const char* file_name)
{
  imBinFile* bfile = imBinFileOpen(file_name);
  if (!bfile) return 0;

  unsigned long file_size = imBinFileSize(bfile);

  imBinFileClose(bfile);
  return file_size;
}

void PrintImageInfo(const char* file_name)
{
  printf("IM Info\n");
  printf("  File Name:\n    %s\n", file_name);

  int error;
  imFile* ifile = imFileOpen(file_name, &error);
  if (!ifile) 
  {
    PrintError(error);
    return;
  }

  double file_size = FileSize(file_name);
  printf("  File Size: %.2f %s\n", file_size, GetSizeDesc(&file_size));

  char format[10];
  char compression[20];
  int image_count;
  imFileGetInfo(ifile, format, compression, &image_count);

  char format_desc[50];
  imFormatInfo(format, format_desc, NULL, NULL);
  printf("  Format: %s - %s\n", format, format_desc);
  printf("  Compression: %s\n", compression);
  printf("  Image Count: %d\n", image_count);
  
  for (int i = 0; i < image_count; i++)
  {
    int width, height, color_mode, data_type;

    error = imFileReadImageInfo(ifile, i, &width, &height, &color_mode, &data_type);
    if (error != IM_ERR_NONE)
    {
      PrintError(error);
      imFileClose(ifile);  
      return;
    }

    printf("  Image #%d\n", i);
    printf("    Width: %d\n", width);
    printf("    Height: %d\n", height);
    printf("    Color Space: %s\n", imColorModeSpaceName(color_mode));
    printf("      Has Alpha: %s\n", imColorModeHasAlpha(color_mode)? "Yes": "No");
    printf("      Is Packed: %s\n", imColorModeIsPacked(color_mode)? "Yes": "No");
    printf("      Is Top Down: %s\n", imColorModeIsTopDown(color_mode)? "Yes": "No");
    printf("    Data Type: %s\n", imDataTypeName(data_type));

    double image_size = imImageDataSize(width, height, color_mode, data_type);
    printf("    Data Size: %.2f %s\n", image_size, GetSizeDesc(&image_size));

    char* attrib_list[50];  // should be dynamic allocated
    int attrib_list_count;
    imFileGetAttributeList(ifile, attrib_list, &attrib_list_count);

    for (int a = 0; a < attrib_list_count; a++)
    {
      if (a == 0)
        printf("    Attributes:\n");

      int attrib_data_type, attrib_count;
      const void* attrib_data = imFileGetAttribute(ifile, attrib_list[a], &attrib_data_type, &attrib_count);

      if (attrib_count == 1)
        printf("      %s: %s\n", attrib_list[a], AttribData2Str(attrib_data, attrib_data_type));
      else if (attrib_data_type == IM_BYTE && FindZero((imbyte*)attrib_data, attrib_count))
        printf("      %s: %s\n", attrib_list[a], attrib_data);
      else
        printf("      %s: %s %s ...\n", attrib_list[a], AttribData2Str(attrib_data, attrib_data_type), AttribData2Str((imbyte*)attrib_data + imDataTypeSize(attrib_data_type), attrib_data_type));
    }
  }
    
  imFileClose(ifile);  
}

int main(int argc, char* argv[])
{
//  imFormatRegisterJP2();
//  imFormatRegisterAVI();
//  imFormatRegisterWMV();   

  if (argc < 2)
  {
    printf("Invalid number of arguments.\n");
    return 0;
  }

  PrintImageInfo(argv[1]);

  return 1;
}