Merge commit '3929ef4207d4cb7552653a37de4251977621907f'

This commit is contained in:
Peter Iakovlev 2018-12-25 17:58:15 +04:00
commit 50b0ebf957
3 changed files with 55 additions and 14 deletions

View File

@ -229,7 +229,7 @@ func decodeTinyThumbnail(data: Data) -> Data? {
resultData.append(data.subdata(in: 3 ..< data.count)) resultData.append(data.subdata(in: 3 ..< data.count))
resultData.append(tinyThumbnailFooterPattern) resultData.append(tinyThumbnailFooterPattern)
resultData.withUnsafeMutableBytes({ (resultBytes: UnsafeMutablePointer<UInt8>) -> Void in resultData.withUnsafeMutableBytes({ (resultBytes: UnsafeMutablePointer<UInt8>) -> Void in
resultBytes[164] = width - 3 resultBytes[164] = width
resultBytes[166] = height resultBytes[166] = height
}) })
return resultData return resultData

View File

@ -62,6 +62,22 @@ public struct ImageCorners: Equatable {
public let bottomLeft: ImageCorner public let bottomLeft: ImageCorner
public let bottomRight: ImageCorner public let bottomRight: ImageCorner
public var isEmpty: Bool {
if self.topLeft != .Corner(0.0) {
return false
}
if self.topRight != .Corner(0.0) {
return false
}
if self.bottomLeft != .Corner(0.0) {
return false
}
if self.bottomRight != .Corner(0.0) {
return false
}
return true
}
public init(radius: CGFloat) { public init(radius: CGFloat) {
self.topLeft = .Corner(radius) self.topLeft = .Corner(radius)
self.topRight = .Corner(radius) self.topRight = .Corner(radius)

View File

@ -625,8 +625,6 @@ public func chatMessagePhotoInternal(photoData: Signal<(Data?, Data?, Bool), NoE
return ({ return ({
return nil return nil
}, { arguments in }, { arguments in
let context = DrawingContext(size: arguments.drawingSize, clear: true)
let drawingRect = arguments.drawingRect let drawingRect = arguments.drawingRect
var fittedSize = arguments.imageSize var fittedSize = arguments.imageSize
if abs(fittedSize.width - arguments.boundingSize.width).isLessThanOrEqualTo(CGFloat(1.0)) { if abs(fittedSize.width - arguments.boundingSize.width).isLessThanOrEqualTo(CGFloat(1.0)) {
@ -685,15 +683,14 @@ public func chatMessagePhotoInternal(photoData: Signal<(Data?, Data?, Bool), NoE
if let thumbnailImage = thumbnailImage { if let thumbnailImage = thumbnailImage {
let thumbnailSize = CGSize(width: thumbnailImage.width, height: thumbnailImage.height) let thumbnailSize = CGSize(width: thumbnailImage.width, height: thumbnailImage.height)
let initialThumbnailContextFittingSize = fittedSize.fitted(CGSize(width: 100.0, height: 100.0)) let initialThumbnailContextFittingSize = fittedSize.fitted(CGSize(width: 90.0, height: 90.0))
let thumbnailContextSize = thumbnailSize.aspectFitted(initialThumbnailContextFittingSize) let thumbnailContextSize = thumbnailSize.aspectFitted(initialThumbnailContextFittingSize)
let thumbnailContext = DrawingContext(size: thumbnailContextSize, scale: 1.0) let thumbnailContext = DrawingContext(size: thumbnailContextSize, scale: 1.0)
thumbnailContext.withFlippedContext { c in thumbnailContext.withFlippedContext { c in
c.interpolationQuality = .none
c.draw(thumbnailImage, in: CGRect(origin: CGPoint(), size: thumbnailContextSize)) c.draw(thumbnailImage, in: CGRect(origin: CGPoint(), size: thumbnailContextSize))
} }
//telegramFastBlur(Int32(thumbnailContextSize.width), Int32(thumbnailContextSize.height), Int32(thumbnailContext.bytesPerRow), thumbnailContext.bytes) telegramFastBlurMore(Int32(thumbnailContextSize.width), Int32(thumbnailContextSize.height), Int32(thumbnailContext.bytesPerRow), thumbnailContext.bytes)
var thumbnailContextFittingSize = CGSize(width: floor(arguments.drawingSize.width * 0.5), height: floor(arguments.drawingSize.width * 0.5)) var thumbnailContextFittingSize = CGSize(width: floor(arguments.drawingSize.width * 0.5), height: floor(arguments.drawingSize.width * 0.5))
if thumbnailContextFittingSize.width < 150.0 || thumbnailContextFittingSize.height < 150.0 { if thumbnailContextFittingSize.width < 150.0 || thumbnailContextFittingSize.height < 150.0 {
@ -716,6 +713,21 @@ public func chatMessagePhotoInternal(photoData: Signal<(Data?, Data?, Bool), NoE
} }
} }
if let blurredThumbnailImage = blurredThumbnailImage, fullSizeImage == nil, arguments.corners.isEmpty {
let context = DrawingContext(size: blurredThumbnailImage.size, scale: blurredThumbnailImage.scale, clear: true)
context.withFlippedContext { c in
c.setBlendMode(.copy)
if let cgImage = blurredThumbnailImage.cgImage {
c.interpolationQuality = .none
drawImage(context: c, image: cgImage, orientation: imageOrientation, in: CGRect(origin: CGPoint(), size: blurredThumbnailImage.size))
c.setBlendMode(.normal)
}
}
return context
}
let context = DrawingContext(size: arguments.drawingSize, clear: true)
context.withFlippedContext { c in context.withFlippedContext { c in
c.setBlendMode(.copy) c.setBlendMode(.copy)
if thumbnailImage == nil && fullSizeImage == nil { if thumbnailImage == nil && fullSizeImage == nil {
@ -1334,13 +1346,13 @@ func mediaGridMessagePhoto(account: Account, photoReference: ImageMediaReference
var blurredThumbnailImage: UIImage? var blurredThumbnailImage: UIImage?
if let thumbnailImage = thumbnailImage { if let thumbnailImage = thumbnailImage {
let thumbnailSize = CGSize(width: thumbnailImage.width, height: thumbnailImage.height) let thumbnailSize = CGSize(width: thumbnailImage.width, height: thumbnailImage.height)
let thumbnailContextSize = thumbnailSize.aspectFitted(CGSize(width: 150.0, height: 150.0)) let thumbnailContextSize = thumbnailSize.aspectFilled(CGSize(width: 90.0, height: 90.0))
let thumbnailContext = DrawingContext(size: thumbnailContextSize, scale: 1.0) let thumbnailContext = DrawingContext(size: thumbnailContextSize, scale: 1.0)
thumbnailContext.withFlippedContext { c in thumbnailContext.withFlippedContext { c in
c.interpolationQuality = .none c.interpolationQuality = .none
c.draw(thumbnailImage, in: CGRect(origin: CGPoint(), size: thumbnailContextSize)) c.draw(thumbnailImage, in: CGRect(origin: CGPoint(), size: thumbnailContextSize))
} }
telegramFastBlur(Int32(thumbnailContextSize.width), Int32(thumbnailContextSize.height), Int32(thumbnailContext.bytesPerRow), thumbnailContext.bytes) telegramFastBlurMore(Int32(thumbnailContextSize.width), Int32(thumbnailContextSize.height), Int32(thumbnailContext.bytesPerRow), thumbnailContext.bytes)
blurredThumbnailImage = thumbnailContext.generateImage() blurredThumbnailImage = thumbnailContext.generateImage()
} }
@ -2228,8 +2240,6 @@ func chatAvatarGalleryPhoto(account: Account, representations: [ImageRepresentat
return signal return signal
|> map { (thumbnailData, fullSizeData, fullSizeComplete) in |> map { (thumbnailData, fullSizeData, fullSizeComplete) in
return { arguments in return { arguments in
let context = DrawingContext(size: arguments.drawingSize, clear: true)
let drawingRect = arguments.drawingRect let drawingRect = arguments.drawingRect
var fittedSize = arguments.imageSize var fittedSize = arguments.imageSize
if abs(fittedSize.width - arguments.boundingSize.width).isLessThanOrEqualTo(CGFloat(1.0)) { if abs(fittedSize.width - arguments.boundingSize.width).isLessThanOrEqualTo(CGFloat(1.0)) {
@ -2273,21 +2283,21 @@ func chatAvatarGalleryPhoto(account: Account, representations: [ImageRepresentat
if let thumbnailImage = thumbnailImage { if let thumbnailImage = thumbnailImage {
let thumbnailSize = CGSize(width: thumbnailImage.width, height: thumbnailImage.height) let thumbnailSize = CGSize(width: thumbnailImage.width, height: thumbnailImage.height)
let initialThumbnailContextFittingSize = fittedSize.fitted(CGSize(width: 100.0, height: 100.0)) let initialThumbnailContextFittingSize = fittedSize.fitted(CGSize(width: 90.0, height: 90.0))
let thumbnailContextSize = thumbnailSize.aspectFitted(initialThumbnailContextFittingSize) let thumbnailContextSize = thumbnailSize.aspectFitted(initialThumbnailContextFittingSize)
let thumbnailContext = DrawingContext(size: thumbnailContextSize, scale: 1.0) let thumbnailContext = DrawingContext(size: thumbnailContextSize, scale: 1.0)
thumbnailContext.withFlippedContext { c in thumbnailContext.withFlippedContext { c in
c.draw(thumbnailImage, in: CGRect(origin: CGPoint(), size: thumbnailContextSize)) c.draw(thumbnailImage, in: CGRect(origin: CGPoint(), size: thumbnailContextSize))
} }
telegramFastBlur(Int32(thumbnailContextSize.width), Int32(thumbnailContextSize.height), Int32(thumbnailContext.bytesPerRow), thumbnailContext.bytes) telegramFastBlurMore(Int32(thumbnailContextSize.width), Int32(thumbnailContextSize.height), Int32(thumbnailContext.bytesPerRow), thumbnailContext.bytes)
var thumbnailContextFittingSize = CGSize(width: floor(arguments.drawingSize.width * 0.5), height: floor(arguments.drawingSize.width * 0.5)) var thumbnailContextFittingSize = CGSize(width: floor(arguments.drawingSize.width * 0.5), height: floor(arguments.drawingSize.width * 0.5))
if thumbnailContextFittingSize.width < 150.0 || thumbnailContextFittingSize.height < 150.0 { if thumbnailContextFittingSize.width < 150.0 || thumbnailContextFittingSize.height < 150.0 {
thumbnailContextFittingSize = thumbnailContextFittingSize.aspectFilled(CGSize(width: 150.0, height: 150.0)) thumbnailContextFittingSize = thumbnailContextFittingSize.aspectFilled(CGSize(width: 150.0, height: 150.0))
} }
if thumbnailContextFittingSize.width > thumbnailContextSize.width { if false, thumbnailContextFittingSize.width > thumbnailContextSize.width {
let additionalContextSize = thumbnailContextFittingSize let additionalContextSize = thumbnailContextFittingSize
let additionalBlurContext = DrawingContext(size: additionalContextSize, scale: 1.0) let additionalBlurContext = DrawingContext(size: additionalContextSize, scale: 1.0)
additionalBlurContext.withFlippedContext { c in additionalBlurContext.withFlippedContext { c in
@ -2303,6 +2313,21 @@ func chatAvatarGalleryPhoto(account: Account, representations: [ImageRepresentat
} }
} }
if let blurredThumbnailImage = blurredThumbnailImage, fullSizeImage == nil {
let context = DrawingContext(size: blurredThumbnailImage.size, scale: blurredThumbnailImage.scale, clear: true)
context.withFlippedContext { c in
c.setBlendMode(.copy)
if let cgImage = blurredThumbnailImage.cgImage {
c.interpolationQuality = .none
drawImage(context: c, image: cgImage, orientation: imageOrientation, in: CGRect(origin: CGPoint(), size: blurredThumbnailImage.size))
c.setBlendMode(.normal)
}
}
return context
}
let context = DrawingContext(size: arguments.drawingSize, clear: true)
context.withFlippedContext { c in context.withFlippedContext { c in
c.setBlendMode(.copy) c.setBlendMode(.copy)
if arguments.imageSize.width < arguments.boundingSize.width || arguments.imageSize.height < arguments.boundingSize.height { if arguments.imageSize.width < arguments.boundingSize.width || arguments.imageSize.height < arguments.boundingSize.height {
@ -2311,7 +2336,7 @@ func chatAvatarGalleryPhoto(account: Account, representations: [ImageRepresentat
c.setBlendMode(.copy) c.setBlendMode(.copy)
if let blurredThumbnailImage = blurredThumbnailImage, let cgImage = blurredThumbnailImage.cgImage { if let blurredThumbnailImage = blurredThumbnailImage, let cgImage = blurredThumbnailImage.cgImage {
c.interpolationQuality = .default c.interpolationQuality = .medium
drawImage(context: c, image: cgImage, orientation: imageOrientation, in: fittedRect) drawImage(context: c, image: cgImage, orientation: imageOrientation, in: fittedRect)
c.setBlendMode(.normal) c.setBlendMode(.normal)
} }