summaryrefslogtreecommitdiff
path: root/test/im_copy.cpp
blob: dd0b82e4df77dd9dd75d1a46efa4a9f0e86b2cf9 (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
/* IM 3 sample that copies an image from one file to another. 
   It is good to test the file formats read and write.
   If the destiny does not supports the input image it aborts and returns an error.

  Needs "im.lib".

  Usage: im_copy <input_file_name> <output_file_name> [<output_format> [<output_compression]]

    Example: im_copy test.tif test_proc.tif
*/

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

#include <stdio.h>
#include <stdlib.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 main(int argc, char* argv[])
{
  if (argc < 3)
  {
    printf("Invalid number of arguments.\n");
    return 0;
  }

  imFormatRegisterAVI();
  imFormatRegisterWMV();

  void* data = NULL;
  imFile* ifile = NULL;
  imFile* ofile = NULL;

  int error;
  ifile = imFileOpen(argv[1], &error);
  if (!ifile) 
    goto man_error;

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

  ofile = imFileNew(argv[2], (argc < 3)? format: argv[3], &error);
  if (!ofile)
    goto man_error;

  if (argc < 4)
    imFileSetInfo(ofile, compression);
  else
    imFileSetInfo(ofile, argv[4]);

  for (int i = 0; i < image_count; i++)
  {
    int size, max_size = 0;
    int width, height, color_mode, data_type;
    error = imFileReadImageInfo(ifile, i, &width, &height, &color_mode, &data_type);
    if (error != IM_ERR_NONE)
      goto man_error;

    size = imImageDataSize(width, height, color_mode, data_type);

    if (size > max_size)
    {
      data = realloc(data, size);
      max_size = size;
    }

    error = imFileReadImageData(ifile, data, 0, -1);
    if (error != IM_ERR_NONE)
      goto man_error;
    
    char* attrib_list[50];
    int attrib_list_count;
    imFileGetAttributeList(ifile, attrib_list, &attrib_list_count);

    for (int a = 0; a < attrib_list_count; a++)
    {
      int attrib_data_type, attrib_count;
      const void* attrib_data = imFileGetAttribute(ifile, attrib_list[a], &attrib_data_type, &attrib_count);
      imFileSetAttribute(ofile, attrib_list[a], attrib_data_type, attrib_count, attrib_data);
    }

    if (imColorModeSpace(color_mode) == IM_MAP)
    {
      long palette[256];
      int palette_count;
      imFileGetPalette(ifile, palette, &palette_count);
      imFileSetPalette(ifile, palette, palette_count);
    }

    error = imFileWriteImageInfo(ofile, width, height, color_mode, data_type);
    if (error != IM_ERR_NONE)
      goto man_error;

    error = imFileWriteImageData(ofile, data);
    if (error != IM_ERR_NONE)
      goto man_error;

    printf(".");
  }
  printf("done");

  free(data);
  imFileClose(ifile);  
  imFileClose(ofile);  

  return 1;

man_error:
  PrintError(error);
  if (data) free(data);
  if (ifile) imFileClose(ifile);
  if (ofile) imFileClose(ofile);
  return 0;
}