BRL-CAD
crop.c
Go to the documentation of this file.
1 /* C R O P . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2013-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file libicv/crop.c
21  *
22  * This file contains functions for cropping images.
23  * There are two types of cropping: rectangular and skeyed.
24  *
25  */
26 #include "bu/log.h"
27 #include "bu/malloc.h"
28 #include "vmath.h"
29 #include "icv.h"
30 
31 int
32 icv_rect(icv_image_t *img, int xorig, int yorig, int xnum, int ynum)
33 {
34  int row;
35  double *p, *in_data, *out_data;
36  int widthstep_in, widthstep_out, bytes_row; /**< */
37 
38  ICV_IMAGE_VAL_INT(img);
39 
40  if (xorig < 0)
41  xorig = 0;
42 
43  if (yorig < 0)
44  yorig = 0;
45 
46  if (xnum <= 0)
47  bu_exit(1, "icv_rect : ERROR: Horizontal Cut Size\n");
48 
49  if (ynum <= 0)
50  bu_exit(1, "icv_rect : ERROR: Vertical Cut Size\n");
51 
52  if (xorig+xnum > img->width)
53  bu_exit(1, "icv_rect : Cut not possible, Input parameters exceeds the width\n");
54 
55  if (yorig+ynum > img->height)
56  bu_exit(1, "icv_rect : Cut not possible, Input parameters exceeds the height\n");
57 
58  /* initialization of variables to insure cropping and copying */
59  widthstep_in = img->width*img->channels;
60  widthstep_out = xnum*img->channels;
61  bytes_row = widthstep_out*sizeof(double);
62  out_data = p = (double *)bu_malloc(ynum*bytes_row,"icv_rect : Cropped Image Data" );
63 
64  /* Hopes to the initial point to be extracted on the first line */
65  in_data = img->data + xorig*img->channels;
66 
67  for (row = yorig; row < yorig+ynum ;row++) {
68  VMOVEN(p,in_data,widthstep_out);
69  in_data += widthstep_in;
70  p += widthstep_out;
71  }
72 
73  bu_free(img->data, "icv image input data");
74  img->width = xnum;
75  img->height = ynum;
76  img->data = out_data;
77  return 0;
78 }
79 
80 int
81 icv_crop(icv_image_t *img, int ulx, int uly, int urx, int ury, int lrx, int lry, int llx, int lly, unsigned int ynum, unsigned int xnum)
82 {
83  float x_1, y_1, x_2, y_2;
84  size_t row, col;
85  int x, y;
86  double *data, *p, *q;
87 
88  ICV_IMAGE_VAL_INT(img);
89 
90  /* Allocates output data and assigns to image*/
91  data = img->data;
92  img->data = p = (double *)bu_malloc(ynum*xnum*img->channels*sizeof(double), "icv_crop: Out Image");
93 
94  for (row = 0; row < ynum; row++) {
95  /* calculate left point of row */
96  x_1 = ((ulx-llx)/(fastf_t)(ynum-1)) * (fastf_t)row + llx;
97  y_1 = ((uly-lly)/(fastf_t)(ynum-1)) * (fastf_t)row + lly;
98  /* calculate right point of row */
99  x_2 = ((urx-lrx)/(fastf_t)(ynum-1)) * (fastf_t)row + lrx;
100  y_2 = ((ury-lry)/(fastf_t)(ynum-1)) * (fastf_t)row + lry;
101  for (col = 0; col < xnum; col++) {
102  /* calculate point along row */
103  x = (int)((x_2-x_1)/(fastf_t)(xnum-1)) * (fastf_t)col + x_1;
104  y = (int)((y_2-y_1)/(fastf_t)(xnum-1)) * (fastf_t)col + y_1;
105  /* Calculates the pointer to the data which has to be copied */
106  q = data + (img->width*y+x)*img->channels;
107  /* Moves pixel to the prescribed location */
108  VMOVEN(p,q,img->channels);
109  /* points to the next pointer where data is to be copied */
110  p += img->channels;
111  }
112  }
113  bu_free(data, "icv_crop : frees input image buffer");
114  img->width = xnum;
115  img->height = ynum;
116  return 0;
117 }
118 
119 /*
120  * Local Variables:
121  * tab-width: 8
122  * mode: C
123  * indent-tabs-mode: t
124  * c-file-style: "stroustrup"
125  * End:
126  * ex: shiftwidth=4 tabstop=8
127  */
double * data
Definition: icv.h:89
int width
Definition: icv.h:91
int icv_crop(icv_image_t *img, int ulx, int uly, int urx, int ury, int lrx, int lry, int llx, int lly, unsigned int ynum, unsigned int xnum)
Definition: crop.c:81
int channels
Definition: icv.h:91
Definition: icv.h:86
int height
Definition: icv.h:91
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
int icv_rect(icv_image_t *img, int xorig, int yorig, int xnum, int ynum)
Definition: crop.c:32
COMPLEX data[64]
Definition: fftest.c:34
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
Definition: bomb.c:195
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define ICV_IMAGE_VAL_INT(_i)
Definition: icv.h:123
double fastf_t
Definition: defines.h:300