summaryrefslogtreecommitdiff
path: root/html/download/im_copy.cpp
blob: cf0e0d534b47be9fdc5bf64eb2cb6111b4f0e5da (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
/* 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>]

    Example: im_copy test.tif test_proc.tif
*/

#include <im.h>
#include <im_util.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;
  }

  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], argv[3]? argv[3]: format, &error);
  if (!ofile)
    goto man_error;

  if (!argv[3])
    imFileSetInfo(ofile, compression);

  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)
      goto man_error;

    data = malloc(imImageDataSize(width, height, color_mode, data_type));

    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);
    }

    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;
  }

  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;
}