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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
//
// Lynkeos
// $Id: MyImageList.m,v 1.11 2005/01/27 23:06:02 j-etienne Exp $
//
// Created by Jean-Etienne LAMIAUD on Wed Sep 24 2003.
// Copyright (c) 2003-2005. 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 "MyImageList.h"
#define K_LIST_KEY @"images"
#define K_SINGLE_PRECISION_KEY @"float"
#define K_RAW_STACK_KEY @"stack"
#define K_BLACK_LEVEL_KEY @"blackl"
#define K_WHITE_LEVEL_KEY @"whitel"
@implementation MyImageList
//==============================================================================
// Coding
//==============================================================================
- (void)encodeWithCoder:(NSCoder *)encoder
{
NSData *stackWrapper = nil;
if ( _rawStack != nil )
stackWrapper = [NSData dataWithBytesNoCopy:_rawStack
length:_stackSize
freeWhenDone:NO];
// List data
[encoder encodeObject:_list forKey:K_LIST_KEY];
// Stack data
[encoder encodeBool:
#ifdef FLOAT_PIXELS
YES
#else
NO
#endif
forKey:K_SINGLE_PRECISION_KEY];
[encoder encodeObject:stackWrapper forKey:K_RAW_STACK_KEY];
[encoder encodeDouble:_blackLevel forKey:K_BLACK_LEVEL_KEY];
[encoder encodeDouble:_whiteLevel forKey:K_WHITE_LEVEL_KEY];
}
- (id)initWithCoder:(NSCoder *)decoder
{
[self init];
if ( [decoder containsValueForKey:K_LIST_KEY] )
{
NSData *stackWrapper;
// List data
_list = [[decoder decodeObjectForKey:K_LIST_KEY] retain];
// Stack data
stackWrapper = [decoder decodeObjectForKey:K_RAW_STACK_KEY];
if ( stackWrapper != nil )
{
BOOL single = [decoder decodeBoolForKey:K_SINGLE_PRECISION_KEY];
u_long i;
REAL *buf;
_stackSize = [stackWrapper length];
if ( _stackSize != 0 )
{
// Convert, if the precision is different
#ifdef FLOAT_PIXELS
if ( ! single )
{
double *source;
_stackSize /= sizeof(double)/sizeof(float);
#else
if ( single )
{
float *source;
_stackSize *= sizeof(double)/sizeof(float);
#endif
_rawStack = (RGB*)malloc( _stackSize );
buf = (REAL*)_rawStack;
source = (typeof(source))[stackWrapper bytes];
for( i = 0; i < _stackSize/sizeof(REAL); i++ )
buf[i] = (REAL)source[i];
}
else
{
_rawStack = (RGB*)malloc( _stackSize );
[stackWrapper getBytes:_rawStack];
}
}
}
if ( [decoder containsValueForKey:K_BLACK_LEVEL_KEY] &&
[decoder containsValueForKey:K_WHITE_LEVEL_KEY])
{
_blackLevel = [decoder decodeDoubleForKey:K_BLACK_LEVEL_KEY];
_whiteLevel = [decoder decodeDoubleForKey:K_WHITE_LEVEL_KEY];
}
return( self );
}
else
{
// File format is not compatible, abort loading
[self release];
return( nil );
}
}
//==============================================================================
// Initializers, Constructors and destructors
//==============================================================================
- (id)init
{
self = [super init];
if (self)
{
// List
_list = nil;
// Stack data
_rawStack = nil;
_stackSize = 0;
[self invalidateLevels];
}
return self;
}
- (id) initWithArray :(NSArray*)list
{
[self init];
_list = [list retain];
return( self );
}
+ (id) imageListWithArray :(NSArray*)list
{
return( [[[self alloc] initWithArray:list] autorelease] );
}
- (void) dealloc
{
[_list release];
if ( _rawStack != NULL )
free( _rawStack );
[super dealloc];
}
//==============================================================================
// Read accessors
//==============================================================================
// List
- (NSMutableArray*) imageArray { return( _list ); }
// Stack data
- (RGB*) stack { return( _rawStack ); }
- (u_long) stackSize { return(_stackSize); }
- (REAL) blackLevel { return(_blackLevel); }
- (REAL) whiteLevel { return(_whiteLevel); }
- (BOOL) validLevels { return( _whiteLevel > _blackLevel ); }
// Enumerator access
- (MyImageListItem*) firstItem
{
MyImageListEnumerator* list = [self imageEnumerator];
MyImageListItem *item;
while ( (item = [list nextObject]) != nil &&
[item getSelectionState] != NSOnState )
;
return( item );
}
- (MyImageListItem*) lastItem
{
MyImageListEnumerator* list = [self imageEnumeratorStartAt:nil
directSense:NO];
MyImageListItem *item;
while ( (item = [list nextObject]) != nil &&
[item getSelectionState] != NSOnState )
;
return( item );
}
- (MyImageListEnumerator*)imageEnumerator
{
return( [[[MyImageListEnumerator alloc] initWithImageList:_list]
autorelease] );
}
- (MyImageListEnumerator*) imageEnumeratorStartAt:(id)item
directSense:(BOOL)direct
{
return( [[[MyImageListEnumerator alloc] initWithImageList:_list
startAt:item directSense:direct]
autorelease] );
}
//==============================================================================
// Write accessors
//==============================================================================
- (BOOL) setStack :(RGB*)stack size:(u_long)size
{
// free the previous stack if any
if ( _rawStack != NULL )
free( _rawStack );
_rawStack = stack;
_stackSize = size;
return( YES );
}
- (BOOL) setBlackLevel:(REAL)black whiteLevel:(REAL)white
{
if ( _blackLevel == black && _whiteLevel == white )
return( NO );
_blackLevel = black;
_whiteLevel = white;
return( YES );
}
- (BOOL) invalidateLevels
{
return( [self setBlackLevel:0.0 whiteLevel:-1.0] );
}
//==============================================================================
// Actions
//==============================================================================
- (BOOL) addItem :(MyImageListItem*)item
{
if ( _list == nil )
_list = [[NSMutableArray array] retain];
[_list addObject: item];
return( YES );
}
- (BOOL) deleteItem :(MyImageListItem*)item
{
if ( [item isMemberOfClass: [MyMovieImage class]] )
{
MyMovie* movie = [(MyMovieImage*)item getParent];
NSAssert( [_list containsObject:movie],
@"Cannot delete from a nonexistent movie!" );
[movie deleteMovieImage:(MyMovieImage*)item];
}
else
{
NSAssert( [_list containsObject:item],
@"Cannot delete a nonexistent item!" );
[_list removeObject:item];
}
return( YES );
}
- (BOOL) changeItemSelection :(MyImageListItem*)item value:(BOOL)v
{
if ( [item getSelectionState] == (v ? NSOnState : NSOffState) )
return( NO );
// Set the desired value
[item setSelected:v];
return( YES );
}
@end
|