6035526fac72ae78889a1bce80e28f84b1257cc1
[experiments/leptonica.git] / leptonica-projective-transform.c
1 /* 
2  * leptonica-projective-transform - Example for pixProjectiveSampled()
3  *
4  * Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdio.h>
21 #include <errno.h>
22 #include <leptonica/allheaders.h>
23
24 #define WIDTH 800
25 #define HEIGHT 480
26
27 #define X_CORRECTION 200
28
29 int main(int argc, char *argv[])
30 {
31         int ret;
32         PIX *image;
33         BOX *viewport;
34         PIX *cropped_image;
35         PTA *src;
36         PTA *dst;
37         l_float32 *transform_coeffs;
38         PIX *transformed_image;
39
40         if (argc != 2) {
41                 fprintf(stderr, "usage: %s <image>\n", argv[0]);
42                 ret = -EINVAL;
43                 goto out;
44         }
45
46         image = pixRead(argv[1]);
47         if (image == NULL) {
48                 fprintf(stderr, "Cannot load image\n");
49                 ret = -EINVAL;
50                 goto out;
51         }
52
53         src = ptaCreate(4);
54         if (src == NULL) {
55                 ret = -ENOMEM;
56                 goto out_destroy_image;
57         }
58         ptaAddPt(src, 0, 0);
59         ptaAddPt(src, WIDTH, 0);
60         ptaAddPt(src, WIDTH, HEIGHT);
61         ptaAddPt(src, 0, HEIGHT);
62
63         dst = ptaCreate(4);
64         if (dst == NULL) {
65                 ret = -ENOMEM;
66                 goto out_destroy_pta_src;
67         }
68         ptaAddPt(dst, X_CORRECTION, 0);
69         ptaAddPt(dst, WIDTH - X_CORRECTION, 0);
70         ptaAddPt(dst, WIDTH, HEIGHT);
71         ptaAddPt(dst, 0, HEIGHT);
72
73         transform_coeffs = NULL;
74         ret = getProjectiveXformCoeffs(dst, src, &transform_coeffs);
75         if (ret != 0) {
76                 ret = -ENOMEM;
77                 goto out_destroy_pta_dst;
78         }
79
80         /* Transform only a part of the whole image */
81         viewport = boxCreateValid(0, 0, WIDTH, HEIGHT);
82         if (viewport == NULL) {
83                 ret = -EINVAL;
84                 goto out_free_transform_coeffs;
85         }
86         cropped_image = pixClipRectangle(image, viewport, NULL);
87         if (cropped_image == NULL) {
88                 ret = -EINVAL;
89                 goto out_destroy_viewport;
90         }
91
92         /* Apply the perspective transformation */
93         transformed_image = pixProjectiveSampled(cropped_image, transform_coeffs, L_BRING_IN_BLACK);
94         if (transformed_image == NULL) {
95                 fprintf(stderr, "Cannot create transformed image\n");
96                 ret = -EINVAL;
97                 goto out_free_cropped_image;
98         }
99
100         pixWrite("transformed_image.jpg", transformed_image, IFF_JFIF_JPEG);
101
102         ret = 0;
103
104         pixDestroy(&transformed_image);
105 out_free_cropped_image:
106         pixDestroy(&cropped_image);
107 out_destroy_viewport:
108         boxDestroy(&viewport);
109 out_free_transform_coeffs:
110         FREE(transform_coeffs);
111 out_destroy_pta_dst:
112         ptaDestroy(&dst);
113 out_destroy_pta_src:
114         ptaDestroy(&src);
115 out_destroy_image:
116         pixDestroy(&image);
117 out:
118         return ret;
119 }