mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-26 17:01:35 +01:00
cgen: refactor patch_pixconv.go
This commit is contained in:
1 parent
311ea1a1f4
commit
aec9003ea0
1 file changed
+72
-88
@@ -38,6 +38,8 @@ func insertBasePixConvSubmoduleYcckC(buf *buffer) error {
|
||||
}
|
||||
stripCFuncBangComments(buf, prefix)
|
||||
|
||||
// ----
|
||||
|
||||
const dstIterEtc = "" +
|
||||
"size_t dst_stride = dst->private_impl.planes[0].stride;\n" +
|
||||
"uint8_t* dst_iter =\n" +
|
||||
@@ -56,88 +58,53 @@ func insertBasePixConvSubmoduleYcckC(buf *buffer) error {
|
||||
"src_ptr1 += stride1;\n" +
|
||||
"src_ptr2 += stride2;\n"
|
||||
|
||||
// ----
|
||||
generalBoxFilterPatches := []struct {
|
||||
newFuncNameLine string
|
||||
patchMap map[string]string
|
||||
}{{
|
||||
|
||||
if err := patchCFunc(buf, generalBoxFilter, [][2]string{{
|
||||
"wuffs_base__pixel_swizzler__swizzle_ycc__general__box_filter(\n",
|
||||
"wuffs_base__pixel_swizzler__swizzle_ycc__bgrx__box_filter(\n",
|
||||
map[string]string{
|
||||
"// ¡ dst_iter = etc\n": dstIterEtc,
|
||||
"// ¡ BEGIN set_color_u32_at\n": strings.Replace(setColorU32At, "_FLAVOR", "", 1),
|
||||
},
|
||||
}, {
|
||||
" // ¡ dst_iter = etc\n",
|
||||
dstIterEtc,
|
||||
}, {
|
||||
" // ¡ BEGIN set_color_u32_at\n",
|
||||
strings.Replace(setColorU32At, "_FLAVOR", "", 1),
|
||||
}}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// ----
|
||||
|
||||
if err := patchCFunc(buf, generalBoxFilter, [][2]string{{
|
||||
"wuffs_base__pixel_swizzler__swizzle_ycc__general__box_filter(\n",
|
||||
"wuffs_base__pixel_swizzler__swizzle_ycc__rgbx__box_filter(\n",
|
||||
map[string]string{
|
||||
"// ¡ dst_iter = etc\n": dstIterEtc,
|
||||
"// ¡ BEGIN set_color_u32_at\n": strings.Replace(setColorU32At, "_FLAVOR", "_abgr", 1),
|
||||
},
|
||||
}, {
|
||||
" // ¡ dst_iter = etc\n",
|
||||
dstIterEtc,
|
||||
}, {
|
||||
" // ¡ BEGIN set_color_u32_at\n",
|
||||
strings.Replace(setColorU32At, "_FLAVOR", "_abgr", 1),
|
||||
}}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// ----
|
||||
|
||||
if err := patchCFunc(buf, generalBoxFilter, [][2]string{{
|
||||
"wuffs_base__pixel_swizzler__swizzle_ycc__general__box_filter(\n",
|
||||
"wuffs_base__pixel_swizzler__swizzle_ycc__bgrx__hv11(\n",
|
||||
map[string]string{
|
||||
"// ¡ dst_iter = etc\n": dstIterEtc,
|
||||
"// ¡ BEGIN set_color_u32_at\n": strings.Replace(setColorU32At, "_FLAVOR", "", 1),
|
||||
"// ¡ BEGIN declare iy\n": "",
|
||||
"// ¡ BEGIN declare ix\n": "",
|
||||
"// ¡ BEGIN use ix\n": useIxHv11,
|
||||
"// ¡ BEGIN use iy\n": useIyHv11,
|
||||
},
|
||||
}, {
|
||||
" // ¡ dst_iter = etc\n",
|
||||
dstIterEtc,
|
||||
}, {
|
||||
" // ¡ BEGIN set_color_u32_at\n",
|
||||
strings.Replace(setColorU32At, "_FLAVOR", "", 1),
|
||||
}, {
|
||||
" // ¡ BEGIN declare iy\n",
|
||||
"",
|
||||
}, {
|
||||
" // ¡ BEGIN declare ix\n",
|
||||
"",
|
||||
}, {
|
||||
" // ¡ BEGIN use ix\n",
|
||||
useIxHv11,
|
||||
}, {
|
||||
" // ¡ BEGIN use iy\n",
|
||||
useIyHv11,
|
||||
}}); err != nil {
|
||||
|
||||
"wuffs_base__pixel_swizzler__swizzle_ycc__rgbx__hv11(\n",
|
||||
map[string]string{
|
||||
"// ¡ dst_iter = etc\n": dstIterEtc,
|
||||
"// ¡ BEGIN set_color_u32_at\n": strings.Replace(setColorU32At, "_FLAVOR", "_abgr", 1),
|
||||
"// ¡ BEGIN declare iy\n": "",
|
||||
"// ¡ BEGIN declare ix\n": "",
|
||||
"// ¡ BEGIN use ix\n": useIxHv11,
|
||||
"// ¡ BEGIN use iy\n": useIyHv11,
|
||||
},
|
||||
}}
|
||||
|
||||
for _, p := range generalBoxFilterPatches {
|
||||
const oldFuncNameLine = "wuffs_base__pixel_swizzler__swizzle_ycc__general__box_filter(\n"
|
||||
p.patchMap[oldFuncNameLine] = p.newFuncNameLine
|
||||
if err := patchCFunc(buf, generalBoxFilter, oldFuncNameLine, p.patchMap); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// ----
|
||||
|
||||
if err := patchCFunc(buf, generalBoxFilter, [][2]string{{
|
||||
"wuffs_base__pixel_swizzler__swizzle_ycc__general__box_filter(\n",
|
||||
"wuffs_base__pixel_swizzler__swizzle_ycc__rgbx__hv11(\n",
|
||||
}, {
|
||||
" // ¡ dst_iter = etc\n",
|
||||
dstIterEtc,
|
||||
}, {
|
||||
" // ¡ BEGIN set_color_u32_at\n",
|
||||
strings.Replace(setColorU32At, "_FLAVOR", "_abgr", 1),
|
||||
}, {
|
||||
" // ¡ BEGIN declare iy\n",
|
||||
"",
|
||||
}, {
|
||||
" // ¡ BEGIN declare ix\n",
|
||||
"",
|
||||
}, {
|
||||
" // ¡ BEGIN use ix\n",
|
||||
useIxHv11,
|
||||
}, {
|
||||
" // ¡ BEGIN use iy\n",
|
||||
useIyHv11,
|
||||
}}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// ----
|
||||
@@ -195,13 +162,11 @@ func stripCFuncBangComments(buf *buffer, s string) {
|
||||
buf.writes(s)
|
||||
}
|
||||
|
||||
func patchCFunc(buf *buffer, src string, patches [][2]string) error {
|
||||
srcFuncNameParenNewline := patches[0][0]
|
||||
func patchCFunc(buf *buffer, src string, oldFuncNameLine string, patchMap map[string]string) error {
|
||||
buf.writes("// Specializes ")
|
||||
buf.writes(srcFuncNameParenNewline[:len(srcFuncNameParenNewline)-2])
|
||||
buf.writes(oldFuncNameLine[:len(oldFuncNameLine)-2])
|
||||
buf.writes(".\n")
|
||||
|
||||
loop:
|
||||
for src != "" {
|
||||
line := ""
|
||||
if i := strings.IndexByte(src, '\n'); i < 0 {
|
||||
@@ -210,12 +175,26 @@ loop:
|
||||
line, src = src[:i+1], src[i+1:]
|
||||
}
|
||||
|
||||
for _, patch := range patches {
|
||||
if line != patch[0] {
|
||||
key, indent := line, ""
|
||||
for i := 0; ; i++ {
|
||||
if i == len(line) {
|
||||
key, indent = "", line
|
||||
break
|
||||
} else if line[i] != ' ' {
|
||||
key, indent = line[i:], line[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
val, ok := patchMap[key]
|
||||
if !ok {
|
||||
if !isCFuncComment(line) {
|
||||
buf.writes(line)
|
||||
}
|
||||
continue
|
||||
}
|
||||
indentCFunc(buf, indent, val)
|
||||
if !strings.Contains(key, "¡ BEGIN") {
|
||||
continue
|
||||
} else if !strings.Contains(line, "¡ BEGIN") {
|
||||
buf.writes(patch[1])
|
||||
continue loop
|
||||
}
|
||||
|
||||
// Extend forwards past (including) the next '\n'.
|
||||
@@ -229,14 +208,6 @@ loop:
|
||||
j++
|
||||
}
|
||||
src = src[j:]
|
||||
|
||||
buf.writes(patch[1])
|
||||
continue loop
|
||||
}
|
||||
|
||||
if !isCFuncComment(line) {
|
||||
buf.writes(line)
|
||||
}
|
||||
}
|
||||
buf.writes("\n")
|
||||
return nil
|
||||
@@ -247,3 +218,16 @@ func isCFuncComment(s string) bool {
|
||||
}
|
||||
return (len(s) >= 2) && (s[0] == '/') && (s[1] == '/')
|
||||
}
|
||||
|
||||
func indentCFunc(buf *buffer, indent string, s string) {
|
||||
for s != "" {
|
||||
line := ""
|
||||
if i := strings.IndexByte(s, '\n'); i < 0 {
|
||||
line, s = s, ""
|
||||
} else {
|
||||
line, s = s[:i+1], s[i+1:]
|
||||
}
|
||||
buf.writes(indent)
|
||||
buf.writes(line)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user