1 /*
2  * File: image.cc
3  *
4  * Copyright (C) 2005-2007 Jorge Arellano Cid <jcid@dillo.org>,
5  *                         Sebastian Geerken  <sgeerken@dillo.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  */
12 
13 /*
14  * This file implements image data transfer methods. It handles the transfer
15  * of data from an Image to a DwImage widget.
16  */
17 
18 #include "msg.h"
19 
20 #include "image.hh"
21 #include "dw/core.hh"
22 #include "dw/image.hh"
23 
24 using namespace dw::core;
25 
26 // Image to Object-ImgRenderer macro
27 #define I2IR(Image)  ((dw::core::ImgRenderer*)(Image->img_rndr))
28 
29 
30 /*
31  * Create and initialize a new image structure.
32  */
a_Image_new(void * layout,void * img_rndr,int32_t bg_color)33 DilloImage *a_Image_new(void *layout, void *img_rndr, int32_t bg_color)
34 {
35    DilloImage *Image;
36 
37    Image = dNew(DilloImage, 1);
38    Image->layout = layout;
39    Image->img_rndr = img_rndr;
40    Image->width = 0;
41    Image->height = 0;
42    Image->bg_color = bg_color;
43    Image->ScanNumber = 0;
44    Image->BitVec = NULL;
45    Image->State = IMG_Empty;
46 
47    Image->RefCount = 0;
48 
49    return Image;
50 }
51 
52 /*
53  * Create and initialize a new image structure with an image widget.
54  */
a_Image_new_with_dw(void * layout,const char * alt_text,int32_t bg_color)55 DilloImage *a_Image_new_with_dw(void *layout, const char *alt_text,
56                                 int32_t bg_color)
57 {
58    dw::Image *dw = new dw::Image(alt_text);
59    return a_Image_new(layout, (void*)(dw::core::ImgRenderer*)dw, bg_color);
60 }
61 
62 /*
63  * Return the image renderer as a widget. This is somewhat tricky,
64  * since simple casting leads to wrong (and hard to debug) results,
65  * because of multiple inheritance. This function can be used from C
66  * code, where only access to void* is possible.
67  */
a_Image_get_dw(DilloImage * Image)68 void *a_Image_get_dw(DilloImage *Image)
69 {
70    return (dw::Image*)(dw::core::ImgRenderer*)Image->img_rndr;
71 }
72 /*
73  * Deallocate an Image structure
74  */
Image_free(DilloImage * Image)75 static void Image_free(DilloImage *Image)
76 {
77    a_Bitvec_free(Image->BitVec);
78    dFree(Image);
79 }
80 
81 /*
82  * Unref and free if necessary
83  * Do nothing if the argument is NULL
84  */
a_Image_unref(DilloImage * Image)85 void a_Image_unref(DilloImage *Image)
86 {
87    _MSG(" %d ", Image->RefCount);
88    if (Image && --Image->RefCount == 0)
89       Image_free(Image);
90 }
91 
92 /*
93  * Add a reference to an Image struct
94  * Do nothing if the argument is NULL
95  */
a_Image_ref(DilloImage * Image)96 void a_Image_ref(DilloImage *Image)
97 {
98    if (Image)
99       ++Image->RefCount;
100 }
101 
102 /*
103  * Set initial parameters of the image
104  */
a_Image_set_parms(DilloImage * Image,void * v_imgbuf,DilloUrl * url,int version,uint_t width,uint_t height,DilloImgType type)105 void a_Image_set_parms(DilloImage *Image, void *v_imgbuf, DilloUrl *url,
106                        int version, uint_t width, uint_t height,
107                        DilloImgType type)
108 {
109    _MSG("a_Image_set_parms: width=%d height=%d iw=%d ih=%d\n",
110         width, height, Image->width, Image->height);
111 
112    /* Resize from 0,0 to width,height */
113    bool resize = true;
114    I2IR(Image)->setBuffer((Imgbuf*)v_imgbuf, resize);
115 
116    if (!Image->BitVec)
117       Image->BitVec = a_Bitvec_new(height);
118    Image->width = width;
119    Image->height = height;
120    Image->State = IMG_SetParms;
121 }
122 
123 /*
124  * Implement the write method
125  */
a_Image_write(DilloImage * Image,uint_t y)126 void a_Image_write(DilloImage *Image, uint_t y)
127 {
128    _MSG("a_Image_write\n");
129    dReturn_if_fail ( y < Image->height );
130 
131    /* Update the row in DwImage */
132    I2IR(Image)->drawRow(y);
133    a_Bitvec_set_bit(Image->BitVec, y);
134    Image->State = IMG_Write;
135 }
136 
137 /*
138  * Implement the close method
139  */
a_Image_close(DilloImage * Image)140 void a_Image_close(DilloImage *Image)
141 {
142    _MSG("a_Image_close\n");
143    I2IR(Image)->finish();
144 }
145 
146 /*
147  * Implement the abort method
148  */
a_Image_abort(DilloImage * Image)149 void a_Image_abort(DilloImage *Image)
150 {
151    _MSG("a_Image_abort\n");
152    I2IR(Image)->fatal();
153 }
154 
155