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
|
//
// Lynkeos
// SER_Writer.m
//
// Created by Jean-Etienne LAMIAUD on Wed Dec 20 2023.
// Copyright (c) 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 <stdio.h>
#include <math.h>
#include <AppKit/AppKit.h>
#include <LynkeosCore/LynkeosMetadata.h>
#include "SER_Writer.h"
@implementation SER_Writer
+ (void) load
{
// Nothing to do, this is just to force the runtime to load this class
}
- (id) init
{
if ( (self = [super init]) != nil )
{
_nPlanes = 0;
}
return( self );
}
+ (NSString*) writerName { return( @"SER" ); }
+ (NSString*) fileExtension { return( @"ser" ); }
+ (BOOL) canSaveDataWithPlanes:(u_short)nPlanes
width:(u_short)w height:(u_short)h
metaData:(NSDictionary*)metaData
{
return( nPlanes == 3 || nPlanes == 1 );
}
- (NSPanel*) configurationPanel
{
return nil;
}
+ (id <LynkeosFileWriter>) writerForURL:(NSURL*)url
planes:(u_short)nPlanes
width:(u_short)w height:(u_short)h
metaData:(NSDictionary*)metaData
{
SER_Writer *writer = [[[self alloc] init] autorelease];
writer->_nPlanes = nPlanes;
return writer;
}
- (u_short) numberOfPlanes {return _nPlanes;}
- (void) saveMovieAtURL:(NSURL *)url
withDelegate:(id<LynkeosMovieFileWriterDelegate>)delegate
opaque:(void *)opaque
blackLevel:(double)black
whiteLevel:(double)white
withPlanes:(u_short)nPlanes
width:(u_short)w
height:(u_short)h
metaData:(NSDictionary *)metaData
{
FILE *file = fopen([[url path] fileSystemRepresentation], "wb");
const double max = white - black;
const REAL * const * planes;
uint16_t *dataLine;
u_short lineW;
int32_t count = 0;
if (file == NULL)
return;
SER_Header_t hdr = {
.FileID = {'0'},
.LuID = 0,
.ColorID = (nPlanes == 3 ? SER_RGB : SER_MONO),
.LittleEndian = 1,
.ImageWidth = w,
.ImageHeight = h,
.PixelDepthPerPlane = 16,
.FrameCount = 0, // To be updated later
.Observer = {0},
.Instrument = {0},
.Telescope = {0},
.DateTime = 0,
.DateTime_UTC = 0
};
#if !GNUSTEP
if (metaData != nil)
{
NSString *str;
NSArray *auth = (NSArray*)[metaData objectForKey:LynkeosMD_Authors()];
if (auth != nil)
strncpy(hdr.Observer,
[[auth componentsJoinedByString:@" "] UTF8String],
SER_STRING_LENGTH);
str = (NSString*)[metaData objectForKey:LynkeosMD_CameraModel()];
if (str != nil)
strncpy(hdr.Instrument, [str UTF8String], SER_STRING_LENGTH);
str = (NSString*)[metaData objectForKey:LynkeosMD_Telescope()];
if (str != nil)
strncpy(hdr.Telescope, [str UTF8String], SER_STRING_LENGTH);
NSDate *d = (NSDate*)[metaData objectForKey:LynkeosMD_CaptureDate()];
if (d != nil)
{
NSTimeZone *tz = [NSTimeZone systemTimeZone];
NSTimeInterval o = (NSTimeInterval)[tz secondsFromGMTForDate:d];
hdr.DateTime_UTC = (int64_t)round([d timeIntervalSinceReferenceDate]/SER_DATE_TIMEBASE)
+ SER_DATE_ORIGIN;
hdr.DateTime = (int64_t)round(([d timeIntervalSinceReferenceDate] + o)/SER_DATE_TIMEBASE)
+ SER_DATE_ORIGIN;
}
}
#endif
if (SER_write_header(file, hdr) < 0)
{
fclose(file);
return;
}
dataLine = (uint16_t*)malloc(w*3*sizeof(uint16_t));
for (;;)
{
BOOL canceled;
[delegate getNextFrameWithData:&planes lineWidth:&lineW opaque:opaque canceled:&canceled];
if (canceled || planes == NULL)
break;
for (u_short y = 0; y < h; y++)
{
for (u_short x = 0; x < w; x++)
{
for (u_short c = 0; c < nPlanes; c++)
{
double v = GET_SAMPLE(planes[c], x, y, lineW)*max;
if ( v < 0.0 )
v = 0.0;
if ( v > 65535.0 )
v = 65535.0;
dataLine[x*3 + c] = (uint16_t)v;
}
}
if (fwrite(dataLine, sizeof(uint16_t), w*3, file) < 0)
{
fclose(file);
free(dataLine);
return;
}
}
count++;
}
free(dataLine);
SER_write_frameCount(file, count);
fclose(file);
}
@end
|