Fawkes API  Fawkes Development Version
shmem.cpp
1 
2 /***************************************************************************
3  * shmem.cpp - Shared memory management tool
4  *
5  * Generated: Mon Jan 16 22:51:34 2006
6  * Copyright 2005-2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <fvutils/ipc/shm_image.h>
24 #include <fvutils/ipc/shm_lut.h>
25 #include <utils/system/argparser.h>
26 #include <fvutils/writers/fvraw.h>
27 
28 #include <iostream>
29 #include <cstring>
30 #include <cstdio>
31 
32 using namespace std;
33 using namespace fawkes;
34 using namespace firevision;
35 
36 int
37 main(int argc, char **argv)
38 {
39 
40  ArgumentParser *argp = new ArgumentParser(argc, argv, "c::hl::i:");
41  bool action_done = false;
42 
43  if ( argp->has_arg("h") ) {
44  // Show usage note
45  cout << endl << "Usage: " << argv[0] << " [-h] [-c] [-c[t]] [-l] [-i image_id] [file]" << endl
46  << " -h Show this help message" << endl
47  << " -i id Save image ID to file" << endl
48  << " -c[t] Cleanup (remove all FireVision related shmem segments of given type)"
49  << endl
50  << " -l[t] List shared memory segments of given type" << endl
51  << endl
52  << " [t] type is a combination of" << endl
53  << " i images" << endl
54  << " l lookup tables" << endl
55  << " or empty in which case all known shared memory segments are mangled" << endl
56  << endl
57  << " [file] is a file name. Content depends on the action. The possibilities are: " << endl
58  << " for -i File where the saved image is stored" << endl
59  << endl
60  << "By default all known shared memory segments are listed" << endl
61  << endl;
62  action_done = true;
63  } else {
64  if ( argp->has_arg("i") ) {
65  if ( argp->num_items() == 0 ) {
66  printf("You have to give a file name where to store the image\n");
67  } else {
68  const char *image_id = argp->arg("i");
69 
70  try {
72 
73  FvRawWriter *w = new FvRawWriter(argp->items()[0], b->width(), b->height(),
74  b->colorspace(), b->buffer());
75  w->write();
76  delete w;
77  delete b;
78  printf("Image '%s' saved to %s\n", image_id, argp->items()[0]);
79  } catch (Exception &e) {
80  printf("Failed top save image\n");
81  e.print_trace();
82  }
83  }
84  }
85  if ( argp->has_arg("c") ) {
86  const char *tmp;
87  if ( (tmp = argp->arg("c")) != NULL) {
88  if ( strchr(tmp, 'i') != NULL) {
89  SharedMemoryImageBuffer::cleanup();
90  }
91  if ( strchr(tmp, 'l') != NULL) {
92  SharedMemoryLookupTable::cleanup();
93  }
94  } else {
95  SharedMemoryImageBuffer::cleanup();
96  SharedMemoryLookupTable::cleanup();
97  }
98 
99  action_done = true;
100  }
101  if ( argp->has_arg("l") ) {
102  const char *tmp;
103  if ( (tmp = argp->arg("l")) != NULL) {
104  if ( strchr(tmp, 'i') != NULL) {
105  SharedMemoryImageBuffer::list();
106  }
107  if ( strchr(tmp, 'l') != NULL) {
108  SharedMemoryLookupTable::list();
109  }
110  } else {
111  SharedMemoryImageBuffer::list();
112  SharedMemoryLookupTable::list();
113  }
114 
115  action_done = true;
116  }
117  }
118 
119  if (! action_done) {
120  SharedMemoryImageBuffer::list();
121  cout << endl;
122  SharedMemoryLookupTable::list();
123  }
124 
125  cout << endl;
126 }
const char * arg(const char *argn)
Get argument value.
Definition: argparser.cpp:182
const std::vector< const char *> & items() const
Get non-option items.
Definition: argparser.cpp:462
Fawkes library namespace.
STL namespace.
Parse command line arguments.
Definition: argparser.h:66
unsigned char * buffer() const
Get image buffer.
Definition: shm_image.cpp:235
std::vector< const char *>::size_type num_items() const
Get number of non-option items.
Definition: argparser.cpp:472
FvRaw Writer implementation.
Definition: fvraw.h:34
Base class for exceptions in Fawkes.
Definition: exception.h:36
Shared memory image buffer.
Definition: shm_image.h:181
virtual void write()
Write to file.
Definition: fvraw.cpp:128
unsigned int width() const
Get image width.
Definition: shm_image.cpp:255
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:619
bool has_arg(const char *argn)
Check if argument has been supplied.
Definition: argparser.cpp:169
unsigned int height() const
Get image height.
Definition: shm_image.cpp:265
colorspace_t colorspace() const
Get color space.
Definition: shm_image.cpp:245