File: simage12.c

package info (click to toggle)
simage 1.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,908 kB
  • sloc: ansic: 14,231; sh: 12,587; cpp: 930; perl: 854; makefile: 617; lisp: 25
file content (197 lines) | stat: -rw-r--r-- 4,586 bytes parent folder | download | duplicates (4)
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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <simage.h>
#include <simage_private.h>
#include <string.h>

s_image * 
s_image_create(int w, int h, int components,
               unsigned char * prealloc /* | NULL */ )
{
  s_image * image = (s_image*) malloc(sizeof(s_image));
  image->width = w;
  image->height = h;
  image->components = components;
  image->order = SIMAGE_ORDER_RGB; 
  image->didalloc = 0;
  image->data = prealloc;
  if (image->data == NULL) {
    image->didalloc = 1;
    image->data = (unsigned char *) malloc(w*h*components);
  }
  
  /* needed for simage 1.6 */
  image->opendata = NULL; 
  image->oktoreadall = 1;
  image->openfilename = NULL;
  memset(&image->openfuncs, 0, sizeof(struct simage_open_funcs));

  /* return image struct */
  return (s_image*) image;
}

void 
s_image_destroy(s_image * image)
{
  if (image) {
    if (image->didalloc) free((void*)image->data);

    if (image->opendata) {
      image->openfuncs.close_func(image->opendata);
    }
    if (image->openfilename) {
      free((void*) image->openfilename);
    }
    free((void*)image);
  }
}

int 
s_image_width(s_image * image)
{
  if (image) return image->width;
  return 0;
}

int 
s_image_height(s_image * image)
{
  if (image) return image->height;
  return 0;
}

int 
s_image_components(s_image * image)
{
  if (image) return image->components;
  return 0;
}


int 
s_image_set_component_order(s_image * image, int order)
{
  int oldorder = image->order;
  image->order = order;
  return oldorder;
}

int 
s_image_get_component_order(s_image * image)
{
  return image->order;
}

unsigned char * 
s_image_data(s_image * image)
{
  if (image) {
    if (image->opendata && image->data == NULL) {
      int i;
      image->data = (unsigned char *) malloc(image->width*image->height*image->components);
      image->didalloc = 1;
      for (i = 0; i < image->height; i++) {
        (void) s_image_read_line(image, i,
                                 image->data+image->width*image->components);
      }
    }
    return image->data;
  }
  return NULL;
}

void 
s_image_set(s_image * image, int w, int h, int components,
            unsigned char * data, int copydata)
{
  if (image->width == w && image->height == h && image->components == components) {
    if (copydata) {
      if (!image->didalloc) {
        /* we shouldn't overwrite preallocated data */
        image->data = (unsigned char*) malloc(w*h*components);
        image->didalloc = 1;
      }
      memcpy(image->data, data, w*h*components);
    }
    else {
      if (image->didalloc) free((void*) image->data);
      image->data = data;
      image->didalloc = 0;
    }
  }
  else {
    if (image->didalloc) free((void*) image->data);
    image->width = w;
    image->height = h;
    image->components = components;
    if (copydata) {
      image->data = (unsigned char *) malloc(w*h*components);
      image->didalloc = 1;
      memcpy(image->data, data, w*h*components);
    }
    else {
      image->data = data;
      image->didalloc = 0;
    }
  }
  image->order = SIMAGE_ORDER_RGB;
}

s_image * 
s_image_load(const char * filename, s_image * prealloc /* | NULL */)
{
  unsigned char * data;
  int w,h,nc;
  
  data = simage_read_image(filename, &w, &h, &nc);
  if (data == NULL) return NULL;
  if (prealloc == NULL || 
      prealloc->width != w || 
      prealloc->height != h ||
      prealloc->components != nc) {
    prealloc = s_image_create(w, h, nc, data);
    prealloc->didalloc = 1; /* we did alloc this data */
  }
  else {
    /* copy into preallocated buffer */
    memcpy(prealloc->data, data, w*h*nc);
    
    /* we don't need this copy any more */
    simage_free_image(data);
  }
  prealloc->order = SIMAGE_ORDER_RGB;
  prealloc->openfilename = (char*) malloc(strlen(filename) + 1);
  strcpy(prealloc->openfilename, filename);
  return prealloc;
}

int 
s_image_save(const char * filename, s_image * image,
             s_params * params /* | NULL */)
{
  char * ext = NULL;
  if (params != NULL) {
    s_params_get(params, S_STRING_PARAM_TYPE, 
                 "file type", &ext,
                 NULL);
  }
  if (ext == NULL) {
    ext = (char*) strrchr(filename, '.');
    if (ext == NULL) return 0;
    
    /* skip period */
    ext++;
  }
  
  return simage_save_image(filename, 
                           s_image_data(image),
                           image->width,
                           image->height,
                           image->components,
                           ext);
}