1 /*
2  */
3 
4 /*
5 
6     Copyright (C) 2014 Ferrero Andrea
7 
8     This program is free software: you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21 
22  */
23 
24 /*
25 
26     These files are distributed with PhotoFlow - http://aferrero2707.github.io/PhotoFlow/
27 
28  */
29 
30 #ifndef PF_RAW_MATRIX_H
31 #define PF_RAW_MATRIX_H
32 
33 #include <glibmm.h>
34 #include "array2d.hh"
35 
36 namespace PF {
37 
38 
39 	//typedef guint8 raw_pixel_t[sizeof(float)/sizeof(guint8)+1];
40   typedef float raw_pixel_t[2];
41 
42 	/*
43   struct RawPixel
44   {
45     float data;
46     unsigned char color;
47     //float color;
48   };
49 	*/
50 
51   class RawMatrixRow
52   {
53     raw_pixel_t* pixels;
54   public:
RawMatrixRow(raw_pixel_t * px)55     RawMatrixRow( raw_pixel_t* px ): pixels ( px )
56     {
57       //std::cout<<"RawMatrixRow::RawMatrixRow(): pixels="<<pixels<<std::endl;
58     }
59 
RawMatrixRow(const RawMatrixRow & row)60     RawMatrixRow( const RawMatrixRow& row ): pixels( row.get_pixels() )
61     {
62       //std::cout<<"RawMatrixRow::RawMatrixRow( const RawMatrixRow& row ): pixels="<<pixels<<std::endl;
63     }
64 
get_pixels() const65     raw_pixel_t* get_pixels() const { return pixels; }
66 
operator [](int c)67     float& operator[](int c) {
68       //std::cout<<"RawMatrixRow::operator[]: pixels="<<pixels<<std::endl;
69       return *((float*)&(pixels[c]));
70     }
71 
color(int c)72     float& color(int c) {
73       //return *((guint8*)&(pixels[c])+sizeof(float));
74       return ( *((float*)&(pixels[c])+1) );
75       //return pixels[c].color;
76     }
icolor(int c)77     guint8 icolor(int c) {
78       //return *((guint8*)&(pixels[c])+sizeof(float));
79       return( (guint8)color(c) );
80       //return pixels[c].color;
81     }
82  };
83 
84 
85   class RawMatrix
86   {
87     unsigned int width, height;
88     unsigned int r_offset, c_offset;
89     raw_pixel_t **rbuf;
90     raw_pixel_t **rows;
91     raw_pixel_t *buf;
92 
93   public:
RawMatrix()94     RawMatrix():
95       width( 0 ), height( 0 ),
96       r_offset( 0 ), c_offset( 0 ),
97       rbuf( NULL ), rows( NULL ), buf( NULL )
98     {
99     }
~RawMatrix()100     ~RawMatrix()
101     {
102       if( rbuf ) free( rbuf );
103       if( buf ) free( buf );
104     }
105 
GetWidth()106     unsigned int GetWidth() { return width; }
GetHeight()107     unsigned int GetHeight() { return height; }
108 
init(unsigned int w,unsigned int h,unsigned int r_offs,unsigned int c_offs,bool do_allocation=false)109     void init(unsigned int w, unsigned int h, unsigned int r_offs, unsigned int c_offs, bool do_allocation=false)
110     {
111       width = w;
112       height = h;
113       r_offset = r_offs;
114       c_offset = c_offs;
115       rbuf = (raw_pixel_t**)realloc( buf, sizeof(raw_pixel_t*)*height );
116       rows = rbuf - r_offset;
117       for( unsigned int i = 0; i < height; i++ )
118         rbuf[i] = NULL;
119       if( do_allocation ) {
120         buf = (raw_pixel_t*)realloc( buf, sizeof(raw_pixel_t*)*height*width );
121         raw_pixel_t* ptr = buf;
122         for( unsigned int row = 0; row < height; row++ ) {
123           set_row( row+r_offset, ptr );
124           ptr += width;
125         }
126       }
127     }
128 
GetBuffer()129     raw_pixel_t* GetBuffer() { return buf; }
130 
131 
set_row(unsigned int row,raw_pixel_t * ptr)132     void set_row( unsigned int row, raw_pixel_t* ptr )
133     {
134       rows[row] = ptr - c_offset;
135       //std::cout<<"RawMatrix::set_row("<<row<<","<<(void*)ptr<<": rows["<<row<<"]="<<rows[row]<<"  c_offset="<<c_offset<<std::endl;
136     }
137 
operator [](int r)138     RawMatrixRow operator[](int r) {
139       //std::cout<<"RawMatrix::operator[]: rows["<<r<<"]="<<rows[r]<<"  r_offset="<<r_offset<<std::endl;
140       return( RawMatrixRow(rows[r]) );
141     }
142   };
143 
144 }
145 
146 #endif
147