Camera and editor improvements

This commit is contained in:
Ilya Laktyushin
2023-06-11 16:35:42 +04:00
parent 901faa8d75
commit 36a5e8c018
4 changed files with 29 additions and 11 deletions

View File

@@ -639,7 +639,7 @@ public final class DrawingTextEntityView: DrawingEntityView, UITextViewDelegate
let emojiTextPosition = emojiRect.center.offsetBy(dx: -textSize.width / 2.0, dy: -textSize.height / 2.0) let emojiTextPosition = emojiRect.center.offsetBy(dx: -textSize.width / 2.0, dy: -textSize.height / 2.0)
let entity = DrawingStickerEntity(content: .file(file)) let entity = DrawingStickerEntity(content: .file(file))
entity.referenceDrawingSize = CGSize(width: itemSize * 2.5, height: itemSize * 2.5) entity.referenceDrawingSize = CGSize(width: itemSize * 4.0, height: itemSize * 4.0)
entity.scale = scale entity.scale = scale
entity.position = textPosition.offsetBy( entity.position = textPosition.offsetBy(
dx: (emojiTextPosition.x * cos(rotation) + emojiTextPosition.y * sin(rotation)) * scale, dx: (emojiTextPosition.x * cos(rotation) + emojiTextPosition.y * sin(rotation)) * scale,

View File

@@ -75,7 +75,11 @@ final class MediaEditorComposer {
self.drawingImage = nil self.drawingImage = nil
} }
self.entities = values.entities.map { $0.entity } .compactMap { composerEntityForDrawingEntity(account: account, entity: $0, colorSpace: colorSpace) } var entities: [MediaEditorComposerEntity] = []
for entity in values.entities {
entities.append(contentsOf: composerEntitiesForDrawingEntity(account: account, entity: entity.entity, colorSpace: colorSpace))
}
self.entities = entities
self.device = MTLCreateSystemDefaultDevice() self.device = MTLCreateSystemDefaultDevice()
if let device = self.device { if let device = self.device {
@@ -182,7 +186,11 @@ public func makeEditorImageComposition(account: Account, inputImage: UIImage, di
drawingImage = image.transformed(by: CGAffineTransform(translationX: -dimensions.width / 2.0, y: -dimensions.height / 2.0)) drawingImage = image.transformed(by: CGAffineTransform(translationX: -dimensions.width / 2.0, y: -dimensions.height / 2.0))
} }
let entities: [MediaEditorComposerEntity] = values.entities.map { $0.entity }.compactMap { composerEntityForDrawingEntity(account: account, entity: $0, colorSpace: colorSpace) } var entities: [MediaEditorComposerEntity] = []
for entity in values.entities {
entities.append(contentsOf: composerEntitiesForDrawingEntity(account: account, entity: entity.entity, colorSpace: colorSpace))
}
makeEditorImageFrameComposition(inputImage: inputImage, gradientImage: gradientImage, drawingImage: drawingImage, dimensions: dimensions, values: values, entities: entities, time: time, completion: { ciImage in makeEditorImageFrameComposition(inputImage: inputImage, gradientImage: gradientImage, drawingImage: drawingImage, dimensions: dimensions, values: values, entities: entities, time: time, completion: { ciImage in
if let ciImage { if let ciImage {
let context = CIContext(options: [.workingColorSpace : NSNull()]) let context = CIContext(options: [.workingColorSpace : NSNull()])

View File

@@ -12,7 +12,7 @@ import TelegramAnimatedStickerNode
import YuvConversion import YuvConversion
import StickerResources import StickerResources
func composerEntityForDrawingEntity(account: Account, entity: DrawingEntity, colorSpace: CGColorSpace) -> MediaEditorComposerEntity? { func composerEntitiesForDrawingEntity(account: Account, entity: DrawingEntity, colorSpace: CGColorSpace) -> [MediaEditorComposerEntity] {
if let entity = entity as? DrawingStickerEntity { if let entity = entity as? DrawingStickerEntity {
let content: MediaEditorComposerStickerEntity.Content let content: MediaEditorComposerStickerEntity.Content
switch entity.content { switch entity.content {
@@ -21,19 +21,26 @@ func composerEntityForDrawingEntity(account: Account, entity: DrawingEntity, col
case let .image(image): case let .image(image):
content = .image(image) content = .image(image)
} }
return MediaEditorComposerStickerEntity(account: account, content: content, position: entity.position, scale: entity.scale, rotation: entity.rotation, baseSize: entity.baseSize, mirrored: entity.mirrored, colorSpace: colorSpace) return [MediaEditorComposerStickerEntity(account: account, content: content, position: entity.position, scale: entity.scale, rotation: entity.rotation, baseSize: entity.baseSize, mirrored: entity.mirrored, colorSpace: colorSpace)]
} else if let renderImage = entity.renderImage, let image = CIImage(image: renderImage, options: [.colorSpace: colorSpace]) { } else if let renderImage = entity.renderImage, let image = CIImage(image: renderImage, options: [.colorSpace: colorSpace]) {
if let entity = entity as? DrawingBubbleEntity { if let entity = entity as? DrawingBubbleEntity {
return MediaEditorComposerStaticEntity(image: image, position: entity.position, scale: 1.0, rotation: entity.rotation, baseSize: entity.size, mirrored: false) return [MediaEditorComposerStaticEntity(image: image, position: entity.position, scale: 1.0, rotation: entity.rotation, baseSize: entity.size, mirrored: false)]
} else if let entity = entity as? DrawingSimpleShapeEntity { } else if let entity = entity as? DrawingSimpleShapeEntity {
return MediaEditorComposerStaticEntity(image: image, position: entity.position, scale: 1.0, rotation: entity.rotation, baseSize: entity.size, mirrored: false) return [MediaEditorComposerStaticEntity(image: image, position: entity.position, scale: 1.0, rotation: entity.rotation, baseSize: entity.size, mirrored: false)]
} else if let entity = entity as? DrawingVectorEntity { } else if let entity = entity as? DrawingVectorEntity {
return MediaEditorComposerStaticEntity(image: image, position: CGPoint(x: entity.drawingSize.width * 0.5, y: entity.drawingSize.height * 0.5), scale: 1.0, rotation: 0.0, baseSize: entity.drawingSize, mirrored: false) return [MediaEditorComposerStaticEntity(image: image, position: CGPoint(x: entity.drawingSize.width * 0.5, y: entity.drawingSize.height * 0.5), scale: 1.0, rotation: 0.0, baseSize: entity.drawingSize, mirrored: false)]
} else if let entity = entity as? DrawingTextEntity { } else if let entity = entity as? DrawingTextEntity {
return MediaEditorComposerStaticEntity(image: image, position: entity.position, scale: entity.scale, rotation: entity.rotation, baseSize: nil, mirrored: false) var entities: [MediaEditorComposerEntity] = []
entities.append(MediaEditorComposerStaticEntity(image: image, position: entity.position, scale: entity.scale, rotation: entity.rotation, baseSize: nil, mirrored: false))
if let renderSubEntities = entity.renderSubEntities {
for subEntity in renderSubEntities {
entities.append(contentsOf: composerEntitiesForDrawingEntity(account: account, entity: subEntity, colorSpace: colorSpace))
} }
} }
return nil return entities
}
}
return []
} }
private class MediaEditorComposerStaticEntity: MediaEditorComposerEntity { private class MediaEditorComposerStaticEntity: MediaEditorComposerEntity {

View File

@@ -479,7 +479,7 @@ public final class MediaEditorVideoExport {
return false return false
} }
let duration: Double = 3.0 let duration: Double = 5.0
let frameRate: Double = Double(self.configuration.frameRate) let frameRate: Double = Double(self.configuration.frameRate)
var position: CMTime = CMTime(value: 0, timescale: Int32(self.configuration.frameRate)) var position: CMTime = CMTime(value: 0, timescale: Int32(self.configuration.frameRate))
@@ -493,6 +493,9 @@ public final class MediaEditorVideoExport {
return false return false
} }
self.pauseDispatchGroup.wait() self.pauseDispatchGroup.wait()
let progress = (position - .zero).seconds / duration
self.statusValue = .progress(Float(progress))
composer.processImage(inputImage: image, pool: writer.pixelBufferPool, time: position, completion: { pixelBuffer, timestamp in composer.processImage(inputImage: image, pool: writer.pixelBufferPool, time: position, completion: { pixelBuffer, timestamp in
if let pixelBuffer { if let pixelBuffer {
if !writer.appendPixelBuffer(pixelBuffer, at: timestamp) { if !writer.appendPixelBuffer(pixelBuffer, at: timestamp) {