MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
displayImageSDL2.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file displayImageSDL2.cpp
5 @author Mitch Richling http://www.mitchr.me/
6 @date 2022-08-18
7 @brief Demonstrate how to display an image with SDL.@EOL
8 @std C++20
9 @copyright
10 @parblock
11 Copyright (c) 2022, Mitchell Jay Richling <http://www.mitchr.me/> All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
14
15 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
16
17 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19
20 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software
21 without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28 DAMAGE.
29 @endparblock
30*/
31/*******************************************************************************************************************************************************.H.E.**/
32
33//--------------------------------------------------------------------------------------------------------------------------------------------------------------
34//#define SDL_MAIN_HANDLED
35#include <SDL2/SDL.h>
36
37//--------------------------------------------------------------------------------------------------------------------------------------------------------------
38#include "ramCanvas.hpp"
39
40//--------------------------------------------------------------------------------------------------------------------------------------------------------------
41int main(int argc, char *argv[]) {
42 mjr::ramCanvas3c8b theRamCanvas;
43 theRamCanvas.setIntAxOrientationY(mjr::ramCanvas3c8b::intAxisOrientation::INVERTED); // In SDL2, the 0 row of pixels is at the bottom.
44
45 if (argc < 2) {
46 std::cout << "ERROR argument required!" << std::endl;
47 return 1;
48 }
49
50 int rRet;
51 if((rRet=theRamCanvas.readTIFFfile(argv[1]))) {
52 std::cout << "ERROR(" << rRet << ") reading file " << argv[1] << std::endl;
53 return 2;
54 }
55
56 rRet = SDL_Init(SDL_INIT_VIDEO);
57 if (rRet < 0) {
58 std::cout << "ERROR: SDL_Init failed: " << SDL_GetError() << std::endl;
59 return 3;
60 }
61
62 SDL_DisplayMode daDisplayMode;
63 rRet = SDL_GetDesktopDisplayMode(0, &daDisplayMode);
64 if (rRet < 0) {
65 std::cout << "ERROR: SDL_GetDesktopDisplayMode failed: " << SDL_GetError() << std::endl;
66 return 4;
67 }
68
69 if ((daDisplayMode.w-100) < theRamCanvas.getNumPixX()) {
70 std::cout << "ERROR: The display is not wide enough for image" << std::endl;
71 return 5;
72 }
73
74 if ((daDisplayMode.h-200) < theRamCanvas.getNumPixY()) {
75 std::cout << "ERROR: The display is not tall enough for image" << std::endl;
76 return 6;
77 }
78
79 SDL_Event daSDLevent;
80 SDL_Window* daSDLwindow;
81 SDL_Surface* daSDLsurface;
82
83 daSDLwindow = SDL_CreateWindow("ramCanvas Observer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, theRamCanvas.getNumPixX(), theRamCanvas.getNumPixY(), SDL_WINDOW_SHOWN);
84 daSDLsurface = SDL_GetWindowSurface(daSDLwindow);
85
86 /* Note we could SDL_BlitSurface, but it takes more code to set that up than to just copy over the bytes. :) */
87 for (int y=0; y<theRamCanvas.getNumPixY(); ++y)
88 for (int x=0; x<theRamCanvas.getNumPixX(); ++x)
89 ((Uint32*)daSDLsurface->pixels)[(y*daSDLsurface->w) + x] = SDL_MapRGB(daSDLsurface->format, theRamCanvas.getPxColorNC(x, y).getRed_byte(), theRamCanvas.getPxColorNC(x, y).getGreen_byte(), theRamCanvas.getPxColorNC(x, y).getBlue_byte());
90 SDL_UpdateWindowSurface(daSDLwindow);
91
92 while (1) {
93 if (SDL_PollEvent(&daSDLevent) && daSDLevent.type == SDL_QUIT) {
94 SDL_DestroyWindow(daSDLwindow);
95 SDL_Quit();
96 return 0;
97 }
98 }
99
100 return 0;
101}
int main(int argc, char *argv[])