Skip to content

Commit

Permalink
feat(plugin-image): fixPng support correct magic number
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon406 committed Apr 21, 2023
1 parent 24e9ae0 commit 91726c2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion plugin-image/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = "me.leon.toolsfx"
version = "1.3.0"
version = "1.3.1"

plugins {
`java-library`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import me.leon.toolsfx.plugin.ext.*
import tornadofx.*

class ImageProcessView : PluginFragment("ImageProcessView") {
override val version = "v1.2.1"
override val date: String = "2022-12-29"
override val version = "v1.3.1"
override val date: String = "2023-04-21"
override val author = "Leon406"
override val description = "图片模块"
private var taInput: TextArea by singleAssign()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ val EXTENSION_IMAGE = arrayOf("jpg", "png", "gif", "bmp")
/** ihdr width height crc 12-29 ihdr 16-20 width 20-24 height 29-33 crc */
private fun ByteArray.parsePngIhdr() = arrayOf(copyOfRange(12, 29), copyOfRange(29, 33))

private val PNG_MAGIC = byteArrayOf(137.toByte(), 80, 78, 71)

fun ByteArray.fixPng(maxPixel: Int = 2048, minPixel: Int = 5): ByteArray {
val bytes = this

for (i in 0..3) {
if (PNG_MAGIC[i] != bytes[i]) {
bytes[i] = PNG_MAGIC[i]
}
}

val (ihdr, crc) = bytes.parsePngIhdr()

if (crc.toHex() == ihdr.crc32()) {
Expand Down

0 comments on commit 91726c2

Please sign in to comment.