summaryrefslogtreecommitdiff
path: root/im/test/im_view.c
blob: e271394eb31f98a53a96a291ac90f7b35888550c (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
/* IM 3 sample that shows an image.

  Needs "im.lib", "iup.lib", "cd.lib" and "cdiup.lib".

  Usage: im_view <file_name>

    Example: im_view test.tif

    
  Click on image to open another file.
*/

#include <iup.h>
#include <cd.h>
#include <cdiup.h>
#include <im.h>
#include <im_image.h>

#include <stdio.h>
#include <string.h>


static int disable_repaint = 0; /* used to optimize repaint, while opening a new file */

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

static int cbRepaint(Ihandle* iup_canvas)
{
  cdCanvas* cd_canvas = (cdCanvas*)IupGetAttribute(iup_canvas, "cdCanvas");
  imImage* image = (imImage*)IupGetAttribute(iup_canvas, "imImage");

  if (!cd_canvas || disable_repaint)
    return IUP_DEFAULT;

  cdCanvasActivate(cd_canvas);
  cdCanvasClear(cd_canvas);

  if (!image)
    return IUP_DEFAULT;

  imcdCanvasPutImage(cd_canvas, image, 0, 0, image->width, image->height, 0, 0, 0, 0);
  
  cdCanvasFlush(cd_canvas);
  
  return IUP_DEFAULT;
}

static void ShowImage(char* file_name, Ihandle* iup_dialog)
{
  int error;
  imImage* image = (imImage*)IupGetAttribute(iup_dialog, "imImage");
  if (image) imImageDestroy(image);
  IupSetAttribute(iup_dialog, "imImage", NULL);

  image = imFileImageLoadBitmap(file_name, 0, &error);
  if (error) PrintError(error);
  if (!image) return;

  IupSetAttribute(iup_dialog, "imImage", (char*)image);
  IupStoreAttribute(iup_dialog, "TITLE", file_name);

  cbRepaint(iup_dialog); /* we can do this because canvas inherit attributes from the dialog */
}

static int cbButton(Ihandle* iup_canvas, int but, int pressed)
{
  char file_name[200] = "*.*";

  if (but != IUP_BUTTON1 || !pressed)
    return IUP_DEFAULT;
  
  disable_repaint = 1;
  if (IupGetFile(file_name) != 0)
  {
    disable_repaint = 0;
    return IUP_DEFAULT;
  }

  disable_repaint = 0;
  ShowImage(file_name, IupGetDialog(iup_canvas));
  
  return IUP_DEFAULT;
}

static int cbClose(Ihandle* iup_dialog)
{
  cdCanvas* cd_canvas = (cdCanvas*)IupGetAttribute(iup_dialog, "cdCanvas");
  imImage* image = (imImage*)IupGetAttribute(iup_dialog, "imImage");

  if (cd_canvas) cdKillCanvas(cd_canvas);
  if (image) imImageDestroy(image);

  IupSetAttribute(iup_dialog, "cdCanvas", NULL);
  IupSetAttribute(iup_dialog, "imImage", NULL);

  return IUP_CLOSE;
}

static Ihandle* CreateDialog(void)
{
  Ihandle *iup_dialog;
  Ihandle *iup_canvas;
  cdCanvas* cd_canvas;

  iup_canvas = IupCanvas("do_nothing");
  IupSetAttribute(iup_canvas, IUP_BUTTON_CB, "cbButton");
  IupSetAttribute(iup_canvas, IUP_ACTION, "cbRepaint");
  
  iup_dialog = IupDialog(iup_canvas);
  IupSetAttribute(iup_dialog, IUP_CLOSE_CB, "cbClose");
  IupSetAttribute(iup_dialog, IUP_SIZE, "HALFxHALF");

  IupSetFunction("cbRepaint", (Icallback)cbRepaint);
  IupSetFunction("cbButton", (Icallback)cbButton);
  IupSetFunction("cbClose", (Icallback)cbClose);

  IupMap(iup_dialog);

  cd_canvas = cdCreateCanvas(CD_IUP, iup_canvas);
  IupSetAttribute(iup_dialog, "cdCanvas", (char*)cd_canvas);

  return iup_dialog;
}

//#include "im_format_avi.h"

int main(int argc, char* argv[])
{
  Ihandle* dlg;

  //imFormatRegisterAVI();
  IupOpen(&argc, &argv);

  dlg = CreateDialog();

  IupShow(dlg);
  
  /* Try to get a file name from the command line. */
  if (argc > 1)
  {
    ShowImage(argv[1], dlg);
  }
  else   
  {
    char file_name[1024] = "*.*";
    if (IupGetFile(file_name) == 0)
    {
      ShowImage(file_name, dlg);
    }
  }
                                   
  IupMainLoop();
  IupDestroy(dlg);
  IupClose();

  return 1;
}