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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
//
// Lynkeos
// $Id$
//
// Created by Jean-Etienne LAMIAUD on Fri Apr 22 2005.
// Copyright (c) 2005-2023. Jean-Etienne LAMIAUD
//
// 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.
//
#include <fitsio.h>
#include "FITSWriter.h"
#define K_NO_COMPRESSION 0
#define K_GZIP_COMPRESSION 1
@implementation FITSWriter
+ (void) load
{
// Nothing to do, this is just to force the runtime to load this class
}
- (id) init
{
if ( (self = [super init]) != nil )
{
[[NSBundle mainBundle] loadNibNamed:@"FITSWriter" owner:self topLevelObjects:&_nibObjects];
[_nibObjects retain];
_compression = K_NO_COMPRESSION;
_imgType = USHORT_IMG;
}
return( self );
}
- (void) dealloc
{
[_nibObjects release];
[super dealloc];
}
+ (NSString*) writerName { return( @"FITS" ); }
+ (NSString*) fileExtension { return( @"fits" ); }
+ (BOOL) canSaveDataWithPlanes:(u_short)nPlanes
width:(u_short)w height:(u_short)h
metaData:(NSDictionary*)metaData
{
return( nPlanes == 1 );
}
- (NSPanel*) configurationPanel { return( _cfgPanel ); }
+ (id <LynkeosFileWriter>) writerForURL:(NSURL*)url
planes:(u_short)nPlanes
width:(u_short)w height:(u_short)h
metaData:(NSDictionary*)metaData
{
// No pre-processing needed
return( [[[self alloc] init] autorelease] );
}
- (void) saveImageAtURL:(NSURL*)url
withData:(const REAL * const * const)data
blackLevel:(double)black whiteLevel:(double)white
withPlanes:(u_short)nPlanes
width:(u_short)w
lineWidth:(u_short)lineW
height:(u_short)h
metaData:(NSDictionary*)metaData
{
fitsfile *fits;
double *buf = NULL;
u_short x, y;
double max, offset, scale, datamax, datamin;
int err = 0;
NSString *suffix = ( _compression == K_NO_COMPRESSION ? @"" : @".gz" );
const char *file;
long size[2] = {w,h};
// Unfortunately, CFITSIO does not handle correctly the
// file://localhost/... URL given by Cocoa
if ( [url isFileURL] )
file = [[@"!" stringByAppendingString:
[[url path] stringByAppendingString:suffix]]
fileSystemRepresentation];
else
file = [[[url absoluteString] stringByAppendingString:suffix] UTF8String];
fits_create_file( &fits, file, &err );
if ( err != 0 )
{
fits_report_error( stderr, err );
return;
}
fits_create_img(fits, _imgType, 2, size, &err);
offset = 0.0;
switch( _imgType )
{
case BYTE_IMG:
max = 255.4;
break;
case USHORT_IMG:
max = 65535.4;
break;
case ULONG_IMG:
max = 4294967295.4;
break;
case FLOAT_IMG:
case DOUBLE_IMG:
default:
// No translation
max = white;
offset = black;
break;
}
scale = max - offset;
// Allocate a translation buffer
buf = (double*)malloc( w*sizeof(double) );
NSAssert( buf != NULL, @"Could not allocate write buffer" );
// Save line by line and convert to the FITS coordinate system
datamin = HUGE_VAL;
datamax = -HUGE_VAL;
for( y = 0; y < h; y++ )
{
long first[2] = {1,y+1};
// Translate the values in a buffer before saving
for( x = 0; x < w; x++ )
{
double v = GET_SAMPLE(data[0],x,h-y-1,lineW);
v = v*scale + offset;
// Still look for extrema
if ( v < datamin )
datamin = v;
if ( v > datamax )
datamax = v;
buf[x] = v;
}
fits_write_pix( fits, TDOUBLE, first, w, buf, &err );
}
// Adjust extrema to the coding
switch( _imgType )
{
case BYTE_IMG:
datamin = (long)(datamin+0.5);
datamax = (long)(datamax+0.5);
datamin -= 128.0;
datamax -= 128.0;
break;
case USHORT_IMG:
datamin = (long)(datamin+0.5);
datamax = (long)(datamax+0.5);
datamin -= 32768.0;
datamax -= 32768.0;
break;
case ULONG_IMG:
datamin = (long)(datamin+0.5);
datamax = (long)(datamax+0.5);
datamin -= 2147483648.0;
datamax -= 2147483648.0;
break;
case FLOAT_IMG:
case DOUBLE_IMG:
default:
break;
}
fits_update_key( fits, TDOUBLE, "DATAMIN", &datamin, "Minimum sample value", &err);
fits_update_key( fits, TDOUBLE, "DATAMAX", &datamax, "Maximum sample value", &err);
// Make explicit that we adhere to the FITS required ordering of rows
fits_update_key( fits, TSTRING, "ROWORDER", "BOTTOM-UP", "Order of the rows in image array", &err);
if ( err != 0 )
fits_report_error( stderr, err );
err = 0;
fits_close_file( fits, &err );
if ( err != 0 )
fits_report_error( stderr, err );
free( buf );
}
- (IBAction) changeCompression :(id)sender
{
_compression = [[sender selectedItem] tag];
}
- (IBAction) changeBits :(id)sender
{
_imgType = (int)[[sender selectedItem] tag];
}
- (IBAction) confirmParams :(id)sender
{
[NSApp stopModalWithCode:NSModalResponseOK];
[_cfgPanel close];
}
- (IBAction) cancelParams :(id)sender
{
[NSApp stopModalWithCode:NSModalResponseCancel];
[_cfgPanel close];
}
@end
|