Skip to content

Commit

Permalink
Fix issue with image finalazing info. More debugging info.
Browse files Browse the repository at this point in the history
  • Loading branch information
mczachurski committed Aug 5, 2018
1 parent 8294eed commit 9c6aee0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Sources/wallpapper/ConsoleOutput/ConsoleIO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class ConsoleIO {
func writeMessage(_ message: String, to: OutputType = .standard) {
switch to {
case .standard:
print("\(message)")
fputs("\(message)", stdout)
fflush(stdout)
case .error:
fputs("Error: \(message)\n", stderr)
}
Expand Down
11 changes: 11 additions & 0 deletions Sources/wallpapper/Errors/ImageFinalizingError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// ImageFinalizingError.swift
// wallpapper
//
// Created by Marcin Czachurski on 05/08/2018.
//

import Foundation

class ImageFinalizingError: Error {
}
18 changes: 16 additions & 2 deletions Sources/wallpapper/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Generator {
var picureInfos: [PictureInfo]
let outputFileName: String
let options = [kCGImageDestinationLossyCompressionQuality: 1.0]
let consoleIO = ConsoleIO()

init(picureInfos: [PictureInfo], outputFileName: String) {
self.picureInfos = picureInfos
Expand All @@ -35,7 +36,10 @@ class Generator {

for (index, pictureInfo) in self.picureInfos.enumerated() {
let fileURL = currentDirectoryURL.appendingPathComponent(pictureInfo.fileName)

self.consoleIO.writeMessage("Reading image file: '\(fileURL)'...")
let orginalImage = NSImage(contentsOf: fileURL)
self.consoleIO.writeMessage("OK.\n")

if let cgImage = orginalImage?.CGImage {

Expand All @@ -55,18 +59,28 @@ class Generator {
throw AddTagImageError()
}

self.consoleIO.writeMessage("Adding image and metadata...")
CGImageDestinationAddImageAndMetadata(destination, cgImage, imageMetadata, self.options as CFDictionary)
self.consoleIO.writeMessage("OK.\n")
} else {
self.consoleIO.writeMessage("Adding image...")
CGImageDestinationAddImage(destination, cgImage, self.options as CFDictionary)
self.consoleIO.writeMessage("OK.\n")
}
}
}

CGImageDestinationFinalize(destination)
let imageData = destinationData as Data
self.consoleIO.writeMessage("Finalizing image container...")
guard CGImageDestinationFinalize(destination) else {
throw ImageFinalizingError()
}
self.consoleIO.writeMessage("OK.\n")

self.consoleIO.writeMessage("Saving data to file '\(self.outputFileName)'...")
let imageData = destinationData as Data
let outputURL = currentDirectoryURL.appendingPathComponent(self.outputFileName)
try imageData.write(to: outputURL)
self.consoleIO.writeMessage("OK.\n")
}
} else {
throw NotSupportedSystemError()
Expand Down
13 changes: 9 additions & 4 deletions Sources/wallpapper/Program.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,25 @@ class Program {
}

let fileURL = currentDirectoryURL.appendingPathComponent(inputFileName)

self.consoleIO.writeMessage("Reading JSON file: '\(fileURL)'...")
let inputFileContents = try Data(contentsOf: fileURL)
self.consoleIO.writeMessage("OK.\n")

let decoder = JSONDecoder()
self.consoleIO.writeMessage("Decoding JSON file...")
let picureInfos = try decoder.decode([PictureInfo].self, from: inputFileContents)
self.consoleIO.writeMessage("OK (\(picureInfos.count) pictures).\n")

let generator = Generator(picureInfos: picureInfos, outputFileName: self.outputFileName)
try generator.run()

} catch let inputError as InputFileNotExistsError {
self.consoleIO.writeMessage("Error: '\(inputError)'. Current directory: '\(inputError.currentDirectory)'. ")
self.consoleIO.writeMessage("type: '\(inputError)'. Current directory: '\(inputError.currentDirectory)'. ", to: .error)
return false
}
catch {
self.consoleIO.writeMessage("Error: '\(error)'.")
self.consoleIO.writeMessage("type: \(error)", to: .error)
return false
}

Expand Down Expand Up @@ -90,15 +95,15 @@ class Program {
}

if self.inputFileName == "" {
self.consoleIO.writeMessage("Unknown input file name.")
self.consoleIO.writeMessage("unknown input file name.", to: .error)
return (true, false)
}

return (false, false)
}

func printVersion() {
self.consoleIO.writeMessage("1.1.0")
self.consoleIO.writeMessage("1.2.0")
}

func printUsage() {
Expand Down

0 comments on commit 9c6aee0

Please sign in to comment.