Commit Graph

84 Commits

Author SHA1 Message Date
Anuj Verma aed19e1524 * src/sdf/ftbsdf.c (compute_gradient): Use root(2) for the filter.
Also, use the computed gradient to approximate
the edge distance from a pixel's position according
to the Gustavson's algorithm.
2020-08-02 16:33:21 +05:30
Anuj Verma fbbec7c64d [sdf -> bsdf] Added function to approximate edge distance.
* src/sdf/ftbsdf.c (bsdf_approximate_edge): The function
  uses the Gustavson's algorithm to approximate the edge
  from pixel values.

* src/sdf/ftbsdf.c (compute_gradient): The function uses
  Sobel's operator to compute the gradient at a pixel.
  The is used to detect edge direction.
2020-08-02 16:33:21 +05:30
Anuj Verma ffb6890089 * src/sdf/ftbsdf.c (_pass): Use function for repetitive task.
Use function `compare_neighbor' for comparing the
neighbors. Makes it easier to tweak the algorithm
and looks better.

* src/sdf/ftbsdf.c (compare_neighbor): Added function
  to compare the neighbors and assign values if the
  new distance is the shortest.
2020-08-02 16:33:21 +05:30
Anuj Verma 419633c374 * src/sdf/ftbsdf.c (bsdf_is_edge): Use macros to make it look cleaner.
Use `CHECK_NEIGHBOR' macro to check neighbors while
finding edges. Make the code more readable and look
cleaner.
2020-08-02 16:33:21 +05:30
Anuj Verma 262e9649f3 [sdf -> bsdf] Fix edge detection bug.
* src/sdf/ftbsdf.c (bsdf_is_edge): [BUG] Check all
  neighbors including the diagonal neighbors to
  properly determine the edge.
2020-08-02 16:33:21 +05:30
Anuj Verma f057095bef [sdf -> bsdf] Added edge detection algorithm.
Added edge detection algorithm. It works by checking
the neighboring pixels and if any neighbor is not
filled (i.e. belongs to background) we mark the
pixel as edge by setting it's distance to 0.

* src/sdf/ftbsdf.c (bsdf_is_edge): Added function to
  detect if the pixel is an edge.
2020-08-02 16:33:21 +05:30
Anuj Verma 8564d5caf2 [sdf -> bsdf] Added the second pass of the '8SED'.
Added the second pass of the 8SED algorithm. The second pass
transverse the bitmap from bottom to top and for each row
it sweeps forward and backward assigning distances to the
grid points.

* src/sdf/ftbsdf.c (second_pas): Added function to do the
  second pass of the 8SED algorithm on the bitmap.
2020-08-02 16:33:21 +05:30
Anuj Verma 6214e58893 [sdf -> bsdf] Optimize the first pass of the 8SED.
* src/sdf/ftbsdf.c (first_pass): Optimize the first
  pass by first approximating the neighbor's distance
  by adding an offset. The offset will be max root(2)
  because the maximum be add to a vector is (1, 1).
  By approximating we can reduce the number of
  `FT_Vector_Length' calls and thus make the process
  faster.
2020-08-02 16:33:21 +05:30
Anuj Verma 6ccbf0ecf6 [sdf] Fix GNU Make build.
* src/sdf/rules.mk (SDF_DRV_H): Manually add source files
  as there is a `ftbsdf.c' file without any `ftbsdf.h'.

* src/sdf/ftbsdf.c: Include `fttrigon.h' for `multi' builds.
2020-08-02 16:33:21 +05:30
Anuj Verma 470131db91 [sdf -> bsdf] Added first pass of the '8SED' algorithm.
Added the first pass of the 8SED algorithm. The first pass
transverse the bitmap from top to bottom and for each row
it sweeps forward and backward assigning distances to the
grid points.

* src/sdf/ftbsdf.c (ED): Added additional parameter `sign'.

* src/sdf/ftbsdf.c (edt8): Added function to convert bitmap
  to SDF using the 8SED algorithm.

* src/sdf/ftbsdf.c (first_pass): Added function to do the
  first pass of the 8SED algorithm on the bitmap.

* src/sdf/ftbsdf.c (finalize_sdf): Added function to assign
  the final SDF data to the target bitmap.

* src/sdf/ftbsdf.c (*): Various minor or experimental changes.

* src/sdf/ftsdfrend.c (ft_bsdf_render): Fix spacing.
2020-08-02 16:33:20 +05:30
Anuj Verma 59d340878a * src/sdf/ftbsdf.c (*): Rename `SDF_TRaster' => `BSDF_TRaster'.
`SDF_TRaster' is for the `sdf' renderer.
2020-08-02 16:33:20 +05:30
Anuj Verma ea5114acfd * src/sdf/ftbsdf.c: Typo. `FT_16D16' is 16.16 representation. 2020-08-02 16:33:20 +05:30
Anuj Verma 13180a3cd4 [sdf -> bsdf renderer] Hold a refrence to `FT_Memory'.
Due to the way the 8SED algorithm works we need to keep
a track of distances and nearest point of all the pixels/
grid points.

* src/sdf/ftbsdf.c (BSDF_TRaster): Added struct to
  hold the `FT_Memory' reference, to be used to allocate
  memory.

* src/sdf/ftbsdf.c (ED, BSDF_Worker): Added few more
  essential struct. `ED' contains infomation about the
  nearest point. `BSDF_Worker' is used to combine all
  the essentail parameters to functions.

* src/sdf/ftbsdf.c (bsdf_copy_source_to_target => 
  bsdf_init_distance_map): Renamed.

* src/sdf/ftbsdf.c (*): Various minor changes.

* src/sdf/sdf.c: Change the order of source include
  because `ftsdf.c' change some internal freetype
  defines which are required in `ftbsdf.c'.
2020-08-02 16:33:20 +05:30
Anuj Verma 99a4759290 [sdf -> bsdf renderer] Convert 8bit bitmap to 16bit.
* src/sdf/ftbsdf.c (bsdf_copy_source_to_target): Added
  extra parameter to flip the bitmap vertically.

* src/sdf/ftbsdf.c (bsdf_copy_source_to_target):
  Implemented functionality to convert 8bits per
  pixel bitmap to a 16bits per pixel bitmap, which
  will be used to generate SDF.
2020-08-02 16:33:20 +05:30
Anuj Verma 2c77a8acb2 [sdf] Started the `bsdf' rasterier.
* src/sdf/ftbsdf.c (*): Fix line endings.

* src/sdf/ftbsdf.c (bsdf_copy_source_to_target): Added
  function to copy the source bitmap to the center of
  the target bitmap and also convert it to normalized
  16 bits per pixel bitmap.
2020-08-02 16:33:20 +05:30
Anuj Verma 1d51c5ef7b [sdf] Added interface functions for `bsdf' converter.
* src/base/ftobjs.c (FT_Render_Glyph_Internal): Remove
  the bitmap check which prevent calling renderers
  if the glyph is already a bitmap. The `bsdf' renderer
  requires a bitmap for conversion.

* src/base/ftobjs.c (ft_add_renderer): Remove the glyph
  format check which ensures that the glyph format for
  a renderer is `FT_GLYPH_FORMAT_OUTLINE', again the `bsdf'
  renderer has `FT_GLYPH_FORMAT_BITMAP' as a glyph format,
  so we need to remove the condition to initialize the
  renderer properly.

* src/sdf/ftbsdf.c (*): Added a rasterizer for the
  `bsdf' renderer and created necessary functions.

* src/sdf/ftbsdf.h: Add forward declaration of the
  rasterizer.

* src/sdf/ftsdfrend.c (ft_bitmap_sdf_renderer_class):
  Define the new `bsdf' rendere and add the
  `FT_Renderer_RenderFunc' function, the rest is
  same as the `sdf' renderer.

* src/sdf/ftsdfrend.h: Add forward declaration of the
  renderer.

* src/sdf/rules.mk (SDF_DRV_SRC): Add the new `ftbsdf.c'
  file to the compile list.

* src/sdf/sdf.c: Inclue the `ftbsdf.c' file if making
  single object.
2020-08-02 16:33:20 +05:30
Anuj Verma ddfa07c5da [sdf] Add alloc/free functions.
* src/sdf/ftsdf.c (*): Add `sdf_alloc' and `sdf_free'
  to allocate and deallocate memory respectively.
  By using function we can directly use them in the
  conditional statements, which is not possible when
  using `do {} while ( 0 )'.

* src/sdf/ftsdf.c (SDF_MEMORY_TRACKER_): When not
  debugging don't leave the macros empty otherwise
  some compiler may emit warning because of empty
  `;' semi-colon.
2020-08-02 16:33:20 +05:30
Anuj Verma f41ee8f0c3 [sdf] Added new renderer `bsdf'.
* src/sdf/ftsdfrend.* (*): Added new renderer
  named `bsdf' which basically convert bitmap
  to signed distance fields. This is a part of
  the `sdf' module and not a separate module on
  it's own.

* src/sdf/module.mk (FTMODULE_H_COMMANDS): Add
  the new renderer to the list of modules.

* include/freetype/config/ftmodule.h: Add the
  new renderer to default list of modules when
  not compiling with GNU Make.
2020-08-02 16:33:20 +05:30
Anuj Verma e78016b46d [sdf] Only track memory in debug mode.
* src/sdf/ftsdf.c (*): Disable total memory
  usage tracking if the program is not in
  debug mode.
2020-08-02 16:33:20 +05:30
Anuj Verma c09257f0dd [sdfb] Removed Module.
Remove sdfb module. Instead of a new module
simply create a new renderer in the `sdf'
module for converting bitmaps to SDF.
2020-08-02 16:33:20 +05:30
Anuj Verma 80ba40341c Revert "[sdfb] Added new module to generate SDF from bitmap."
This reverts commit 2136eddbccfa59b7478d87fe0947b35ca6a157f8.
2020-08-02 16:33:20 +05:30
Anuj Verma f4918e139c Revert "[sdfb] Fix GNU Make build."
This reverts commit 0d9179f4e8cdb02986f5d01f178364e3f2b8dc07.
2020-08-02 16:33:20 +05:30
Anuj Verma fca2ed969c [sdfb] Fix GNU Make build.
* src/sdfb/rules.mk (SDFB_DRV_H): Fix name of
  the module specific error file `ftsdfberrs.h'.

* src/sdf/ftsdf.h (Optimizations): Fix warning.
  `warning: comma at end of enumerator list [-Wpedantic]'

* src/sdf/ftsdf.c (sdf_generate_coarse_grid): Remove
  unused variable `cindex', causes warning.
  `warning: unused variable 'cindex' [-Wunused-variable]'
2020-08-02 16:33:20 +05:30
Anuj Verma 04f2a34a7d [sdfb] Added new module to generate SDF from bitmap.
* src/sdfb/sdfb.c, src/sdfb/ftsdfbrend.c,
  src/sdfb/ftsdfb.c, src/sdfb/ftsdfbrend.h,
  src/sdfb/ftsdfb.h, src/sdfb/ftsdfberrs.h:
  Added files required by the `sdfb' renderer module.

* src/sdfb/rules.mk, src/sdfb/module.mk: Added
  files required to build the `sdfb' module using
  the default build system.

* CMakeLists.txt (BASE_SRCS): Add `src/sdfb/sdfb.c'
  to the variable.

* include/freetype/config/ftmodule.h: Added `sdfb'
  module declaration so that the module can be compiled
  when not compiling with GNU make.

* modules.cfg (RASTER_MODULES): Include `sdfb' module
  to the default rasterizer module list.

* include/freetype/ftmoderr.h: sdfb module error define

* src/sdf/rules.mk: Add EOF.
2020-08-02 16:33:20 +05:30
Anuj Verma 968a2f6237 [sdf] Added memory tracker.
* src/sdf/ftsdf.c (SDF_MemoryUser): Added struct
  which is used in place of `FT_Memory::user'.

* src/sdf/ftsdf.c (SDF_ALLOC, SDF_FREE): Added macros
  for allocating and deallocating memory. These macros
  restore the old `FT_Memory::user' before calling the
  relevant allocation/deallocation functions so as to
  not cause errors while debugging memory. And later
  they add the allocated memory size in a variable.

* src/sdf/ftsdf.c (*): Use `SDF_ALLOC', `SDF_FREE'
  instead of the `FT_' variant.
2020-08-02 16:33:19 +05:30
Anuj Verma dfdbc34108 Revert "[sdf] Added total memory allocation log."
This reverts commit d97e060891af0cc59af6fefa84484bec0d1794a4.
2020-08-02 16:33:19 +05:30
Anuj Verma 531d51eb2a [sdf] Added total memory allocation log.
* src/sdf/ftsdf.c (*): Replaced `FT_QNEW' and `FT_ALLOC_MULT'
  to custom macros in order to track memory allocations
  throughout the process of generating SDF. It basically
  add the memory being allocated to a static global variable
  and at the end outputs it at the end.
2020-08-02 16:33:19 +05:30
Anuj Verma 08d5a325dc * src/sdf/ftsdfrend.c (sdf_property_set): Minor fix. 2020-08-02 16:33:19 +05:30
Anuj Verma d40fc55dad [sdf] Temporary change.
Added new property to dynamically change the
optimization to be used to generate the SDF.
This can be used to compare the performance of
different optimization techniques without going
and recompiling the program.
And will also be used in the demo to check the
performance.
2020-08-02 16:33:19 +05:30
Anuj Verma dd5276601c [sdf] Added more properties.
Added two properties:
- `flip_y': To flip the generated SDF along the y axis.
- `flip_sign': By default outside is treated to have
   negative sign, setting `flip_sign' to 1 the outside
   pixels will have positive sign.

* src/sdf/ftsdf.* (*): Use the newly added properties.

* src/sdf/ftsdfrend.* (*): Add the newly added properties,
  and add functionality to set them using `FT_Property_Set'.
2020-08-02 16:33:19 +05:30
Anuj Verma 6b03b1c57a [sdf] Check for left or right fill.
* src/sdf/ftsdf.c (SDF_Params): Added struct which
  contains some internal data required by the rater-
  izer (such as left/right fill).

* src/sdf/ftsdf.c (sdf_raster_render): Determine the
  fill side and pass it to the `sdf_generate_' funcs.

* src/sdf/ftsdf.c (sdf_generate_): Use the new `SDF_Params'
  to generate SDF accordingly.
2020-08-02 16:33:19 +05:30
Anuj Verma 548bd3cf62 [sdf] Optimize the coarse grid optimization.
* src/sdf/ftsdf.c (sdf_generate_coarse_grid): Merge
  the relevant edge finding loop and shortest dist-
  ance search loop. We can find the relevant edges
  of a coarse grid and immediately use them to find
  the shortest distance of the points in the coarse
  grid. This drastically reduce memory usage and 
  performance.
  Also, do the sign assignment of the edges which was
  missing.
2020-08-02 16:33:19 +05:30
Anuj Verma ad696c07d3 * src/sdf/ftsdf.c (*): Fixed warnings.
Fixed various warnings and other few errors.
2020-08-02 16:33:19 +05:30
Anuj Verma 2045c7d9b3 * src/sdf/ftsdf.c (sdf_generate_coarse_grid): Comments.
Add more comments explaining the step by step
process of using coarse grid to increase the
performance of nearest distance search.
2020-08-02 16:33:19 +05:30
Anuj Verma 2afa0cb369 * src/sdf/ftsdf.c (sdf_generate_coarse_grid): Memory leak.
Release the allocated lists for the coarse grid after
we are done using them.
2020-08-02 16:33:19 +05:30
Anuj Verma 80c5bed575 [sdf] Completed the coarse grid optimization.
* src/sdf/ftsdf.c (sdf_generate_coarse_grid): Fixed
  an issue with position calculation and upside down
  images.
2020-08-02 16:33:19 +05:30
Anuj Verma 992609a1db [sdf] Partially completed the coarse grid optimization.
* src/sdf/ftsdf.c (sdf_generate_coarse_grid): Added
  functionality to check distances of all edges from
  the coarse grid and add the relevant edges to the 
  list.
  [Note]: The function is not complete yet.
2020-08-02 16:33:19 +05:30
Anuj Verma 111b755128 [sdf] Added the coarse grid optimization function.
* src/sdf/ftsdf.c (sdf_generate_coarse_grid): The
  function uses coarse grid to optimize nearest edge
  search performance.
2020-08-02 16:33:19 +05:30
Anuj Verma d9eeac807e [sdf] Remove use of `FT_List'.
Simply use a `next' pointer inside `SDF_Edge' and
`SDF_Contour' to create a linked list. This reduces
the number of allocations due to `FT_ListNode', also
we don't need a doubly linked list.

* src/sdf/ftsdf.c (SDF_Edge, SDF_Contour): Remove the
  `FT_List' and use a `next' pointer to create the
  linked list.

* src/sdf/ftsdf.c(sdf_edge_destructor, sdf_contour_destructor):
  Removed, not needed any more.

* src/sdf/ftsdf.c (*): Remove the use of `FT_List_'
  functions wherever necessary and sync with the new
  list.
2020-08-02 16:33:19 +05:30
Anuj Verma 8bf9da21e0 [sdf] Added subdivision optimization.
* src/sdf/ftsdfrend.c (sdf_generate_subdivision): The
  function generate SDF just like the `sdf_generate'
  function, but subdivide the curve into a number of
  lines and then use the `sdf_generate_bounding_box'
  function to generate SDF.
2020-08-02 16:33:19 +05:30
Anuj Verma fa7251ba35 [sdf] Added functions to subdivide a cubic curve.
* src/sdf/ftsdf.c (split_cubic, split_sdf_cubic):
  These functions can be used to subdivide a
  cubic bezier curve into line segments which can
  then be used to generate the SDF.

* src/sdf/ftsdf.c (split_sdf_shape): Added function
  to split a cubic into a line segments.

* src/sdf/ftsdf.c (sdf_shape_done): No need to pass
  `FT_Memory' as a parameter, it can be accessed
  from the `shape' struct.
2020-08-02 16:33:19 +05:30
Anuj Verma 1ba3da86e1 * src/sdf/ftsdf.c (split_sdf_shape): Typo. 2020-08-02 16:33:18 +05:30
Anuj Verma 90abe41ead * src/sdf/ftsdf.c (split_sdf_shape): Memory leak.
* src/sdf/ftsdf.c (split_sdf_shape): Memory leak.
  After recursion the edges variable becomes `NULL'
  so reassign it to deallocate the list completely.
2020-08-02 16:33:18 +05:30
Anuj Verma 470d02aa60 [sdf] Added functions to subdivide a conic curve.
* src/sdf/ftsdf.c (split_conic, split_sdf_conic):
  These functions can be used to subdivide a
  conic bezier curve into line segments which can
  then be used to generate the SDF.

* src/sdf/ftsdf.c (split_sdf_shape): Added function
  to split a outline into a line segments.
2020-08-02 16:33:18 +05:30
Anuj Verma de86a14b32 *[GSoC]ChangeLog: Fix typos. 2020-08-02 16:33:18 +05:30
Anuj Verma c4f64fc0e1 * src/sdf/ftsdfrend.c (sdf_generate_bounding_box): Add buffer for signs. 2020-08-02 16:33:18 +05:30
Anuj Verma e6576cc0e7 [sdf] Precompute the orthogonality. 2020-08-02 16:33:18 +05:30
Anuj Verma 1e3c41d19e [sdf] Added bounding box optimization. 2020-08-02 16:33:18 +05:30
preversewharf45 1d5d8ff288 * src/sdf/ftsdfrend.c (sdf_shape_dump): Use `%ld' to printf `signed long'. 2020-08-02 16:33:18 +05:30
Anuj Verma 44e16a0be6 * src/sdf/ftsdfrend.c (ft_sdf_render): Don't negate unsigned integers. 2020-08-02 16:33:18 +05:30