BRL-CAD
screengrab.c
Go to the documentation of this file.
1 /* S C R E E N G R A B . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-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 libged/screengrab.c
21  *
22  * The screengrab command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 
32 
33 #include "icv.h"
34 
35 #include "./ged_private.h"
36 
37 
38 /* !!! FIXME: this command should not be directly utilizing LIBDM or
39  * LIBFB as this breaks library encapsulation. Generic functionality
40  * should be moved out of LIBDM into LIBICV, or be handled by the
41  * application logic calling this routine.
42  */
43 int
44 ged_screen_grab(struct ged *gedp, int argc, const char *argv[])
45 {
46 
47  int i;
48  int width = 0;
49  int height = 0;
50  int bytes_per_pixel = 0;
51  int bytes_per_line = 0;
52  static const char *usage = "image_name.ext";
53  unsigned char **rows = NULL;
54  unsigned char *idata = NULL;
55  struct icv_image *bif = NULL; /**< icv image container for saving images */
56 
57  if (gedp->ged_dmp_is_null) {
58  bu_vls_printf(gedp->ged_result_str, "Bad display pointer.");
59  return GED_ERROR;
60  }
61 
62  if (gedp->ged_dm_get_display_image == NULL) {
63  bu_vls_printf(gedp->ged_result_str, "Bad display function pointer.");
64  return GED_ERROR;
65  }
66 
70  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
71 
72  /* initialize result */
73  bu_vls_trunc(gedp->ged_result_str, 0);
74 
75  /* must be wanting help */
76  if (argc == 1) {
77  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
78  return GED_HELP;
79  }
80 
81  if (argc != 2) {
82  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
83  return GED_ERROR;
84  }
85 
86  width = gedp->ged_dm_width;
87  height = gedp->ged_dm_height;
88 
89  if (width <= 0 || height <= 0) {
90  bu_vls_printf(gedp->ged_result_str, "%s: invalid screen dimensions.", argv[1]);
91  return GED_ERROR;
92  }
93 
94  bytes_per_pixel = 3;
95  bytes_per_line = width * bytes_per_pixel;
96 
97  /* create image file */
98 
99  if ((bif = icv_create(width, height, ICV_COLOR_SPACE_RGB)) == NULL) {
100  bu_vls_printf(gedp->ged_result_str, "%s: could not create icv_image write structure.", argv[1]);
101  return GED_ERROR;
102  }
103 
104  rows = (unsigned char **)bu_calloc(height, sizeof(unsigned char *), "rows");
105 
106  gedp->ged_dm_get_display_image(gedp, &idata);
107 
108  for (i = 0; i < height; ++i) {
109  rows[i] = (unsigned char *)(idata + ((height-i-1)*bytes_per_line));
110  /* TODO : Add double type data to maintain resolution */
111  icv_writeline(bif, i, rows[i], ICV_DATA_UCHAR);
112  }
113 
114  if (bif != NULL) {
115  icv_write(bif, argv[1], ICV_IMAGE_AUTO);
116  icv_destroy(bif);
117  bif = NULL;
118  }
119 
120  bu_free(rows, "rows");
121  bu_free(idata, "image data");
122 
123  return GED_OK;
124 }
125 
126 /*
127  * Local Variables:
128  * tab-width: 8
129  * mode: C
130  * indent-tabs-mode: t
131  * c-file-style: "stroustrup"
132  * End:
133  * ex: shiftwidth=4 tabstop=8
134  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
int ged_dm_width
Definition: ged.h:383
Definition: ged.h:338
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
#define GED_CHECK_ARGC_GT_0(_gedp, _argc, _flags)
Definition: ged.h:202
int icv_destroy(icv_image_t *bif)
Definition: fileformat.c:321
Header file for the BRL-CAD common definitions.
Definition: icv.h:86
int height
Definition: icv.h:91
ustring width
#define GED_ERROR
Definition: ged.h:61
int ged_screen_grab(struct ged *gedp, int argc, const char *argv[])
Definition: screengrab.c:44
int icv_write(icv_image_t *bif, const char *filename, ICV_IMAGE_FORMAT format)
Definition: fileformat.c:186
#define GED_CHECK_VIEW(_gedp, _flags)
Definition: ged.h:140
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
int icv_writeline(icv_image_t *bif, int y, void *data, ICV_DATA type)
Definition: fileformat.c:219
#define GED_CHECK_DRAWABLE(_gedp, _flags)
Definition: ged.h:129
int ged_dm_height
Definition: ged.h:384
struct bu_vls * ged_result_str
Definition: ged.h:357
int ged_dmp_is_null
Definition: ged.h:385
icv_image_t * icv_create(int width, int height, ICV_COLOR_SPACE color_space)
Definition: fileformat.c:276
void(* ged_dm_get_display_image)(struct ged *, unsigned char **)
Definition: ged.h:386
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define GED_HELP
Definition: ged.h:62
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328