[4] | 1 | /***************************************************************************************** |
---|
| 2 | * imageflip: loads an image sequence and sends it to SAGE for display |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2007 Electronic Visualization Laboratory, |
---|
| 5 | * University of Illinois at Chicago |
---|
| 6 | * |
---|
| 7 | * All rights reserved. |
---|
| 8 | * |
---|
| 9 | * Redistribution and use in source and binary forms, with or without |
---|
| 10 | * modification, are permitted provided that the following conditions are met: |
---|
| 11 | * |
---|
| 12 | * * Redistributions of source code must retain the above copyright |
---|
| 13 | * notice, this list of conditions and the following disclaimer. |
---|
| 14 | * * Redistributions in binary form must reproduce the above |
---|
| 15 | * copyright notice, this list of conditions and the following disclaimer |
---|
| 16 | * in the documentation and/or other materials provided with the distribution. |
---|
| 17 | * * Neither the name of the University of Illinois at Chicago nor |
---|
| 18 | * the names of its contributors may be used to endorse or promote |
---|
| 19 | * products derived from this software without specific prior written permission. |
---|
| 20 | * |
---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 25 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 28 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 32 | * |
---|
| 33 | * Direct questions, comments etc to www.evl.uic.edu/cavern/forum |
---|
| 34 | * |
---|
| 35 | * Author: Ratko Jagodic, Luc Renambot |
---|
| 36 | * |
---|
| 37 | *****************************************************************************************/ |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | #include <stdio.h> |
---|
| 42 | #include <stdlib.h> |
---|
| 43 | #include <string> |
---|
| 44 | #include <time.h> |
---|
| 45 | #include <unistd.h> |
---|
| 46 | #include <wand/magick-wand.h> |
---|
| 47 | |
---|
| 48 | // headers for SAGE |
---|
| 49 | #include "sail.h" |
---|
| 50 | #include "misc.h" |
---|
| 51 | #include "util.h" |
---|
| 52 | |
---|
| 53 | #define FLIP_DEPTH 10 |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | // Set of filenames |
---|
| 57 | std::set<std::string> Names; |
---|
| 58 | // Number of images to ne processed |
---|
| 59 | int Limit; |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | #if MagickLibVersion >= 0x645 |
---|
| 63 | #define MagickGetImagePixels MagickGetAuthenticPixels |
---|
| 64 | #endif |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | typedef unsigned char byte; |
---|
| 69 | #define memalign(x,y) malloc((y)) |
---|
| 70 | |
---|
| 71 | using namespace std; |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | #define ThrowWandException(wand) \ |
---|
| 75 | { \ |
---|
| 76 | char \ |
---|
| 77 | *description; \ |
---|
| 78 | \ |
---|
| 79 | ExceptionType \ |
---|
| 80 | severity; \ |
---|
| 81 | \ |
---|
| 82 | description=MagickGetException(wand,&severity); \ |
---|
| 83 | (void) fprintf(stderr,"%s %s %ld %s\n",GetMagickModule(),description); \ |
---|
| 84 | description=(char *) MagickRelinquishMemory(description); \ |
---|
| 85 | exit(-1); \ |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | // ----------------------------------------------------------------------------- |
---|
| 90 | |
---|
| 91 | void getRGB(string fileName, byte *rgb, unsigned int &width, unsigned int &height) |
---|
| 92 | { |
---|
| 93 | // use ImageMagick to read all other formats |
---|
| 94 | MagickBooleanType status; |
---|
| 95 | MagickWand *wand; |
---|
| 96 | |
---|
| 97 | // read file |
---|
| 98 | wand=NewMagickWand(); |
---|
| 99 | status=MagickReadImage(wand, fileName.data()); |
---|
| 100 | if (status == MagickFalse) |
---|
| 101 | ThrowWandException(wand); |
---|
| 102 | |
---|
| 103 | // get the image size |
---|
| 104 | width = MagickGetImageWidth(wand); |
---|
| 105 | height = MagickGetImageHeight(wand); |
---|
| 106 | |
---|
| 107 | // get the pixels |
---|
| 108 | MagickGetImagePixels(wand, 0, 0, width, height, "RGB", CharPixel, rgb); |
---|
| 109 | DestroyMagickWand(wand); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | // ----------------------------------------------------------------------------- |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | // ----------------------------------------------------------------------------- |
---|
| 116 | |
---|
| 117 | void getDimensions(string fileName, unsigned int &width, unsigned int &height) |
---|
| 118 | { |
---|
| 119 | // use ImageMagick to read all other formats |
---|
| 120 | MagickBooleanType status; |
---|
| 121 | MagickWand *wand; |
---|
| 122 | |
---|
| 123 | // read file |
---|
| 124 | wand=NewMagickWand(); |
---|
| 125 | status=MagickReadImage(wand, fileName.data()); |
---|
| 126 | if (status == MagickFalse) |
---|
| 127 | ThrowWandException(wand); |
---|
| 128 | |
---|
| 129 | // get the image size |
---|
| 130 | width = MagickGetImageWidth(wand); |
---|
| 131 | height = MagickGetImageHeight(wand); |
---|
| 132 | |
---|
| 133 | DestroyMagickWand(wand); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | // ----------------------------------------------------------------------------- |
---|
| 137 | |
---|
| 138 | void RefreshList(string dir) |
---|
| 139 | { |
---|
| 140 | DIR *dirh; |
---|
| 141 | struct dirent *dirp; |
---|
| 142 | char pathname[1024]; |
---|
| 143 | char linkname[1024]; |
---|
| 144 | struct stat statbuf; |
---|
| 145 | |
---|
| 146 | const char *dn = dir.c_str(); |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | if ((dirh = opendir(dn)) == NULL) |
---|
| 150 | { |
---|
| 151 | perror("opendir"); |
---|
| 152 | exit(1); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | |
---|
| 156 | for (dirp = readdir(dirh); dirp != NULL; dirp = readdir(dirh)) |
---|
| 157 | { |
---|
| 158 | if (strcmp(".",dirp->d_name) == 0 || strcmp("..",dirp->d_name) == 0) |
---|
| 159 | { |
---|
| 160 | continue; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | sprintf(pathname,"%s/%s",dn,dirp->d_name); |
---|
| 164 | |
---|
| 165 | if (lstat(pathname,&statbuf) == -1) /* see man stat */ |
---|
| 166 | { |
---|
| 167 | perror("stat"); |
---|
| 168 | continue; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | if (S_ISREG(statbuf.st_mode)) |
---|
| 172 | { |
---|
| 173 | //printf("%s is a regular file\n",pathname); |
---|
| 174 | string pn = string(pathname); |
---|
| 175 | string fileExt = pn.substr(pn.rfind(".")); |
---|
| 176 | if ( (fileExt.compare(".JPG") == 0) || (fileExt.compare(".jpg") == 0) ) |
---|
| 177 | if (Names.insert(pathname).second) |
---|
| 178 | { |
---|
| 179 | cout << pathname << " added successfully" << endl; |
---|
| 180 | } |
---|
| 181 | else |
---|
| 182 | { |
---|
| 183 | //cout << pathname << " rejected" << endl; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | if (S_ISDIR(statbuf.st_mode)) |
---|
| 189 | { |
---|
| 190 | //printf("%s is a directory\n",pathname); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | if (S_ISLNK(statbuf.st_mode)) |
---|
| 194 | { |
---|
| 195 | bzero(linkname,1024); /* clear string */ |
---|
| 196 | readlink(pathname,linkname,1024); |
---|
| 197 | //printf("%s is a link to %s\n",pathname,linkname); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | //printf("The mode of %s is %o\n\n",pathname,statbuf.st_mode & 07777); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | closedir(dirh); |
---|
| 204 | |
---|
| 205 | Limit = Names.size() < FLIP_DEPTH ? 0 : Names.size() - FLIP_DEPTH; |
---|
| 206 | |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | // ----------------------------------------------------------------------------- |
---|
| 210 | |
---|
| 211 | |
---|
| 212 | |
---|
| 213 | int main(int argc,char **argv) |
---|
| 214 | { |
---|
| 215 | byte *sageBuffer = NULL; // buffer for sage |
---|
| 216 | unsigned int width, height; // image size |
---|
| 217 | unsigned int window_width=0, window_height=0; // sage window size |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | // parse command line arguments |
---|
| 221 | if (argc < 2){ |
---|
| 222 | fprintf(stderr, "\n\nUSAGE: imageflip directory [width] [height]"); |
---|
| 223 | return 0; |
---|
| 224 | } |
---|
| 225 | for (int argNum=2; argNum<argc; argNum++) |
---|
| 226 | { |
---|
| 227 | if(atoi(argv[argNum]) != 0 && atoi(argv[argNum+1]) != 0) { |
---|
| 228 | window_width = atoi( argv[argNum] ); |
---|
| 229 | window_height = atoi( argv[argNum+1] ); |
---|
| 230 | argNum++; // increment because we read two args here |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | // check file extension |
---|
| 236 | string dirName; |
---|
| 237 | dirName = string(argv[1]); |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | // Populate the filename list |
---|
| 241 | RefreshList(dirName); |
---|
| 242 | |
---|
| 243 | cout << Names.size() << " elements in the set" << endl; |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | // List the names |
---|
| 247 | set<string>::iterator it; |
---|
| 248 | int k =0; |
---|
| 249 | for ( it = Names.begin() ; k < Limit ; it++ ) {k++;} |
---|
| 250 | for ( ; it != Names.end(); it++ ) |
---|
| 251 | cout << " " << *it << endl; |
---|
| 252 | |
---|
| 253 | // last element |
---|
| 254 | cout << "Last element: " << *(Names.rbegin()) << endl; |
---|
| 255 | |
---|
| 256 | string firstname = *( Names.begin() ); |
---|
| 257 | |
---|
| 258 | // if the user didn't specify the window size, use the image size |
---|
| 259 | if (window_height == 0 && window_width == 0) |
---|
| 260 | { |
---|
| 261 | // Get the dimensions of the first file |
---|
| 262 | getDimensions(firstname , width, height); |
---|
| 263 | window_width = width; |
---|
| 264 | window_height = height; |
---|
| 265 | cout << "Dimensions of image series: " << window_width << "x" << window_height << endl; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | // initialize SAIL |
---|
| 269 | sail sageInf; // sail object |
---|
| 270 | sailConfig scfg; |
---|
| 271 | scfg.init("imageflip.conf"); |
---|
| 272 | scfg.setAppName("imageflip"); |
---|
| 273 | |
---|
| 274 | scfg.resX = width; |
---|
| 275 | scfg.resY = height; |
---|
| 276 | |
---|
| 277 | if (scfg.winWidth == -1 || scfg.winHeight == -1) |
---|
| 278 | { |
---|
| 279 | scfg.winWidth = window_width; |
---|
| 280 | scfg.winHeight = window_height; |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | scfg.pixFmt = PIXFMT_888; |
---|
| 284 | scfg.rowOrd = TOP_TO_BOTTOM; |
---|
| 285 | |
---|
| 286 | sageInf.init(scfg); |
---|
| 287 | |
---|
| 288 | |
---|
| 289 | // get buffer from SAGE and fill it with data |
---|
| 290 | sageBuffer = (byte*)sageInf.getBuffer(); |
---|
| 291 | // Get some pixels |
---|
| 292 | getRGB(firstname, sageBuffer, width, height); |
---|
| 293 | sageInf.swapBuffer(); |
---|
| 294 | |
---|
| 295 | // Rewind |
---|
| 296 | k = 0; |
---|
| 297 | for ( it = Names.begin() ; k < Limit ; it++ ) {k++;} |
---|
| 298 | |
---|
| 299 | // Wait the end |
---|
| 300 | while (1) |
---|
| 301 | { |
---|
| 302 | if (it == Names.end()) { |
---|
| 303 | // Rewind |
---|
| 304 | k = 0; |
---|
| 305 | for ( it = Names.begin() ; k < Limit ; it++ ) {k++;} |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | // get buffer from SAGE and fill it with data |
---|
| 309 | sageBuffer = (byte*)sageInf.getBuffer(); |
---|
| 310 | // Get some pixels |
---|
| 311 | getRGB(*it, sageBuffer, width, height); |
---|
| 312 | cerr << "Getting " << *it << "\r"; // endl; |
---|
| 313 | sageInf.swapBuffer(); |
---|
| 314 | // Next step |
---|
| 315 | it++; |
---|
| 316 | |
---|
| 317 | // Refresh the filename list |
---|
| 318 | RefreshList(dirName); |
---|
| 319 | |
---|
| 320 | // Process SAGE messages |
---|
| 321 | sageMessage msg; |
---|
| 322 | if (sageInf.checkMsg(msg, false) > 0) { |
---|
| 323 | switch (msg.getCode()) { |
---|
| 324 | case APP_QUIT: |
---|
| 325 | cout << "\nDone\n\n"; |
---|
| 326 | sageInf.shutdown(); |
---|
| 327 | exit(1); |
---|
| 328 | break; |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | return 0; |
---|
| 334 | } |
---|