~ubuntu-branches/ubuntu/warty/xmedcon/warty

« back to all changes in this revision

Viewing changes to libs/dicom/image.c

  • Committer: Bazaar Package Importer
  • Author(s): Roland Marcus Rutschmann
  • Date: 2004-06-07 09:00:14 UTC
  • Revision ID: james.westby@ubuntu.com-20040607090014-t39n52qc9zjqqqkh
Tags: upstream-0.9.6
ImportĀ upstreamĀ versionĀ 0.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************
 
2
 * libdicom by Tony Voet *
 
3
 *************************/
 
4
/* 
 
5
 * $Id: image.c,v 1.2 2003/09/03 21:51:43 enlf Exp $
 
6
 */
 
7
#include <stdio.h>
 
8
#include <stdlib.h>
 
9
#include <string.h>
 
10
#include "dicom.h"
 
11
/* eNlf: BEGIN - comment out unwanted stuff */
 
12
/*
 
13
#include "jpeglib.h"
 
14
*/
 
15
/* eNlf: END   - comment out unwanted stuff */
 
16
 
 
17
/*******
 
18
 * new *
 
19
 *******/
 
20
 
 
21
IMAGE *dicom_new(int rgb,U16 frames,U16 w,U16 h)
 
22
{
 
23
  IMAGE *image;
 
24
 
 
25
  dicom_log(DEBUG,"dicom_new()");
 
26
 
 
27
  image=malloc(sizeof(IMAGE));
 
28
  if (!image)
 
29
  {
 
30
    dicom_log(ERROR,"Out of memory");
 
31
    return 0L;
 
32
  }
 
33
 
 
34
  image->rgb=rgb;
 
35
  image->frames=frames;
 
36
  image->w=w;
 
37
  image->h=h;
 
38
 
 
39
  if (rgb)
 
40
    image->data.rgb=malloc((unsigned)(frames*w*h)*3U);
 
41
  else
 
42
    image->data.gray=malloc((unsigned)(frames*w*h)*2U);
 
43
 
 
44
  if (!image->data.rgb)
 
45
  {
 
46
    dicom_log(ERROR,"Out of memory");
 
47
 
 
48
    eNlfSafeFree(image);
 
49
    return 0L;
 
50
  }
 
51
 
 
52
  return image;
 
53
}
 
54
 
 
55
/********
 
56
 * read *
 
57
 ********/
 
58
 
 
59
int dicom_read(const char *file,IMAGE **image,int *images,int parametric)
 
60
{
 
61
  SINGLE        *single;
 
62
  IMAGE         *new,*tmp;
 
63
 
 
64
  dicom_log(DEBUG,"dicom_read()");
 
65
 
 
66
  if (!file)
 
67
  {
 
68
    dicom_log(ERROR,"No file given");
 
69
    return -1;
 
70
  }
 
71
 
 
72
  if (!image || !images)
 
73
  {
 
74
    dicom_log(ERROR,"Argument missing");
 
75
    return -2;
 
76
  }
 
77
 
 
78
  if (dicom_open(file))
 
79
    return -3;
 
80
 
 
81
  for (*image=0L,*images=0;;)
 
82
  {
 
83
    single=dicom_single();
 
84
    if (!single)
 
85
      break;
 
86
 
 
87
    new=dicom_transform(single,parametric);
 
88
    if (new)
 
89
    {
 
90
      if (*image) 
 
91
        tmp=realloc(*image,(*images+1)*sizeof(IMAGE));
 
92
      else
 
93
        tmp=malloc(sizeof(IMAGE));
 
94
 
 
95
      if (!tmp)
 
96
      {
 
97
        dicom_log(ERROR,"Error reallocating memory");
 
98
        eNlfSafeFree(new->data.rgb);
 
99
      }
 
100
      else
 
101
      {
 
102
        *image=tmp;
 
103
        memcpy(*image+*images,new,sizeof(IMAGE));
 
104
        (*images)++;
 
105
      }
 
106
    }
 
107
 
 
108
    dicom_single_free();
 
109
  }
 
110
 
 
111
  if (*images==0)
 
112
  {
 
113
    dicom_log(ERROR,"No images found");
 
114
    /* eNlf: BEGIN -- changes for integration in MedCon */
 
115
    dicom_close();
 
116
    /* eNlf: END   -- changes for integration in MedCon */
 
117
    return -4;
 
118
  }
 
119
 
 
120
  return 0;
 
121
}
 
122
 
 
123
/********
 
124
 * free *
 
125
 ********/
 
126
 
 
127
void dicom_free(IMAGE *image,int images)
 
128
{
 
129
  int i;
 
130
 
 
131
  dicom_log(DEBUG,"dicom_free()");
 
132
 
 
133
  if (!image)
 
134
    return;
 
135
 
 
136
  for (i=0; i<images; i++)
 
137
    eNlfSafeFree(image[i].data.rgb);
 
138
 
 
139
  eNlfSafeFree(image);
 
140
}
 
141
 
 
142
/*********
 
143
 * write *
 
144
 *********/
 
145
 
 
146
int dicom_write(const char *file,const IMAGE *image)
 
147
{
 
148
  dicom_log(DEBUG,"dicom_write()");
 
149
 
 
150
  if (!file)
 
151
  {
 
152
    dicom_log(ERROR,"No file given");
 
153
    return -1;
 
154
  }
 
155
 
 
156
  if (!image)
 
157
  {
 
158
    dicom_log(ERROR,"No image given");
 
159
    return -2;
 
160
  }
 
161
 
 
162
  dicom_log(EMERGENCY,"DICOM write is not implemented yet");
 
163
 
 
164
  return -3;
 
165
}
 
166
/* eNlf: BEGIN - comment out unwanted stuff */
 
167
/***************
 
168
 * write ascii *
 
169
 ***************/
 
170
/*
 
171
int dicom_write_ascii(const char *file,const IMAGE *image,int width)
 
172
{
 
173
  static char gray[]=
 
174
    "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
 
175
 
 
176
  FILE  *stream;
 
177
  IMAGE *zoom;
 
178
  U16   frame,x,y,*pixel;
 
179
 
 
180
  dicom_log(DEBUG,"dicom_write_ascii()");
 
181
 
 
182
  if (!image)
 
183
  {
 
184
    dicom_log(ERROR,"No image given");
 
185
    return -1;
 
186
  }
 
187
 
 
188
  if (!file)
 
189
    stream=stdout;
 
190
  else
 
191
  {
 
192
    stream=fopen(file,"wb");
 
193
    if (!stream)
 
194
    {
 
195
      dicom_log(ERROR,"Unable to create ascii file");
 
196
      return -2;
 
197
    }
 
198
  }
 
199
 
 
200
  zoom=dicom_zoom(image,width,width*image->h/(image->w<<1),-1);
 
201
  if (!zoom)
 
202
    return -3;
 
203
 
 
204
  dicom_gray(zoom);
 
205
  dicom_max(zoom);
 
206
 
 
207
  pixel=zoom->data.gray;
 
208
 
 
209
  for (frame=zoom->frames; frame; frame--)
 
210
  {
 
211
    for (y=zoom->h; y; y--)
 
212
    {
 
213
      for (x=zoom->w; x; x--,pixel++)
 
214
        putc(gray[(int) 69**pixel/0xFFFF],stream);
 
215
      puts("");
 
216
    }
 
217
    puts("");
 
218
  }
 
219
 
 
220
  dicom_free(zoom,1);
 
221
 
 
222
  return 0;
 
223
}
 
224
*/
 
225
 
 
226
/**************
 
227
 * write jpeg *
 
228
 **************/
 
229
/*
 
230
int dicom_write_jpeg(const char *file,const IMAGE *image,int quality)
 
231
{
 
232
  struct jpeg_compress_struct   cinfo;
 
233
  struct jpeg_error_mgr         jerr;
 
234
  JSAMPROW                      line,target;
 
235
  FILE                          *stream;
 
236
  U16                           *source,l;
 
237
 
 
238
  dicom_log(DEBUG,"dicom_write_jpeg()");
 
239
 
 
240
  if (!file)
 
241
  {
 
242
    dicom_log(ERROR,"No file given");
 
243
    return -1;
 
244
  }
 
245
 
 
246
  if (!image)
 
247
  {
 
248
    dicom_log(ERROR,"No image given");
 
249
    return -2;
 
250
  }
 
251
 
 
252
  if (!image->rgb)
 
253
  {
 
254
    line=malloc(image->w*2);
 
255
    if (!line)
 
256
    {
 
257
      dicom_log(ERROR,"Out of memory");
 
258
      return -3;
 
259
    }
 
260
  }
 
261
 
 
262
  stream=fopen(file,"wb");
 
263
  if (!stream)
 
264
  {
 
265
    dicom_log(ERROR,"Unable to create jpeg file");
 
266
    return -4;
 
267
  }
 
268
 
 
269
  cinfo.err=jpeg_std_error(&jerr);
 
270
  jpeg_create_compress(&cinfo);
 
271
  jpeg_stdio_dest(&cinfo,stream);
 
272
 
 
273
  cinfo.image_width=image->w;
 
274
  cinfo.image_height=image->h*image->frames;
 
275
 
 
276
  if (image->rgb)
 
277
  {
 
278
    cinfo.input_components=3;
 
279
    cinfo.in_color_space=JCS_RGB;
 
280
  }
 
281
  else
 
282
  {
 
283
    cinfo.input_components=1;
 
284
    cinfo.in_color_space=JCS_GRAYSCALE;
 
285
  }
 
286
 
 
287
  jpeg_set_defaults(&cinfo);
 
288
  jpeg_set_quality(&cinfo,quality,-1);
 
289
  jpeg_start_compress(&cinfo,-1);
 
290
 
 
291
  while (cinfo.next_scanline<cinfo.image_height)
 
292
  {
 
293
    if (image->rgb)
 
294
      line=image->data.rgb+cinfo.next_scanline*image->w*3;
 
295
    else
 
296
    {
 
297
      source=image->data.gray+cinfo.next_scanline*image->w;
 
298
      target=line;
 
299
 
 
300
      for (l=image->w; l; l--)
 
301
        *target++=*source++>>8;
 
302
    }
 
303
 
 
304
    jpeg_write_scanlines(&cinfo,&line,1);
 
305
  }
 
306
 
 
307
  if (!image->rgb)
 
308
    eNlfSafeFree(line);
 
309
 
 
310
  jpeg_finish_compress(&cinfo);
 
311
  fclose(stream);
 
312
  jpeg_destroy_compress(&cinfo);
 
313
 
 
314
  return 0;
 
315
}
 
316
*/
 
317
/*************
 
318
 * write eps *
 
319
 *************/
 
320
/*
 
321
int dicom_write_eps(const char *file,const IMAGE *image)
 
322
{
 
323
  dicom_log(DEBUG,"dicom_write_eps()");
 
324
 
 
325
  if (!file)
 
326
  {
 
327
    dicom_log(ERROR,"No file given");
 
328
    return -1;
 
329
  }
 
330
 
 
331
  if (!image)
 
332
  {
 
333
    dicom_log(ERROR,"No image given");
 
334
    return -2;
 
335
  }
 
336
 
 
337
  dicom_log(EMERGENCY,"DICOM write EPS is not implemented yet");
 
338
 
 
339
  return -3;
 
340
}
 
341
*/
 
342
/*eNlf: END  - comment out unwanted stuff */
 
343