1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
/* $Id: flipx.c,v 1.10 2006/02/17 10:26:58 yjung Exp $
*
* This file is part of the ESO QFITS Library
* Copyright (C) 2001-2004 European Southern Observatory
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* $Author: yjung $
* $Date: 2006/02/17 10:26:58 $
* $Revision: 1.10 $
* $Name: qfits-6_2_0 $
*/
/*-----------------------------------------------------------------------------
Includes
-----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include "qfits_rw.h"
#include "qfits_tools.h"
/*-----------------------------------------------------------------------------
Private functions
-----------------------------------------------------------------------------*/
/*
* Swap pixels between position p1 and p2, regardless of the pixel
* type and endian-ness of the local host.
*/
static void swap_pix(char * buf, int p1, int p2, int psize)
{
int i ;
char c ;
for (i=0 ; i<psize ; i++) {
c = buf[p1+i] ;
buf[p1+i] = buf[p2+i];
buf[p2+i] = c ;
}
}
/*
* Main processing function. It expects one only file name
* and will flip pixels on the input frame.
*/
static int fits_flip(char * pname, char * filename)
{
char * sval ;
int dstart;
int lx, ly ;
int bpp ;
int i, j ;
char * buf ;
char * fbuf ;
int psize;
struct stat fileinfo ;
int fd ;
printf("%s: processing %s\n", pname, filename);
if (stat(filename, &fileinfo)!=0) {
return -1 ;
}
if (fileinfo.st_size<1) {
printf("cannot stat file\n");
return -1 ;
}
/* Retrieve image attributes */
if (qfits_is_fits(filename)!=1) {
printf("not a FITS file\n");
return -1 ;
}
sval = qfits_query_hdr(filename, "NAXIS1");
if (sval==NULL) {
printf("cannot read NAXIS1\n");
return -1 ;
}
lx = atoi(sval);
sval = qfits_query_hdr(filename, "NAXIS2");
if (sval==NULL) {
printf("cannot read NAXIS2\n");
return -1 ;
}
ly = atoi(sval);
sval = qfits_query_hdr(filename, "BITPIX");
if (sval==NULL) {
printf("cannot read BITPIX\n");
return -1 ;
}
bpp = atoi(sval);
psize = bpp/8 ;
if (psize<0) psize=-psize ;
/* Retrieve start of first data section */
if (qfits_get_hdrinfo(filename, 0, &dstart, NULL)!=0) {
printf("reading header information\n");
return -1 ;
}
/* Map the input file in read/write mode (input file is modified) */
if ((fd=open(filename, O_RDWR))==-1) {
perror("open");
printf("reading file\n");
return -1 ;
}
fbuf = (char*)mmap(0,
fileinfo.st_size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,
0);
if (fbuf==(char*)-1) {
perror("mmap");
printf("mapping file\n");
return -1 ;
}
buf = fbuf + dstart ;
/* Double loop */
for (j=0 ; j<ly ; j++) {
for (i=0 ; i<lx/2 ; i++) {
/* Swap bytes */
swap_pix(buf, i*psize, (lx-i-1)*psize, psize);
}
buf += lx * psize ;
}
if (munmap(fbuf, fileinfo.st_size)!=0) {
printf("unmapping file\n");
return -1 ;
}
return 0 ;
}
/*-----------------------------------------------------------------------------
Main
-----------------------------------------------------------------------------*/
int main(int argc, char * argv[])
{
int i ;
int err ;
if (argc<2) {
printf("use: %s <list of FITS files...>\n", argv[0]);
return 1 ;
}
err=0 ;
for (i=1 ; i<argc ; i++) {
err += fits_flip(argv[0], argv[i]) ;
}
if (err>0) {
fprintf(stderr, "%s: %d error(s) occurred\n", argv[0], err);
return -1 ;
}
return 0 ;
}
|