2 * leptonica-projective-transform - Example for pixProjectiveSampled()
4 * Copyright (C) 2013 Antonio Ospite <ospite@studenti.unina.it>
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.
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.
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/>.
22 #include <leptonica/allheaders.h>
27 #define X_CORRECTION 200
29 int main(int argc, char *argv[])
37 l_float32 *transform_coeffs;
38 PIX *transformed_image;
41 fprintf(stderr, "usage: %s <image>\n", argv[0]);
46 image = pixRead(argv[1]);
48 fprintf(stderr, "Cannot load image\n");
56 goto out_destroy_image;
59 ptaAddPt(src, WIDTH, 0);
60 ptaAddPt(src, WIDTH, HEIGHT);
61 ptaAddPt(src, 0, HEIGHT);
66 goto out_destroy_pta_src;
68 ptaAddPt(dst, X_CORRECTION, 0);
69 ptaAddPt(dst, WIDTH - X_CORRECTION, 0);
70 ptaAddPt(dst, WIDTH, HEIGHT);
71 ptaAddPt(dst, 0, HEIGHT);
73 transform_coeffs = NULL;
74 ret = getProjectiveXformCoeffs(dst, src, &transform_coeffs);
77 goto out_destroy_pta_dst;
80 /* Transform only a part of the whole image */
81 viewport = boxCreateValid(0, 0, WIDTH, HEIGHT);
82 if (viewport == NULL) {
84 goto out_free_transform_coeffs;
86 cropped_image = pixClipRectangle(image, viewport, NULL);
87 if (cropped_image == NULL) {
89 goto out_destroy_viewport;
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");
97 goto out_free_cropped_image;
100 ret = pixWrite("transformed_image.jpg", transformed_image, IFF_JFIF_JPEG);
109 pixDestroy(&transformed_image);
110 out_free_cropped_image:
111 pixDestroy(&cropped_image);
112 out_destroy_viewport:
113 boxDestroy(&viewport);
114 out_free_transform_coeffs:
115 FREE(transform_coeffs);