main.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /****************************************************************************
  2. *
  3. * MODULE: wximgview
  4. * AUTHOR(S): Glynn Clements
  5. * PURPOSE: View BMP images from the PNG/cairo drivers
  6. * COPYRIGHT: (C) 2010 Glynn Clements
  7. *
  8. * This program is free software under the GNU General Public
  9. * License (>=v2). Read the file COPYING that comes with GRASS
  10. * for details.
  11. *
  12. *****************************************************************************/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <string.h>
  17. #include <signal.h>
  18. #include <errno.h>
  19. #ifdef __MINGW32__
  20. #include <Windows.h>
  21. #else
  22. #include <sys/mman.h>
  23. #endif
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <sys/stat.h>
  27. #include <sys/time.h>
  28. #include <sys/types.h>
  29. #include <wx/wx.h>
  30. extern "C" {
  31. #include <grass/gis.h>
  32. #include <grass/glocale.h>
  33. }
  34. #include "wximgview.h"
  35. #define HEADER_SIZE 64
  36. IMPLEMENT_APP_NO_MAIN(MyApp)
  37. BEGIN_EVENT_TABLE(MyFrame, wxFrame)
  38. EVT_ERASE_BACKGROUND(MyFrame::erase)
  39. EVT_PAINT(MyFrame::redraw)
  40. EVT_TIMER(wxID_ANY, MyFrame::tick)
  41. END_EVENT_TABLE()
  42. ;
  43. const wxString MyFrame::title = wxString::FromAscii("Image Viewer");
  44. static const char *filename;
  45. static double fraction;
  46. void MyFrame::Create()
  47. {
  48. timer = new wxTimer(this);
  49. timer->Start(100, true);
  50. }
  51. void MyFrame::erase(wxEraseEvent &ev)
  52. {
  53. ev.GetDC();
  54. }
  55. void MyFrame::draw()
  56. {
  57. MyApp &app = wxGetApp();
  58. wxSize size = GetSize();
  59. int x0 = (size.GetWidth() - app.i_width) / 2;
  60. int y0 = (size.GetHeight() - app.i_height) / 2;
  61. wxPaintDC dc(this);
  62. wxImage image(app.i_width, app.i_height);
  63. const unsigned char *p = app.imgbuf;
  64. for (int y = 0; y < app.i_height; y++)
  65. for (int x = 0; x < app.i_width; x++, p += 4)
  66. image.SetRGB(x, y, p[2], p[1], p[0]);
  67. dc.DrawBitmap(wxBitmap(image), x0, y0, false);
  68. }
  69. void MyFrame::redraw(wxPaintEvent &ev)
  70. {
  71. if (::fraction > 0.001) {
  72. struct timeval tv0;
  73. gettimeofday(&tv0, NULL);
  74. draw();
  75. struct timeval tv1;
  76. gettimeofday(&tv1, NULL);
  77. double last = (tv1.tv_sec - tv0.tv_sec) * 1e3 + (tv1.tv_usec - tv0.tv_usec) * 1e-3;
  78. double delay = last / ::fraction;
  79. timer->Start((int) delay, true);
  80. }
  81. else
  82. draw();
  83. }
  84. void MyFrame::tick(wxTimerEvent &ev)
  85. {
  86. Refresh();
  87. }
  88. static unsigned int get_2(const unsigned char **q)
  89. {
  90. const unsigned char *p = *q;
  91. unsigned int n = (p[0] << 0) | (p[1] << 8);
  92. *q += 2;
  93. return n;
  94. }
  95. static unsigned int get_4(const unsigned char **q)
  96. {
  97. const unsigned char *p = *q;
  98. unsigned int n = (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
  99. *q += 4;
  100. return n;
  101. }
  102. int MyApp::read_bmp_header(const unsigned char *p)
  103. {
  104. int size;
  105. if (*p++ != 'B')
  106. return 0;
  107. if (*p++ != 'M')
  108. return 0;
  109. size = get_4(&p);
  110. get_4(&p);
  111. if (get_4(&p) != HEADER_SIZE)
  112. return 0;
  113. if (get_4(&p) != 40)
  114. return 0;
  115. i_width = get_4(&p);
  116. i_height = -get_4(&p);
  117. get_2(&p);
  118. if (get_2(&p) != 32)
  119. return 0;
  120. if (get_4(&p) != 0)
  121. return 0;
  122. if (get_4(&p) != (unsigned int) i_width * i_height * 4)
  123. return 0;
  124. if (size != HEADER_SIZE + i_width * i_height * 4)
  125. return 0;
  126. get_4(&p);
  127. get_4(&p);
  128. get_4(&p);
  129. get_4(&p);
  130. return 1;
  131. }
  132. void MyApp::map_file()
  133. {
  134. unsigned char header[HEADER_SIZE];
  135. size_t size;
  136. void *ptr;
  137. int fd;
  138. fd = open(::filename, O_RDONLY);
  139. if (fd < 0)
  140. G_fatal_error(_("Unable to open image file"));
  141. if (read(fd, (char *) header, sizeof(header)) != sizeof(header))
  142. G_fatal_error(_("Unable to read BMP header"));
  143. if (!read_bmp_header(header))
  144. G_fatal_error(_("Invalid BMP header"));
  145. size = HEADER_SIZE + i_width * i_height * 4;
  146. #ifdef __MINGW32__
  147. HANDLE handle = CreateFileMapping((HANDLE) _get_osfhandle(fd),
  148. NULL, PAGE_READONLY,
  149. 0, size, NULL);
  150. if (!handle)
  151. return;
  152. ptr = MapViewOfFile(handle, FILE_MAP_READ, 0, 0, size);
  153. if (!ptr)
  154. G_fatal_error(_("Unable to map image file"));
  155. #else
  156. ptr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, (off_t) 0);
  157. if (ptr == MAP_FAILED)
  158. G_fatal_error(_("Unable to map image file"));
  159. #endif
  160. imgbuf = (unsigned char *)ptr + HEADER_SIZE;
  161. close(fd);
  162. }
  163. static void dummy_handler(int sig)
  164. {
  165. wxTimerEvent ev = wxTimerEvent();
  166. wxPostEvent(wxGetApp().mainwin, ev);
  167. }
  168. static void set_handler(void)
  169. {
  170. #ifndef __MINGW32__
  171. struct sigaction act;
  172. act.sa_handler = &dummy_handler;
  173. sigemptyset(&act.sa_mask);
  174. act.sa_flags = 0;
  175. sigaction(SIGUSR1, &act, NULL);
  176. #endif
  177. }
  178. bool MyApp::OnInit()
  179. {
  180. map_file();
  181. wxSize size(i_width, i_height);
  182. mainwin = new MyFrame(size);
  183. mainwin->Show();
  184. SetTopWindow(mainwin);
  185. set_handler();
  186. return true;
  187. }
  188. int main(int argc, char **argv)
  189. {
  190. struct GModule *module;
  191. struct
  192. {
  193. struct Option *image, *percent;
  194. } opt;
  195. G_gisinit(argv[0]);
  196. module = G_define_module();
  197. G_add_keyword(_("display"));
  198. G_add_keyword(_("graphics"));
  199. G_add_keyword(_("raster"));
  200. G_add_keyword(_("vector"));
  201. G_add_keyword(_("visualization"));
  202. module->description = _("View BMP images from the PNG driver.");
  203. opt.image = G_define_standard_option(G_OPT_F_INPUT);
  204. opt.image->key = "image";
  205. opt.image->required = YES;
  206. opt.image->gisprompt = "old_file,file,file";
  207. opt.image->description = _("Image file");
  208. opt.percent = G_define_option();
  209. opt.percent->key = "percent";
  210. opt.percent->type = TYPE_INTEGER;
  211. opt.percent->required = NO;
  212. opt.percent->multiple = NO;
  213. opt.percent->description = _("Percentage of CPU time to use");
  214. opt.percent->answer = "10";
  215. if (G_parser(argc, argv))
  216. exit(EXIT_FAILURE);
  217. ::filename = opt.image->answer;
  218. ::fraction = atoi(opt.percent->answer) / 100.0;
  219. return wxEntry(argc, argv);
  220. return EXIT_SUCCESS;
  221. }