diff --git a/internal/cgen/base/pixconv-submodule-ycck.c b/internal/cgen/base/pixconv-submodule-ycck.c index 4be4671f..a9a51731 100644 --- a/internal/cgen/base/pixconv-submodule-ycck.c +++ b/internal/cgen/base/pixconv-submodule-ycck.c @@ -585,15 +585,19 @@ wuffs_base__pixel_swizzler__swizzle_ycc__general__box_filter( const uint8_t* src_iter1 = src_ptr1; const uint8_t* src_iter2 = src_ptr2; + // ¡ dst_iter = etc + uint32_t ix0 = 0u; uint32_t ix1 = 0u; uint32_t ix2 = 0u; uint32_t x = 0u; while (true) { + // ¡ BEGIN set_color_u32_at wuffs_base__pixel_buffer__set_color_u32_at( dst, x, y, wuffs_base__color_ycc__as__color_u32(*src_iter0, *src_iter1, *src_iter2)); + // ¡ END set_color_u32_at if ((x + 1u) == width) { break; @@ -640,193 +644,7 @@ wuffs_base__pixel_swizzler__swizzle_ycc__general__box_filter( // -------- -// Specializes wuffs_base__pixel_swizzler__swizzle_ycc__general__box_filter. -static void // -wuffs_base__pixel_swizzler__swizzle_ycc__bgrx__box_filter( - wuffs_base__pixel_buffer* dst, - uint32_t width, - uint32_t height, - const uint8_t* src_ptr0, - const uint8_t* src_ptr1, - const uint8_t* src_ptr2, - uint32_t stride0, - uint32_t stride1, - uint32_t stride2, - uint32_t inv_h0, - uint32_t inv_h1, - uint32_t inv_h2, - uint32_t inv_v0, - uint32_t inv_v1, - uint32_t inv_v2, - uint32_t half_width_for_2to1, - uint32_t half_height_for_2to1, - uint8_t* scratch_buffer_2k_ptr) { - uint32_t h0_out_of_12 = 12u / inv_h0; - uint32_t h1_out_of_12 = 12u / inv_h1; - uint32_t h2_out_of_12 = 12u / inv_h2; - uint32_t v0_out_of_12 = 12u / inv_v0; - uint32_t v1_out_of_12 = 12u / inv_v1; - uint32_t v2_out_of_12 = 12u / inv_v2; - - uint32_t iy0 = 0u; - uint32_t iy1 = 0u; - uint32_t iy2 = 0u; - uint32_t y = 0u; - while (true) { - const uint8_t* src_iter0 = src_ptr0; - const uint8_t* src_iter1 = src_ptr1; - const uint8_t* src_iter2 = src_ptr2; - - size_t dst_stride = dst->private_impl.planes[0].stride; - uint8_t* dst_iter = - dst->private_impl.planes[0].ptr + (dst_stride * ((size_t)y)); - - uint32_t ix0 = 0u; - uint32_t ix1 = 0u; - uint32_t ix2 = 0u; - uint32_t x = 0u; - while (true) { - wuffs_base__poke_u32le__no_bounds_check( // - dst_iter, wuffs_base__color_ycc__as__color_u32( // - *src_iter0, *src_iter1, *src_iter2)); - dst_iter += 4; - - if ((x + 1u) == width) { - break; - } - x = x + 1u; - ix0 += h0_out_of_12; - if (ix0 >= 12u) { - ix0 = 0u; - src_iter0++; - } - ix1 += h1_out_of_12; - if (ix1 >= 12u) { - ix1 = 0u; - src_iter1++; - } - ix2 += h2_out_of_12; - if (ix2 >= 12u) { - ix2 = 0u; - src_iter2++; - } - } - - if ((y + 1u) == height) { - break; - } - y = y + 1u; - iy0 += v0_out_of_12; - if (iy0 >= 12u) { - iy0 = 0u; - src_ptr0 += stride0; - } - iy1 += v1_out_of_12; - if (iy1 >= 12u) { - iy1 = 0u; - src_ptr1 += stride1; - } - iy2 += v2_out_of_12; - if (iy2 >= 12u) { - iy2 = 0u; - src_ptr2 += stride2; - } - } -} - -// Specializes wuffs_base__pixel_swizzler__swizzle_ycc__general__box_filter. -static void // -wuffs_base__pixel_swizzler__swizzle_ycc__rgbx__box_filter( - wuffs_base__pixel_buffer* dst, - uint32_t width, - uint32_t height, - const uint8_t* src_ptr0, - const uint8_t* src_ptr1, - const uint8_t* src_ptr2, - uint32_t stride0, - uint32_t stride1, - uint32_t stride2, - uint32_t inv_h0, - uint32_t inv_h1, - uint32_t inv_h2, - uint32_t inv_v0, - uint32_t inv_v1, - uint32_t inv_v2, - uint32_t half_width_for_2to1, - uint32_t half_height_for_2to1, - uint8_t* scratch_buffer_2k_ptr) { - uint32_t h0_out_of_12 = 12u / inv_h0; - uint32_t h1_out_of_12 = 12u / inv_h1; - uint32_t h2_out_of_12 = 12u / inv_h2; - uint32_t v0_out_of_12 = 12u / inv_v0; - uint32_t v1_out_of_12 = 12u / inv_v1; - uint32_t v2_out_of_12 = 12u / inv_v2; - - uint32_t iy0 = 0u; - uint32_t iy1 = 0u; - uint32_t iy2 = 0u; - uint32_t y = 0u; - while (true) { - const uint8_t* src_iter0 = src_ptr0; - const uint8_t* src_iter1 = src_ptr1; - const uint8_t* src_iter2 = src_ptr2; - - size_t dst_stride = dst->private_impl.planes[0].stride; - uint8_t* dst_iter = - dst->private_impl.planes[0].ptr + (dst_stride * ((size_t)y)); - - uint32_t ix0 = 0u; - uint32_t ix1 = 0u; - uint32_t ix2 = 0u; - uint32_t x = 0u; - while (true) { - wuffs_base__poke_u32le__no_bounds_check( // - dst_iter, wuffs_base__color_ycc__as__color_u32_abgr( // - *src_iter0, *src_iter1, *src_iter2)); - dst_iter += 4; - - if ((x + 1u) == width) { - break; - } - x = x + 1u; - ix0 += h0_out_of_12; - if (ix0 >= 12u) { - ix0 = 0u; - src_iter0++; - } - ix1 += h1_out_of_12; - if (ix1 >= 12u) { - ix1 = 0u; - src_iter1++; - } - ix2 += h2_out_of_12; - if (ix2 >= 12u) { - ix2 = 0u; - src_iter2++; - } - } - - if ((y + 1u) == height) { - break; - } - y = y + 1u; - iy0 += v0_out_of_12; - if (iy0 >= 12u) { - iy0 = 0u; - src_ptr0 += stride0; - } - iy1 += v1_out_of_12; - if (iy1 >= 12u) { - iy1 = 0u; - src_ptr1 += stride1; - } - iy2 += v2_out_of_12; - if (iy2 >= 12u) { - iy2 = 0u; - src_ptr2 += stride2; - } - } -} +// ¡ INSERT patch_pixconv // -------- diff --git a/internal/cgen/cgen.go b/internal/cgen/cgen.go index 8bb555fb..9ac2865c 100644 --- a/internal/cgen/cgen.go +++ b/internal/cgen/cgen.go @@ -124,7 +124,7 @@ func Do(args []string) error { return nil, fmt.Errorf("base package shouldn't have any .wuffs files") } buf := make(buffer, 0, 128*1024) - if err := expandBangBangInsert(&buf, embedBaseAllImplC.Trim(), map[string]func(*buffer) error{ + if err := expandBangInsert(&buf, embedBaseAllImplC.Trim(), map[string]func(*buffer) error{ "// ¡ INSERT InterfaceDeclarations.\n": insertInterfaceDeclarations, "// ¡ INSERT InterfaceDefinitions.\n": insertInterfaceDefinitions, "// ¡ INSERT base/all-private.h.\n": insertBaseAllPrivateH, @@ -238,7 +238,7 @@ func (b *buffer) writeb(x byte) { *b = append(*b, x) func (b *buffer) writes(s string) { *b = append(*b, s...) } func (b *buffer) writex(s []byte) { *b = append(*b, s...) } -func expandBangBangInsert(b *buffer, s string, m map[string]func(*buffer) error) error { +func expandBangInsert(b *buffer, s string, m map[string]func(*buffer) error) error { for { remaining := "" if i := strings.IndexByte(s, '\n'); i >= 0 { @@ -290,7 +290,7 @@ func insertBaseAllPrivateH(buf *buffer) error { } func insertBaseAllPublicH(buf *buffer) error { - if err := expandBangBangInsert(buf, embedBaseFundamentalPublicH.Trim(), map[string]func(*buffer) error{ + if err := expandBangInsert(buf, embedBaseFundamentalPublicH.Trim(), map[string]func(*buffer) error{ "// ¡ INSERT FourCCs.\n": func(b *buffer) error { for i, z := range builtin.FourCCs { if i != 0 { @@ -387,11 +387,6 @@ func insertBasePixConvSubmoduleRegularC(buf *buffer) error { return nil } -func insertBasePixConvSubmoduleYcckC(buf *buffer) error { - buf.writes(embedBasePixConvSubmoduleYcckC.Trim()) - return nil -} - func insertBaseUTF8SubmoduleC(buf *buffer) error { buf.writes(embedBaseUTF8SubmoduleC.Trim()) return nil diff --git a/internal/cgen/patch_pixconv.go b/internal/cgen/patch_pixconv.go new file mode 100644 index 00000000..aacc8324 --- /dev/null +++ b/internal/cgen/patch_pixconv.go @@ -0,0 +1,187 @@ +// Copyright 2023 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cgen + +import ( + "fmt" + "strings" +) + +func insertBasePixConvSubmoduleYcckC(buf *buffer) error { + src := embedBasePixConvSubmoduleYcckC.Trim() + + generalBoxFilter, err := extractCFunc(src, ""+ + "static void //\n"+ + "wuffs_base__pixel_swizzler__swizzle_ycc__general__box_filter(\n") + if err != nil { + return err + } + + prefix, suffix := "", "" + const insertPatchPixconv = "// ¡ INSERT patch_pixconv\n\n" + if i := strings.Index(src, insertPatchPixconv); i < 0 { + return fmt.Errorf("internal error: could not find %q", insertPatchPixconv) + } else { + prefix, suffix = src[:i], src[i+len(insertPatchPixconv):] + } + stripCFuncBangComments(buf, prefix) + + const dstIterEtc = "" + + " size_t dst_stride = dst->private_impl.planes[0].stride;\n" + + " uint8_t* dst_iter =\n" + + " dst->private_impl.planes[0].ptr + (dst_stride * ((size_t)y));\n" + const setColorU32At = "" + + " wuffs_base__poke_u32le__no_bounds_check(\n" + + " dst_iter, wuffs_base__color_ycc__as__color_u32_FLAVOR(\n" + + " *src_iter0, *src_iter1, *src_iter2));\n" + + " dst_iter += 4;\n" + + // ---- + + 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", + }, { + " // ¡ 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", + }, { + " // ¡ dst_iter = etc\n", + dstIterEtc, + }, { + " // ¡ BEGIN set_color_u32_at\n", + strings.Replace(setColorU32At, "_FLAVOR", "_abgr", 1), + }}); err != nil { + return err + } + + // ---- + + stripCFuncBangComments(buf, suffix) + return nil +} + +func extractCFunc(src string, start string) (string, error) { + if i := strings.Index(src, start); i < 0 { + goto fail + } else { + src = src[i:] + } + + if i := strings.Index(src, "\n}\n"); i < 0 { + goto fail + } else { + return src[:i+3], nil + } + +fail: + return "", fmt.Errorf("internal error: could not extractCFunc %q", start) +} + +func stripCFuncBangComments(buf *buffer, s string) { + for { + const slashEtc = "// ¡" + i := strings.Index(s, slashEtc) + if i < 0 { + break + } + + // Extend forwards past (including) the next '\n'. + j := i + len(slashEtc) + for ; (j < len(s)) && (s[j] != '\n'); j++ { + } + if j < len(s) { + j++ + } + + // Extend backwards up to (but excluding) the previous '\n'. + for ; (i > 0) && (s[i-1] != '\n'); i-- { + } + + // Collapse multiple '\n's. + if (i >= 2) && (s[i-2] == '\n') { + for ; (j < len(s)) && (s[j] == '\n'); j++ { + } + } + + buf.writes(s[:i]) + s = s[j:] + } + buf.writes(s) +} + +func patchCFunc(buf *buffer, src string, patches [][2]string) error { + srcFuncNameParenNewline := patches[0][0] + buf.writes("// Specializes ") + buf.writes(srcFuncNameParenNewline[:len(srcFuncNameParenNewline)-2]) + buf.writes(".\n") + +loop: + for src != "" { + line := "" + if i := strings.IndexByte(src, '\n'); i < 0 { + return fmt.Errorf("internal error: patchCFunc failed to find '\n'") + } else { + line, src = src[:i+1], src[i+1:] + } + + for _, patch := range patches { + if line != patch[0] { + continue + } else if !strings.Contains(line, "¡ BEGIN") { + buf.writes(patch[1]) + continue loop + } + + // Extend forwards past (including) the next '\n'. + j := strings.Index(src, "¡ END ") + if j < 0 { + return fmt.Errorf("internal error: patchCFunc failed to find \"¡ END\"") + } + for ; (j < len(src)) && (src[j] != '\n'); j++ { + } + if j < len(src) { + j++ + } + src = src[j:] + + buf.writes(patch[1]) + continue loop + } + + if !isCFuncComment(line) { + buf.writes(line) + } + } + buf.writes("\n") + return nil +} + +func isCFuncComment(s string) bool { + for ; (len(s) > 0) && (s[0] <= ' '); s = s[1:] { + } + return (len(s) >= 2) && (s[0] == '/') && (s[1] == '/') +} diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 9d14479b..6fe49fab 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -23406,8 +23406,8 @@ wuffs_base__pixel_swizzler__swizzle_ycc__bgrx__box_filter( uint32_t ix2 = 0u; uint32_t x = 0u; while (true) { - wuffs_base__poke_u32le__no_bounds_check( // - dst_iter, wuffs_base__color_ycc__as__color_u32( // + wuffs_base__poke_u32le__no_bounds_check( + dst_iter, wuffs_base__color_ycc__as__color_u32( *src_iter0, *src_iter1, *src_iter2)); dst_iter += 4; @@ -23500,8 +23500,8 @@ wuffs_base__pixel_swizzler__swizzle_ycc__rgbx__box_filter( uint32_t ix2 = 0u; uint32_t x = 0u; while (true) { - wuffs_base__poke_u32le__no_bounds_check( // - dst_iter, wuffs_base__color_ycc__as__color_u32_abgr( // + wuffs_base__poke_u32le__no_bounds_check( + dst_iter, wuffs_base__color_ycc__as__color_u32_abgr( *src_iter0, *src_iter1, *src_iter2)); dst_iter += 4;