automatic commit
[folded-ctf.git] / rgb_image_subpixel.cc
1
2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify  //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation.                         //
6 //                                                                       //
7 // This program is distributed in the hope that it will be useful, but   //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of            //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
10 // General Public License for more details.                              //
11 //                                                                       //
12 // You should have received a copy of the GNU General Public License     //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>.  //
14 //                                                                       //
15 // Written by Francois Fleuret                                           //
16 // (C) Idiap Research Institute                                          //
17 //                                                                       //
18 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
19 ///////////////////////////////////////////////////////////////////////////
20
21 #include "rgb_image_subpixel.h"
22
23 RGBImageSubpixel::RGBImageSubpixel(int width, int height) : RGBImage(width * scale, height* scale) { }
24
25 RGBImageSubpixel::RGBImageSubpixel(RGBImage *image) : RGBImage(image->width() * scale, image->height() * scale) {
26   for(int y = 0; y < _height; y++) {
27     for(int x = 0; x < _width; x++) {
28       set_pixel(x, y,
29                 image->pixel(x / scale, y / scale, RGBImage::RED),
30                 image->pixel(x / scale, y / scale, RGBImage::GREEN),
31                 image->pixel(x / scale, y / scale, RGBImage::BLUE));
32     }
33   }
34 }
35
36 RGBImageSubpixel::~RGBImageSubpixel() { }
37
38 void RGBImageSubpixel::read_png(const char *filename) {
39   abort();
40 }
41
42 void RGBImageSubpixel::write_png(const char *filename) {
43   RGBImage tmp(_width / scale, _height / scale);
44   for(int y = 0; y < _height / scale; y++) {
45     for(int x = 0; x < _width / scale; x++) {
46       int sr = 0, sg = 0, sb = 0;
47       for(int yy = y * scale; yy < (y + 1) * scale; yy++) {
48         for(int xx = x * scale; xx < (x + 1) * scale; xx++) {
49           sr += int(_bit_plans[RED][yy][xx]);
50           sg += int(_bit_plans[GREEN][yy][xx]);
51           sb += int(_bit_plans[BLUE][yy][xx]);
52         }
53       }
54
55       tmp.set_pixel(x, y,
56                     sr / (scale * scale), sg / (scale * scale), sb / (scale * scale));
57     }
58   }
59   tmp.write_png(filename);
60 }
61
62
63 void RGBImageSubpixel::read_jpg(const char *filename) {
64   abort();
65 }
66
67 void RGBImageSubpixel::write_jpg(const char *filename, int quality) {
68   RGBImage tmp(_width / scale, _height / scale);
69
70   for(int y = 0; y < _height / scale; y++) {
71     for(int x = 0; x < _width / scale; x++) {
72       int sr = 0, sg = 0, sb = 0;
73       for(int yy = y * scale; yy < (y + 1) * scale; yy++) {
74         for(int xx = x * scale; xx < (x + 1) * scale; xx++) {
75           sr += int(_bit_plans[RED][yy][xx]);
76           sg += int(_bit_plans[GREEN][yy][xx]);
77           sb += int(_bit_plans[BLUE][yy][xx]);
78         }
79       }
80
81       tmp.set_pixel(x, y,
82                     sr / (scale * scale), sg / (scale * scale), sb / (scale * scale));
83     }
84   }
85
86   tmp.write_jpg(filename, quality);
87 }
88
89
90 void RGBImageSubpixel::draw_line(int thickness,
91                                  unsigned char r, unsigned char g, unsigned char b,
92                                  scalar_t x0, scalar_t y0, scalar_t x1, scalar_t y1) {
93   RGBImage::draw_line(int(thickness * scale),
94                       r, g, b,
95                       x0 * scale + scale/2, y0 * scale + scale/2,
96                       x1 * scale + scale/2, y1 * scale + scale/2);
97 }