Skip to content

Commit

Permalink
Align actions spec
Browse files Browse the repository at this point in the history
  • Loading branch information
adimiz1 authored May 15, 2022
2 parents d28a6c8 + 49c83d6 commit bbfaf1b
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.cloudinary.transformation.effect

import com.cloudinary.transformation.Action
import com.cloudinary.transformation.Color
import com.cloudinary.transformation.Format
import com.cloudinary.transformation.TransformationComponentBuilder
import com.cloudinary.transformation.layer.source.Source

Expand All @@ -18,7 +19,13 @@ abstract class Effect : Action {
fun makeTransparent(options: (MakeTransparent.Builder.() -> Unit)? = null) =
buildEffect(MakeTransparent.Builder(), options)

fun waveform(options: (Waveform.Builder.() -> Unit)? = null) = buildEffect(Waveform.Builder(), options)
@Deprecated("This function will be removed in the next major version, use VideoEdit.waveform instead", replaceWith = ReplaceWith("VideoEdit.waveform(format)"))
fun waveform(format: Format, options: (Waveform.Builder.() -> Unit)? = null) : Effect {
if (options == null) {
return Waveform(format)
}
return buildEffect(Waveform(format).Builder(), options)
}

fun fadeIn(options: (FadeIn.Builder.() -> Unit)? = null): FadeIn {
val builder = FadeIn.Builder()
Expand Down Expand Up @@ -78,7 +85,7 @@ abstract class Effect : Action {

interface EffectBuilder : TransformationComponentBuilder

private fun <T : EffectBuilder> buildEffect(builder: T, options: (T.() -> Unit)?): Effect {
fun <T : EffectBuilder> buildEffect(builder: T, options: (T.() -> Unit)?): Effect {
options?.let { builder.it() }
return builder.build() as Effect
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -488,23 +488,25 @@ class MakeTransparent private constructor(private val tolerance: Any?, private v
}
}

class Waveform private constructor(private var color: Any?, private val backgroundColor: Color?) : Effect() {
class Waveform internal constructor(private val format: Format, private var color: Any? = null, private val backgroundColor: Color? = null) : Effect() {

override fun toString(): String {
return asComponentString(
"f_$format",
backgroundColor?.let { "b_$backgroundColor" },
color?.let { "co_$color" },
Flag.waveform()
)
}

class Builder : EffectBuilder {
inner class Builder : EffectBuilder {
private var color: Color? = null
private var backgroundColor: Color? = null

fun color(color: Color) = apply { this.color = color }
fun background(color: Color) = apply { this.backgroundColor = color }

override fun build() = Waveform(color, backgroundColor)
override fun build() = Waveform(format, color, backgroundColor)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TextSource internal constructor(
private val text: Any,
private val style: Any,
private val backgroundColor: Color? = null,
private val textColor: Color? = null,
private val textColor: Any? = null,
override val transformation: ITransformableImage<*>? = null
) : Source {

Expand Down Expand Up @@ -46,7 +46,7 @@ class TextSource internal constructor(

private var style: Any? = null
private var backgroundColor: Color? = null
private var textColor: Color? = null
private var textColor: Any? = null
private var transformation: ITransformableImage<*>? = null

fun style(style: TextStyle) = apply { this.style = style }
Expand All @@ -64,6 +64,7 @@ class TextSource internal constructor(

fun backgroundColor(background: Color) = apply { this.backgroundColor = background }
fun textColor(textColor: Color) = apply { this.textColor = textColor }
fun textColor(textColor: String) = apply {this.textColor = textColor }

fun transformation(transformation: ITransformableImage<*>) = apply { this.transformation = transformation }
fun transformation(transformation: ImageTransformation.Builder.() -> Unit) = apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,48 @@ class Fill(

class Builder : BaseBuilder<Builder>() {
private var gravity: Gravity? = null
private var offsetX: Any? = null
private var offsetY: Any? = null
private var x: Any? = null
private var y: Any? = null

fun gravity(gravity: Gravity) = apply {
this.gravity = gravity
}

private fun offsetX(x: Any) = apply {
this.offsetX = x
private fun x(x: Any) = apply {
this.x = x
}
//Only used in conjunction with gravity(Gravity.xyCenter())
fun x(x: String) = x(x as Any)
fun x(x: Expression) = x(x as Any)
fun x(x: Int) = x(x as Any)
fun x(x: Float) = x(x as Any)

fun offsetX(x: String) = offsetX(x as Any)
fun offsetX(x: Expression) = offsetX(x as Any)
fun offsetX(x: Int) = offsetX(x as Any)
fun offsetX(x: Float) = offsetX(x as Any)
@Deprecated("This function will be removed in the next major version, use x instead", replaceWith = ReplaceWith("x(x)"))
fun offsetX(x: String) = x(x as Any)
@Deprecated("This function will be removed in the next major version, use x instead", replaceWith = ReplaceWith("x(x)"))
fun offsetX(x: Expression) = x(x as Any)
@Deprecated("This function will be removed in the next major version, use x instead", replaceWith = ReplaceWith("x(x)"))
fun offsetX(x: Int) = x(x as Any)
@Deprecated("This function will be removed in the next major version, use x instead", replaceWith = ReplaceWith("x(x)"))
fun offsetX(x: Float) = x(x as Any)

private fun offsetY(y: Any) = apply {
this.offsetY = y
private fun y(y: Any) = apply {
this.y = y
}
//Only used in conjunction with gravity(Gravity.xyCenter())
fun y(y: String) = y(y as Any)
fun y(y: Expression) = y(y as Any)
fun y(y: Int) = y(y as Any)
fun y(y: Float) = y(y as Any)

fun offsetY(y: String) = offsetY(y as Any)
fun offsetY(y: Expression) = offsetY(y as Any)
fun offsetY(y: Int) = offsetY(y as Any)
fun offsetY(y: Float) = offsetY(y as Any)
@Deprecated("This function will be removed in the next major version, use y instead", replaceWith = ReplaceWith("y(y)"))
fun offsetY(y: String) = y(y as Any)
@Deprecated("This function will be removed in the next major version, use y instead", replaceWith = ReplaceWith("y(y)"))
fun offsetY(y: Expression) = y(y as Any)
@Deprecated("This function will be removed in the next major version, use y instead", replaceWith = ReplaceWith("y(y)"))
fun offsetY(y: Int) = y(y as Any)
@Deprecated("This function will be removed in the next major version, use y instead", replaceWith = ReplaceWith("y(y)"))
fun offsetY(y: Float) = y(y as Any)

override fun getThis() = this

Expand All @@ -57,8 +75,8 @@ class Fill(
relative,
regionRelative,
gravity,
offsetX,
offsetY
x,
y
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ class VideoCodecLevel private constructor(private val value: String) {
fun vcl51() = vcl51
private val vcl52 = VideoCodecLevel("5.2")
fun vcl52() = vcl52
private val auto = VideoCodecLevel("auto")
fun auto() = auto
}

override fun toString(): String {
Expand All @@ -229,6 +231,8 @@ class VideoCodecProfile private constructor(private val value: String) {
fun main() = main
private val high = VideoCodecProfile("high")
fun high() = high
private val auto = VideoCodecProfile("auto")
fun auto() = auto
}

override fun toString(): String {
Expand All @@ -250,6 +254,12 @@ open class VideoCodec(protected val codec: Any) {
return builder.build()
}

fun h265(options: (H265Codec.Builder.() -> Unit)? = null): H265Codec {
val builder = H265Codec.Builder()
options?.let { builder.it() }
return builder.build()
}

private val h265 = VideoCodec("h265")
fun h265() = h265
private val theora = VideoCodec("theora")
Expand Down Expand Up @@ -283,6 +293,41 @@ class H264Codec private constructor(private val profile: VideoCodecProfile? = nu
}
}

class H265Codec private constructor(private val profile: VideoCodecProfile? = null, private val level: Any? = null, private val bFrames: Boolean = true) :
VideoCodec("h265") {
override fun toString(): String {
var bFramesString: String? = null
if (!bFrames) {
bFramesString = "bframes_no"
}
return codec.joinWithValues(profile, level, bFramesString)
}

class Builder {
private var profile: VideoCodecProfile? = null
private var level: Any? = null
private var bFrames: Boolean = true

fun bFrames(bFrames: Boolean) = apply {
setAutoProfileAndLevel(!bFrames)
this.bFrames = bFrames
}

private fun setAutoProfileAndLevel(setAutoProfileAndLevel: Boolean) {
if (setAutoProfileAndLevel) {
this.profile = VideoCodecProfile.auto()
this.level = VideoCodecLevel.auto()
} else {
this.profile = null
this.level = null
}

}

fun build() = H265Codec(profile, level, bFrames)
}
}

class AnimatedFormat private constructor(private val value: String) {
companion object {
private val auto = AnimatedFormat("auto")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.cloudinary.transformation.videoedit

import com.cloudinary.transformation.Action
import com.cloudinary.transformation.Format
import com.cloudinary.transformation.effect.Effect
import com.cloudinary.transformation.effect.Waveform
import com.cloudinary.transformation.effect.buildEffect
import com.cloudinary.transformation.layer.source.FetchSource
import com.cloudinary.transformation.layer.source.ImageSource
import com.cloudinary.transformation.layer.source.Source
Expand Down Expand Up @@ -39,5 +43,13 @@ abstract class VideoEdit : Action {
options?.let { builder.it() }
return builder.build()
}

fun waveform(format: Format, options: (Waveform.Builder.() -> Unit)? = null) : Effect {
if (options == null) {
return Waveform(format)
}
return buildEffect(Waveform(format).Builder(), options)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.cloudinary.transformation
import com.cloudinary.cldAssert
import com.cloudinary.transformation.Region.Companion.custom
import com.cloudinary.transformation.effect.*
import com.cloudinary.transformation.expression.Expression
import com.cloudinary.transformation.layer.source.Source
import com.cloudinary.transformation.resize.Resize
import org.junit.Test
Expand Down Expand Up @@ -152,10 +153,10 @@ class EffectTest {

@Test
fun testWaveform() {
cldAssert("fl_waveform", Effect.waveform())
cldAssert("f_jpg,fl_waveform", Effect.waveform(Format.jpg()))

cldAssert(
"b_white,co_black,fl_waveform", Effect.waveform {
"b_white,co_black,f_png,fl_waveform", Effect.waveform(Format.png()) {
color(Color.BLACK)
background(Color.WHITE)
}
Expand Down Expand Up @@ -241,6 +242,7 @@ class EffectTest {
@Test
fun testBlur() {
cldAssert("e_blur", Effect.blur())
cldAssert("e_blur:100_div_2", Effect.blur { strength(Expression("100 / 2")) })
cldAssert("e_blur:300", Effect.blur {
strength(300)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.cloudinary.transformation

import com.cloudinary.cldAssert
import com.cloudinary.transformation.background.Background
import com.cloudinary.transformation.expression.Expression
import com.cloudinary.transformation.gravity.Gravity
import com.cloudinary.transformation.resize.AspectRatio
import com.cloudinary.transformation.resize.Resize
Expand Down Expand Up @@ -53,6 +54,24 @@ class ResizeTest {
width(100)
liquidRescaling(true)
})
cldAssert("ar_w_mul_2,c_scale", Resize.scale {
aspectRatio(Expression("width * 2"))
})
cldAssert("c_scale,h_w_mul_2", Resize.scale {
height(Expression("width * 2"))
})
cldAssert("c_scale,w_w_mul_2", Resize.scale {
width(Expression("width * 2"))
})
cldAssert("c_crop,x_w_mul_2", Resize.crop {
x(Expression("width * 2"))
})
cldAssert("c_crop,y_w_mul_2", Resize.crop {
y(Expression("width * 2"))
})
cldAssert("c_crop,z_w_mul_2", Resize.crop {
zoom(Expression("width * 2"))
})
}

@Test
Expand Down Expand Up @@ -144,6 +163,14 @@ class ResizeTest {

@Test
fun testFill() {
cldAssert("c_fill,g_xy_center,x_100,y_100", Resize.fill {
gravity(Gravity.xyCenter())
offsetX(100)
offsetY(100)})
cldAssert("c_fill,g_xy_center,x_100,y_100", Resize.fill {
gravity(Gravity.xyCenter())
x(100)
y(100)})
cldAssert("c_fill,w_100", Resize.fill { width(100) })
cldAssert("c_fill,w_1.0", Resize.fill { width(1f) })
cldAssert("c_fill,h_100,w_100", Resize.fill {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class TranscodeTest {
@Test
fun testAudioFrequency() {
cldAssert("af_8000", Transcode.audioFrequency(AudioFrequency.freq8000()))
cldAssert("af_iaf", Transcode.audioFrequency(AudioFrequency.original()))
}

@Test
Expand Down Expand Up @@ -47,6 +48,14 @@ class TranscodeTest {
profile(VideoCodecProfile.high())
level(VideoCodecLevel.vcl31())
}))

cldAssert("vc_h265", videoCodec(VideoCodec.h265()))
}

@Test
fun testDisableBFrames() {
cldAssert("vc_h265:auto:auto:bframes_no", videoCodec(VideoCodec.h265 { bFrames(false) }))
cldAssert("vc_h265", videoCodec(VideoCodec.h265 { bFrames(true) }))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ class VideoEditTest {
cldAssert("eo_auto", trim { endOffset("auto") })
cldAssert("eo_2.63", trim { endOffset(2.63f) })
cldAssert("eo_35.0p", trim { endOffset("35.0p") })

cldAssert("so_du_div_3", VideoEdit.trim {
startOffset(Expression("duration / 3"))
})
cldAssert("eo_du_div_3", VideoEdit.trim {
endOffset(Expression("duration / 3"))
})
cldAssert("du_2.63", trim { duration(2.63f) })
cldAssert("du_35.0p", trim { duration("35.0p") })

Expand Down Expand Up @@ -95,4 +100,16 @@ class VideoEditTest {
cldAssert("e_volume:10db", VideoEdit.volume(Volume.byDecibels(10)))
cldAssert("e_volume:mute", VideoEdit.volume(Volume.mute()))
}
}

@Test
fun testWaveform() {
cldAssert("f_jpg,fl_waveform", VideoEdit.waveform(Format.jpg()))

cldAssert(
"b_white,co_black,f_png,fl_waveform", VideoEdit.waveform(Format.png()) {
color(Color.BLACK)
background(Color.WHITE)
}
)
}
}
Loading

0 comments on commit bbfaf1b

Please sign in to comment.