| 1 |
// |
| 2 |
// CRTableViewController.m |
| 3 |
// ImageSpliter |
| 4 |
// |
| 5 |
// Created by hiroshi tomioka on 13/02/12. |
| 6 |
// Copyright (c) 2013å¹´ hiroshi tomioka. All rights reserved. |
| 7 |
// |
| 8 |
|
| 9 |
#import "CRTableViewController.h" |
| 10 |
#import <Cocoa/Cocoa.h> |
| 11 |
#import "CRDropView.h" |
| 12 |
#import "NSImage+data.h" |
| 13 |
#import "ZipArchive.h" |
| 14 |
|
| 15 |
@implementation CRTableViewController { |
| 16 |
NSMutableArray *_dataArray; |
| 17 |
IBOutlet CRDropView *mainView; |
| 18 |
IBOutlet NSTableView *_tableView; |
| 19 |
IBOutlet NSProgressIndicator *_indicator; |
| 20 |
} |
| 21 |
@synthesize _dirPath; |
| 22 |
//@synthesize mainView; |
| 23 |
|
| 24 |
- (id)init |
| 25 |
{ |
| 26 |
self = [super init]; |
| 27 |
if (self) { |
| 28 |
NSLog(@"%s", __func__); |
| 29 |
|
| 30 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondFileDropedNotification:) name:@"files droped" object:nil]; |
| 31 |
|
| 32 |
_dataArray = [[NSMutableArray alloc] init]; |
| 33 |
} |
| 34 |
return self; |
| 35 |
} |
| 36 |
|
| 37 |
- (void)awakeFromNib { |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
#pragma mark - NSTableView data source |
| 43 |
|
| 44 |
- (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView |
| 45 |
{ |
| 46 |
NSLog(@"%s", __func__); |
| 47 |
|
| 48 |
return _dataArray.count; |
| 49 |
} |
| 50 |
|
| 51 |
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row |
| 52 |
{ |
| 53 |
NSDictionary *data = [_dataArray objectAtIndex:row]; |
| 54 |
if ([[tableColumn identifier] isEqualToString:@"TITLE"]) { |
| 55 |
return [data objectForKey:@"title"]; |
| 56 |
} else { |
| 57 |
return [data objectForKey:@"description"]; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
//ドãƒãƒƒãƒ—ファイルã�®é€šçŸ¥ |
| 62 |
- (void)respondFileDropedNotification:(NSNotification*)notification { |
| 63 |
NSDictionary* dic = notification.userInfo; |
| 64 |
NSArray *files = [dic objectForKey:@"files"]; |
| 65 |
|
| 66 |
_dataArray = [[NSMutableArray alloc] init]; |
| 67 |
for (int i = 0; i < [files count]; i++) { |
| 68 |
NSDictionary *data = @{@"title": [files[i] lastPathComponent], |
| 69 |
@"description": files[i]}; |
| 70 |
[_dataArray addObject:data]; |
| 71 |
} |
| 72 |
[_tableView reloadData]; |
| 73 |
} |
| 74 |
|
| 75 |
- (NSString*)imageTypeExp:(NSInteger)type { |
| 76 |
NSString *exp = @""; |
| 77 |
switch (type) { |
| 78 |
case IMAGE_TYPE_JPEG: |
| 79 |
exp = @"jpg"; |
| 80 |
break; |
| 81 |
case IMAGE_TYPE_PNG: |
| 82 |
exp = @"png"; |
| 83 |
break; |
| 84 |
case IMAGE_TYPE_TIFF: |
| 85 |
exp = @"tif"; |
| 86 |
break; |
| 87 |
case IMAGE_TYPE_JPEG2000: |
| 88 |
exp = @"jpg"; |
| 89 |
break; |
| 90 |
case IMAGE_TYPE_BMP: |
| 91 |
exp = @"bmp"; |
| 92 |
break; |
| 93 |
} |
| 94 |
return exp; |
| 95 |
} |
| 96 |
|
| 97 |
- (IBAction)buildButton:(id)sender { |
| 98 |
NSLog(@"pushed"); |
| 99 |
BOOL isDirectory; |
| 100 |
NSInteger imageType = [[NSUserDefaults standardUserDefaults] integerForKey:@"image type"]; |
| 101 |
NSInteger zipFlag = [[NSUserDefaults standardUserDefaults] integerForKey:@"zip flag"]; |
| 102 |
NSInteger sizeFlag = [[NSUserDefaults standardUserDefaults] integerForKey:@"fit size"]; |
| 103 |
int fitWidth = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"fit width"]; |
| 104 |
int fitHeight = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"fit height"]; |
| 105 |
double qualityJpeg2000 = [[NSUserDefaults standardUserDefaults] doubleForKey:@"jpeg2000 quality"]; |
| 106 |
double qualityJpeg = [[NSUserDefaults standardUserDefaults] doubleForKey:@"jpeg quality"]; |
| 107 |
|
| 108 |
NSString *dir = NSTemporaryDirectory(); |
| 109 |
NSLog(@"dir %@", dir); |
| 110 |
|
| 111 |
if (0 == zipFlag) { |
| 112 |
NSInteger result; |
| 113 |
NSOpenPanel* openPanel; |
| 114 |
|
| 115 |
openPanel = [NSOpenPanel openPanel]; |
| 116 |
[openPanel setCanChooseDirectories:YES]; |
| 117 |
[openPanel setCanCreateDirectories:YES]; |
| 118 |
result = [openPanel runModal]; |
| 119 |
if(result == NSOKButton) { |
| 120 |
NSString* dirPath = [[openPanel URL] path]; |
| 121 |
BOOL isDirectory; |
| 122 |
if ([[NSFileManager defaultManager] fileExistsAtPath:dirPath isDirectory:&isDirectory]) { |
| 123 |
if (isDirectory) { |
| 124 |
self._dirPath = dirPath; |
| 125 |
dir = dirPath; |
| 126 |
} else { |
| 127 |
NSRunAlertPanel(@"info", @"set a folder", |
| 128 |
nil, nil, nil); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} else { |
| 133 |
|
| 134 |
NSInteger result; |
| 135 |
NSSavePanel* savePanel; |
| 136 |
|
| 137 |
savePanel = [NSSavePanel savePanel]; |
| 138 |
NSArray *allowedFileTypes = [NSArray arrayWithObjects:@"zip",@"'ZIP'",nil]; |
| 139 |
[savePanel setAllowedFileTypes:allowedFileTypes]; |
| 140 |
|
| 141 |
[savePanel setCanCreateDirectories:YES]; |
| 142 |
result = [savePanel runModal]; |
| 143 |
if(result == NSOKButton) { |
| 144 |
NSString* dirPath = [[savePanel URL] path]; |
| 145 |
self._dirPath = dirPath; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
[_indicator setDoubleValue:0]; |
| 154 |
if ([[NSFileManager defaultManager] fileExistsAtPath:dir isDirectory:&isDirectory]) { |
| 155 |
if (isDirectory) { |
| 156 |
_indicator.minValue = 0; |
| 157 |
_indicator.maxValue = [mainView._files count] * 2; |
| 158 |
|
| 159 |
int count = 0; |
| 160 |
int progress = 0; |
| 161 |
for (int i = 0; i < [mainView._files count]; i++) { |
| 162 |
|
| 163 |
NSImage *image; |
| 164 |
NSData *data; |
| 165 |
NSBitmapImageRep *bitmapImageRep; |
| 166 |
|
| 167 |
image = [[NSImage alloc] initWithContentsOfFile:mainView._files[i]]; |
| 168 |
data = [image TIFFRepresentation]; |
| 169 |
bitmapImageRep = [NSBitmapImageRep imageRepWithData:data]; |
| 170 |
NSSize pointSize = NSMakeSize(bitmapImageRep.pixelsWide, bitmapImageRep.pixelsHigh); |
| 171 |
|
| 172 |
if (pointSize.width < pointSize.height) { |
| 173 |
NSString *filename; |
| 174 |
filename = [dir stringByAppendingFormat:@"/%06d.%@", count++, [self imageTypeExp:imageType]]; |
| 175 |
|
| 176 |
NSImage *newImage = image; |
| 177 |
if (0 == sizeFlag) { |
| 178 |
newImage = [image fitImageInSize:NSMakeSize(fitWidth, fitHeight)]; |
| 179 |
} |
| 180 |
switch (imageType) { |
| 181 |
case IMAGE_TYPE_JPEG: |
| 182 |
[[newImage JPEGDataWithQuality:qualityJpeg] writeToFile:filename atomically:YES]; |
| 183 |
break; |
| 184 |
case IMAGE_TYPE_PNG: |
| 185 |
[[newImage PNGDataWithInterlaced:YES] writeToFile:filename atomically:YES]; |
| 186 |
break; |
| 187 |
case IMAGE_TYPE_TIFF: |
| 188 |
[[newImage TIFFRepresentation] writeToFile:filename atomically:YES]; |
| 189 |
break; |
| 190 |
case IMAGE_TYPE_JPEG2000: |
| 191 |
[[newImage JPEG2000DataWithQuality:qualityJpeg2000] writeToFile:filename atomically:YES]; |
| 192 |
break; |
| 193 |
case IMAGE_TYPE_BMP: |
| 194 |
[[newImage BMPData] writeToFile:filename atomically:YES]; |
| 195 |
break; |
| 196 |
} |
| 197 |
|
| 198 |
progress += 2; |
| 199 |
[_indicator setDoubleValue:progress]; |
| 200 |
} else { |
| 201 |
for (int j = 0; j < 2; j++) { |
| 202 |
NSString *filename; |
| 203 |
filename = [dir stringByAppendingFormat:@"/%06d.%@", count++, [self imageTypeExp:imageType]]; |
| 204 |
NSRect rc = NSMakeRect((pointSize.width / 2.0 * (1 - j)), 0, pointSize.width / 2.0, pointSize.height); |
| 205 |
|
| 206 |
NSImage *newImage = [image trimImageByRect:rc]; |
| 207 |
if (0 == sizeFlag) { |
| 208 |
newImage = [newImage fitImageInSize:NSMakeSize(fitWidth, fitHeight)]; |
| 209 |
} |
| 210 |
switch (imageType) { |
| 211 |
case IMAGE_TYPE_JPEG: |
| 212 |
[[newImage JPEGDataWithQuality:qualityJpeg] writeToFile:filename atomically:YES]; |
| 213 |
break; |
| 214 |
case IMAGE_TYPE_PNG: |
| 215 |
[[newImage PNGDataWithInterlaced:YES] writeToFile:filename atomically:YES]; |
| 216 |
break; |
| 217 |
case IMAGE_TYPE_TIFF: |
| 218 |
[[newImage TIFFRepresentation] writeToFile:filename atomically:YES]; |
| 219 |
break; |
| 220 |
case IMAGE_TYPE_JPEG2000: |
| 221 |
[[newImage JPEG2000DataWithQuality:qualityJpeg2000] writeToFile:filename atomically:YES]; |
| 222 |
break; |
| 223 |
case IMAGE_TYPE_BMP: |
| 224 |
[[newImage BMPData] writeToFile:filename atomically:YES]; |
| 225 |
break; |
| 226 |
} |
| 227 |
|
| 228 |
progress++; |
| 229 |
[_indicator setDoubleValue:progress]; |
| 230 |
} |
| 231 |
} |
| 232 |
[image release]; |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
// NSError *error; |
| 237 |
// for (int i = 0; i < count; i++) { |
| 238 |
// NSString* atPath = [dir stringByAppendingFormat:@"/%06d.jpg", i]; |
| 239 |
// NSString* toPath = [self._dirPath stringByAppendingFormat:@"/a%06d.jpg", i]; |
| 240 |
// if (![[NSFileManager defaultManager] moveItemAtPath:atPath toPath:toPath error:&error]) { |
| 241 |
// |
| 242 |
// NSRunAlertPanel(@"info", error.debugDescription, |
| 243 |
// nil, nil, nil); |
| 244 |
// break; |
| 245 |
// } |
| 246 |
// } |
| 247 |
|
| 248 |
if (0 != zipFlag) { |
| 249 |
NSString* zipFile = self._dirPath; |
| 250 |
ZipArchive* archiver = [[ZipArchive alloc] init]; |
| 251 |
[archiver CreateZipFile2:zipFile]; |
| 252 |
|
| 253 |
for (int i = 0; i < count; i++) { |
| 254 |
NSString* atPath = [dir stringByAppendingFormat:@"/%06d.%@", i, [self imageTypeExp:imageType]]; |
| 255 |
NSString* toPath = [NSString stringWithFormat:@"%06d.%@", i, [self imageTypeExp:imageType]]; |
| 256 |
[archiver addFileToZip:atPath |
| 257 |
newname:toPath]; |
| 258 |
} |
| 259 |
[archiver CloseZipFile2]; |
| 260 |
[archiver release]; |
| 261 |
|
| 262 |
for (int i = 0; i < count; i++) { |
| 263 |
NSError *error; |
| 264 |
NSString* atPath = [dir stringByAppendingFormat:@"/%06d.%@", i, [self imageTypeExp:imageType]]; |
| 265 |
|
| 266 |
if (![[NSFileManager defaultManager] removeItemAtPath:atPath error:&error]) { |
| 267 |
NSLog(@"error %@", error.debugDescription); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
NSRunAlertPanel(@"info", @"finish", |
| 273 |
nil, nil, nil); |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
} |
| 278 |
} else { |
| 279 |
NSRunAlertPanel(@"info", @"set a folder", |
| 280 |
nil, nil, nil); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
- (IBAction)onClearButton:(id)sender |
| 285 |
{ |
| 286 |
[mainView._files removeAllObjects]; |
| 287 |
_dataArray = [[NSMutableArray alloc] init]; |
| 288 |
[_tableView reloadData]; |
| 289 |
} |
| 290 |
|
| 291 |
- (IBAction)onOpenButton:(id)sender |
| 292 |
{ |
| 293 |
NSInteger result; |
| 294 |
NSOpenPanel* openPanel; |
| 295 |
|
| 296 |
openPanel = [NSOpenPanel openPanel]; |
| 297 |
[openPanel setCanChooseDirectories:YES]; |
| 298 |
[openPanel setCanCreateDirectories:YES]; |
| 299 |
[openPanel setAllowsMultipleSelection:YES]; |
| 300 |
|
| 301 |
result = [openPanel runModal]; |
| 302 |
if(result == NSOKButton) { |
| 303 |
|
| 304 |
|
| 305 |
for (NSURL *url in [openPanel URLs]) { |
| 306 |
NSString *path = [url path]; |
| 307 |
|
| 308 |
BOOL isDirectory; |
| 309 |
if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) { |
| 310 |
if (isDirectory) { |
| 311 |
NSArray* array = [[NSFileManager defaultManager] subpathsAtPath:path]; |
| 312 |
array = [array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; |
| 313 |
|
| 314 |
for (NSString *filename in array) { |
| 315 |
filename = [path stringByAppendingPathComponent:filename]; |
| 316 |
if ([NSImage isImageFile:filename]) { |
| 317 |
NSLog(@"%@", filename); |
| 318 |
[mainView._files addObject:filename]; |
| 319 |
} |
| 320 |
} |
| 321 |
} else { |
| 322 |
if ([NSImage isImageFile:path]) { |
| 323 |
[mainView._files addObject:path]; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
NSArray *files = mainView._files; |
| 332 |
|
| 333 |
_dataArray = [[NSMutableArray alloc] init]; |
| 334 |
for (int i = 0; i < [files count]; i++) { |
| 335 |
NSDictionary *data = @{@"title": [files[i] lastPathComponent], |
| 336 |
@"description": files[i]}; |
| 337 |
[_dataArray addObject:data]; |
| 338 |
} |
| 339 |
[_tableView reloadData]; |
| 340 |
} |
| 341 |
|
| 342 |
- (IBAction)onInfo:(id)sender { |
| 343 |
if (0 < [mainView._files count] && 0 <= [_tableView selectedRow]) { |
| 344 |
NSString *selectedStringValue = [mainView._files objectAtIndex:[_tableView selectedRow]]; |
| 345 |
if (selectedStringValue) { |
| 346 |
if ([[NSWorkspace sharedWorkspace] openFile:selectedStringValue]) { //withApplication |
| 347 |
return; |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
NSRunAlertPanel(@"info", @"can't open", |
| 352 |
nil, nil, nil); |
| 353 |
} |
| 354 |
|
| 355 |
//- (IBAction)savePDF:(id)sender |
| 356 |
//{ |
| 357 |
// // (1) setup filename |
| 358 |
// NSArray *paths = NSSearchPathForDirectoriesInDomains( |
| 359 |
// NSDesktopDirectory, NSUserDomainMask, YES); |
| 360 |
// NSString* path = [NSString stringWithFormat:@"%@/test.pdf", |
| 361 |
// [paths objectAtIndex:0], nil]; |
| 362 |
// |
| 363 |
// // (2) setup WebView |
| 364 |
// NSView* view = [[[_web_view mainFrame] frameView] documentView]; |
| 365 |
// |
| 366 |
// // (3) setup NSDictionary for NSPrintInfo |
| 367 |
// NSMutableDictionary* p_dict = [NSMutableDictionary |
| 368 |
// dictionaryWithDictionary:[[NSPrintInfo sharedPrintInfo] dictionary]]; |
| 369 |
// [p_dict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition]; |
| 370 |
// [p_dict setObject:path forKey:NSPrintSavePath]; |
| 371 |
// |
| 372 |
// // (4) setup NSPrintInfo |
| 373 |
// NSPrintInfo* p_info = [[NSPrintInfo alloc] initWithDictionary:p_dict]; |
| 374 |
// [p_info setHorizontalPagination:NSAutoPagination]; |
| 375 |
// [p_info setVerticalPagination:NSAutoPagination]; |
| 376 |
// [p_info setVerticallyCentered:NO]; |
| 377 |
// |
| 378 |
// // (5) setup NSPrintOperation |
| 379 |
// NSPrintOperation* p_ope = |
| 380 |
// [NSPrintOperation printOperationWithView:view |
| 381 |
// printInfo:p_info]; |
| 382 |
// [p_ope setShowsPrintPanel:NO]; |
| 383 |
// |
| 384 |
// // (6) print it |
| 385 |
// [p_ope runOperation]; |
| 386 |
//} |
| 387 |
|
| 388 |
@end |