1141cc406Sopenharmony_ci/* sane - Scanner Access Now Easy.
2141cc406Sopenharmony_ci
3141cc406Sopenharmony_ci   Copyright (C) 2020 Thierry HUCHARD <thierry@ordissimo.com>
4141cc406Sopenharmony_ci
5141cc406Sopenharmony_ci   This file is part of the SANE package.
6141cc406Sopenharmony_ci
7141cc406Sopenharmony_ci   SANE is free software; you can redistribute it and/or modify it under
8141cc406Sopenharmony_ci   the terms of the GNU General Public License as published by the Free
9141cc406Sopenharmony_ci   Software Foundation; either version 3 of the License, or (at your
10141cc406Sopenharmony_ci   option) any later version.
11141cc406Sopenharmony_ci
12141cc406Sopenharmony_ci   SANE is distributed in the hope that it will be useful, but WITHOUT
13141cc406Sopenharmony_ci   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14141cc406Sopenharmony_ci   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15141cc406Sopenharmony_ci   for more details.
16141cc406Sopenharmony_ci
17141cc406Sopenharmony_ci   You should have received a copy of the GNU General Public License
18141cc406Sopenharmony_ci   along with sane; see the file COPYING.
19141cc406Sopenharmony_ci   If not, see <https://www.gnu.org/licenses/>.
20141cc406Sopenharmony_ci
21141cc406Sopenharmony_ci   This file implements a SANE backend for eSCL scanners.  */
22141cc406Sopenharmony_ci
23141cc406Sopenharmony_ci#define DEBUG_DECLARE_ONLY
24141cc406Sopenharmony_ci#include "../include/sane/config.h"
25141cc406Sopenharmony_ci
26141cc406Sopenharmony_ci#include "escl.h"
27141cc406Sopenharmony_ci#include <stdio.h>
28141cc406Sopenharmony_ci#include <stdlib.h>
29141cc406Sopenharmony_ci#include <string.h>
30141cc406Sopenharmony_ci
31141cc406Sopenharmony_ciunsigned char *
32141cc406Sopenharmony_ciescl_crop_surface(capabilities_t *scanner,
33141cc406Sopenharmony_ci               unsigned char *surface,
34141cc406Sopenharmony_ci	       int w,
35141cc406Sopenharmony_ci	       int h,
36141cc406Sopenharmony_ci	       int bps,
37141cc406Sopenharmony_ci	       int *width,
38141cc406Sopenharmony_ci	       int *height)
39141cc406Sopenharmony_ci{
40141cc406Sopenharmony_ci    double ratio = 1.0;
41141cc406Sopenharmony_ci    int x_off = 0, x = 0;
42141cc406Sopenharmony_ci    int real_w = 0;
43141cc406Sopenharmony_ci    int y_off = 0, y = 0;
44141cc406Sopenharmony_ci    int real_h = 0;
45141cc406Sopenharmony_ci    unsigned char *surface_crop = NULL;
46141cc406Sopenharmony_ci
47141cc406Sopenharmony_ci    DBG( 1, "Escl Image Crop\n");
48141cc406Sopenharmony_ci    ratio = (double)w / (double)scanner->caps[scanner->source].width;
49141cc406Sopenharmony_ci    scanner->caps[scanner->source].width = w;
50141cc406Sopenharmony_ci    if (scanner->caps[scanner->source].pos_x < 0)
51141cc406Sopenharmony_ci       scanner->caps[scanner->source].pos_x = 0;
52141cc406Sopenharmony_ci    if (scanner->caps[scanner->source].pos_x &&
53141cc406Sopenharmony_ci        (scanner->caps[scanner->source].width >
54141cc406Sopenharmony_ci        scanner->caps[scanner->source].pos_x))
55141cc406Sopenharmony_ci       x_off = (int)((double)scanner->caps[scanner->source].pos_x * ratio);
56141cc406Sopenharmony_ci    real_w = scanner->caps[scanner->source].width - x_off;
57141cc406Sopenharmony_ci
58141cc406Sopenharmony_ci    scanner->caps[scanner->source].height = h;
59141cc406Sopenharmony_ci    if (scanner->caps[scanner->source].pos_y &&
60141cc406Sopenharmony_ci        (scanner->caps[scanner->source].height >
61141cc406Sopenharmony_ci        scanner->caps[scanner->source].pos_y))
62141cc406Sopenharmony_ci       y_off = (int)((double)scanner->caps[scanner->source].pos_y * ratio);
63141cc406Sopenharmony_ci    real_h = scanner->caps[scanner->source].height - y_off;
64141cc406Sopenharmony_ci
65141cc406Sopenharmony_ci    DBG( 1, "Escl Image Crop [%dx%d|%dx%d]\n", scanner->caps[scanner->source].pos_x, scanner->caps[scanner->source].pos_y,
66141cc406Sopenharmony_ci		    scanner->caps[scanner->source].width, scanner->caps[scanner->source].height);
67141cc406Sopenharmony_ci
68141cc406Sopenharmony_ci    *width = real_w;
69141cc406Sopenharmony_ci    *height = real_h;
70141cc406Sopenharmony_ci    DBG( 1, "Escl Image Crop [%dx%d]\n", *width, *height);
71141cc406Sopenharmony_ci    if (x_off > 0 || real_w < scanner->caps[scanner->source].width ||
72141cc406Sopenharmony_ci        y_off > 0 || real_h < scanner->caps[scanner->source].height) {
73141cc406Sopenharmony_ci          surface_crop = (unsigned char *)malloc (sizeof (unsigned char) * real_w
74141cc406Sopenharmony_ci                     * real_h * bps);
75141cc406Sopenharmony_ci	  if(!surface_crop) {
76141cc406Sopenharmony_ci             DBG( 1, "Escl Crop : Surface_crop Memory allocation problem\n");
77141cc406Sopenharmony_ci	     free(surface);
78141cc406Sopenharmony_ci	     surface = NULL;
79141cc406Sopenharmony_ci	     goto finish;
80141cc406Sopenharmony_ci	  }
81141cc406Sopenharmony_ci          for (y = 0; y < real_h; y++)
82141cc406Sopenharmony_ci          {
83141cc406Sopenharmony_ci             for (x = 0; x < real_w; x++)
84141cc406Sopenharmony_ci             {
85141cc406Sopenharmony_ci                surface_crop[(y * real_w * bps) + (x * bps)] =
86141cc406Sopenharmony_ci                   surface[((y + y_off) * w  * bps) + ((x + x_off) * bps)];
87141cc406Sopenharmony_ci                surface_crop[(y * real_w * bps) + (x * bps) + 1] =
88141cc406Sopenharmony_ci	           surface[((y + y_off) * w  * bps) + ((x + x_off) * bps) + 1];
89141cc406Sopenharmony_ci	        surface_crop[(y * real_w * bps) + (x * bps) + 2] =
90141cc406Sopenharmony_ci	           surface[((y + y_off) * w  * bps) + ((x + x_off) * bps) + 2];
91141cc406Sopenharmony_ci             }
92141cc406Sopenharmony_ci          }
93141cc406Sopenharmony_ci          free(surface);
94141cc406Sopenharmony_ci	  surface = surface_crop;
95141cc406Sopenharmony_ci    }
96141cc406Sopenharmony_ci    // we don't need row pointers anymore
97141cc406Sopenharmony_ci    scanner->img_data = surface;
98141cc406Sopenharmony_ci    scanner->img_size = (int)(real_w * real_h * bps);
99141cc406Sopenharmony_ci    scanner->img_read = 0;
100141cc406Sopenharmony_cifinish:
101141cc406Sopenharmony_ci    return surface;
102141cc406Sopenharmony_ci}
103