~noskcaj/ubuntu/trusty/gthumb/3.2.5

« back to all changes in this revision

Viewing changes to extensions/file_tools/gth-file-tool-enhance.c

  • Committer: Package Import Robot
  • Author(s): David Paleino
  • Date: 2011-12-22 22:40:29 UTC
  • mfrom: (5.2.19 sid)
  • Revision ID: package-import@ubuntu.com-20111222224029-l58g65u1nfa6ojtg
Tags: 3:2.14.1-1
* New upstream version (Closes: #652692)
* Patches refreshed
* Bump build-dependencies requirements
* Fix FTBFS, added missing #include
* debian/watch fixed to point to new location (.xz tarballs)

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 
37
37
 
38
38
typedef struct {
39
 
        GtkWidget    *viewer_page;
40
 
        GthHistogram *histogram;
41
 
        Levels       *levels;
 
39
        GtkWidget       *viewer_page;
 
40
        cairo_surface_t *source;
 
41
        cairo_surface_t *destination;
 
42
        GthHistogram    *histogram;
 
43
        Levels          *levels;
42
44
} EnhanceData;
43
45
 
44
46
 
108
110
 
109
111
 
110
112
static void
111
 
adjust_levels_init (GthPixbufTask *pixop)
 
113
enhance_before (GthAsyncTask *task,
 
114
                gpointer      user_data)
112
115
{
113
 
        EnhanceData *data = pixop->data;
114
 
        int          channel;
115
 
 
116
 
        copy_source_to_destination (pixop);
117
 
        data->histogram = gth_histogram_new ();
118
 
        gth_histogram_calculate (data->histogram, pixop->src);
119
 
 
120
 
        data->levels = g_new0 (Levels, 1);
121
 
 
122
 
        for (channel = 0; channel < GTH_HISTOGRAM_N_CHANNELS; channel++) {
123
 
                data->levels->gamma[channel]       = 1.0;
124
 
                data->levels->low_input[channel]   = 0;
125
 
                data->levels->high_input[channel]  = 255;
126
 
                data->levels->low_output[channel]  = 0;
127
 
                data->levels->high_output[channel] = 255;
128
 
        }
129
 
 
130
 
        for (channel = 1; channel < GTH_HISTOGRAM_N_CHANNELS - 1; channel++)
131
 
                levels_channel_auto (data->levels, data->histogram, channel);
 
116
        gth_task_progress (GTH_TASK (task), _("White balance correction"), NULL, TRUE, 0.0);
132
117
}
133
118
 
134
119
 
185
170
}
186
171
 
187
172
 
188
 
static void
189
 
adjust_levels_step (GthPixbufTask *pixop)
 
173
static gpointer
 
174
enhance_exec (GthAsyncTask *task,
 
175
              gpointer      user_data)
190
176
{
191
 
        EnhanceData *data = pixop->data;
192
 
 
193
 
        pixop->dest_pixel[RED_PIX]   = levels_func (pixop->src_pixel[RED_PIX], data->levels, RED_PIX);
194
 
        pixop->dest_pixel[GREEN_PIX] = levels_func (pixop->src_pixel[GREEN_PIX], data->levels, GREEN_PIX);
195
 
        pixop->dest_pixel[BLUE_PIX]  = levels_func (pixop->src_pixel[BLUE_PIX], data->levels, BLUE_PIX);
196
 
 
197
 
        if (pixop->has_alpha)
198
 
                pixop->dest_pixel[ALPHA_PIX] = pixop->src_pixel[ALPHA_PIX];
 
177
        EnhanceData     *enhance_data = user_data;
 
178
        int              channel;
 
179
        cairo_format_t   format;
 
180
        int              width;
 
181
        int              height;
 
182
        int              source_stride;
 
183
        int              destination_stride;
 
184
        unsigned char   *p_source_line;
 
185
        unsigned char   *p_destination_line;
 
186
        unsigned char   *p_source;
 
187
        unsigned char   *p_destination;
 
188
        gboolean         cancelled;
 
189
        double           progress;
 
190
        gboolean         terminated;
 
191
        int              x, y;
 
192
        unsigned char    red, green, blue, alpha;
 
193
 
 
194
        /* initialize some extra data */
 
195
 
 
196
        enhance_data->histogram = gth_histogram_new ();
 
197
        gth_histogram_calculate_for_image (enhance_data->histogram, enhance_data->source);
 
198
 
 
199
        enhance_data->levels = g_new0 (Levels, 1);
 
200
        for (channel = 0; channel < GTH_HISTOGRAM_N_CHANNELS; channel++) {
 
201
                enhance_data->levels->gamma[channel]       = 1.0;
 
202
                enhance_data->levels->low_input[channel]   = 0;
 
203
                enhance_data->levels->high_input[channel]  = 255;
 
204
                enhance_data->levels->low_output[channel]  = 0;
 
205
                enhance_data->levels->high_output[channel] = 255;
 
206
        }
 
207
 
 
208
        for (channel = 1; channel < GTH_HISTOGRAM_N_CHANNELS - 1; channel++)
 
209
                levels_channel_auto (enhance_data->levels, enhance_data->histogram, channel);
 
210
 
 
211
        /* convert the image */
 
212
 
 
213
        format = cairo_image_surface_get_format (enhance_data->source);
 
214
        width = cairo_image_surface_get_width (enhance_data->source);
 
215
        height = cairo_image_surface_get_height (enhance_data->source);
 
216
        source_stride = cairo_image_surface_get_stride (enhance_data->source);
 
217
 
 
218
        enhance_data->destination = cairo_image_surface_create (format, width, height);
 
219
        cairo_surface_flush (enhance_data->destination);
 
220
        destination_stride = cairo_image_surface_get_stride (enhance_data->destination);
 
221
        p_source_line = cairo_image_surface_get_data (enhance_data->source);
 
222
        p_destination_line = cairo_image_surface_get_data (enhance_data->destination);
 
223
        for (y = 0; y < height; y++) {
 
224
                gth_async_task_get_data (task, NULL, &cancelled, NULL);
 
225
                if (cancelled)
 
226
                        return NULL;
 
227
 
 
228
                progress = (double) y / height;
 
229
                gth_async_task_set_data (task, NULL, NULL, &progress);
 
230
 
 
231
                p_source = p_source_line;
 
232
                p_destination = p_destination_line;
 
233
                for (x = 0; x < width; x++) {
 
234
                        CAIRO_GET_RGBA (p_source, red, green, blue, alpha);
 
235
                        red   = levels_func (red, enhance_data->levels, RED_PIX);
 
236
                        green = levels_func (green, enhance_data->levels, GREEN_PIX);
 
237
                        blue  = levels_func (blue, enhance_data->levels, BLUE_PIX);
 
238
                        CAIRO_SET_RGBA (p_destination, red, green, blue, alpha);
 
239
 
 
240
                        p_source += 4;
 
241
                        p_destination += 4;
 
242
                }
 
243
                p_source_line += source_stride;
 
244
                p_destination_line += destination_stride;
 
245
        }
 
246
 
 
247
        cairo_surface_mark_dirty (enhance_data->destination);
 
248
        terminated = TRUE;
 
249
        gth_async_task_set_data (task, &terminated, NULL, NULL);
 
250
 
 
251
        return NULL;
199
252
}
200
253
 
201
254
 
202
255
static void
203
 
adjust_levels_release (GthPixbufTask *pixop,
204
 
                       GError        *error)
 
256
enhance_after (GthAsyncTask *task,
 
257
               GError       *error,
 
258
               gpointer      user_data)
205
259
{
206
 
        EnhanceData *data = pixop->data;
 
260
        EnhanceData *enhance_data = user_data;
207
261
 
208
262
        if (error == NULL)
209
 
                gth_image_viewer_page_set_pixbuf (GTH_IMAGE_VIEWER_PAGE (data->viewer_page), pixop->dest, TRUE);
210
 
 
211
 
        g_object_unref (data->histogram);
212
 
        data->histogram = NULL;
213
 
        g_free (data->levels);
214
 
        data->levels = NULL;
 
263
                gth_image_viewer_page_set_image (GTH_IMAGE_VIEWER_PAGE (enhance_data->viewer_page), enhance_data->destination, TRUE);
 
264
 
 
265
        g_object_unref (enhance_data->histogram);
 
266
        enhance_data->histogram = NULL;
 
267
 
 
268
        g_free (enhance_data->levels);
 
269
        enhance_data->levels = NULL;
215
270
}
216
271
 
217
272
 
218
273
static void
219
 
adjust_levels_destroy_data (gpointer user_data)
 
274
enhance_data_free (gpointer user_data)
220
275
{
221
 
        EnhanceData *data = user_data;
 
276
        EnhanceData *enhance_data = user_data;
222
277
 
223
 
        g_object_unref (data->viewer_page);
224
 
        g_free (data);
 
278
        g_object_unref (enhance_data->viewer_page);
 
279
        cairo_surface_destroy (enhance_data->destination);
 
280
        cairo_surface_destroy (enhance_data->source);
 
281
        g_free (enhance_data);
225
282
}
226
283
 
227
284
 
228
285
static void
229
286
gth_file_tool_enhance_activate (GthFileTool *base)
230
287
{
231
 
        GtkWidget   *window;
232
 
        GtkWidget   *viewer_page;
233
 
        GtkWidget   *viewer;
234
 
        GdkPixbuf   *src_pixbuf;
235
 
        EnhanceData *data;
236
 
        GthTask     *task;
 
288
        GtkWidget       *window;
 
289
        GtkWidget       *viewer_page;
 
290
        GtkWidget       *viewer;
 
291
        cairo_surface_t *image;
 
292
        EnhanceData     *enhance_data;
 
293
        GthTask         *task;
237
294
 
238
295
        window = gth_file_tool_get_window (base);
239
296
        viewer_page = gth_browser_get_viewer_page (GTH_BROWSER (window));
241
298
                return;
242
299
 
243
300
        viewer = gth_image_viewer_page_get_image_viewer (GTH_IMAGE_VIEWER_PAGE (viewer_page));
244
 
        src_pixbuf = gth_image_viewer_get_current_pixbuf (GTH_IMAGE_VIEWER (viewer));
245
 
        if (src_pixbuf == NULL)
 
301
        image = gth_image_viewer_get_current_image (GTH_IMAGE_VIEWER (viewer));
 
302
        if (image == NULL)
246
303
                return;
247
304
 
248
 
        data = g_new0 (EnhanceData, 1);
249
 
        data->viewer_page = g_object_ref (viewer_page);
250
 
        data->histogram = NULL;
251
 
        data->levels = NULL;
252
 
        task = gth_pixbuf_task_new (_("White balance correction"),
253
 
                                    FALSE,
254
 
                                    adjust_levels_init,
255
 
                                    adjust_levels_step,
256
 
                                    adjust_levels_release,
257
 
                                    data,
258
 
                                    adjust_levels_destroy_data);
259
 
        gth_pixbuf_task_set_source (GTH_PIXBUF_TASK (task), src_pixbuf);
 
305
        enhance_data = g_new0 (EnhanceData, 1);
 
306
        enhance_data->viewer_page = g_object_ref (viewer_page);
 
307
        enhance_data->source = cairo_surface_reference (image);
 
308
        enhance_data->histogram = NULL;
 
309
        enhance_data->levels = NULL;
 
310
        task = gth_async_task_new (enhance_before,
 
311
                                   enhance_exec,
 
312
                                   enhance_after,
 
313
                                   enhance_data,
 
314
                                   enhance_data_free);
260
315
        gth_browser_exec_task (GTH_BROWSER (window), task, FALSE);
261
316
 
262
317
        g_object_unref (task);