Skip to content

Commit

Permalink
1.0: init
Browse files Browse the repository at this point in the history
  • Loading branch information
zvezdochiot committed Nov 23, 2023
0 parents commit a44e1fe
Show file tree
Hide file tree
Showing 12 changed files with 494 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "src/stb"]
path = src/stb
url = https://github.com/nothings/stb.git
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 2.8)
project(stbidespeckle)
add_compile_options(-O3)
include_directories(src)
add_executable(stbidespeckle src/stbidespeckle.c src/dependencies.c)
target_link_libraries(stbidespeckle m)
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# stbidespeckle

Stb Image Despeckle.

| origin | despeckle | [stbimmetrics](https://github.com/ImageProcessing-ElectronicPublications/stb-image-metrics) (vifp1) |
| --- | --- | --- |
| (lossless) | (default opts: radius = 5, coef. = 0.25) | (black - bad, white - good) |
| ![orig](images/lena.png) | ![despeckled](images/lena.despeckle.png) | ![metrics: vifp1](images/lena.despeckle.vifp1.png) |

## build

### load submodules

submodules:
- [stb](https://github.com/nothings/stb.git) -> [src/stb](src)

```shell
$ git submodule init
$ git submodule update
```

### install dependencies

build dependencies:

- build-essential
- cmake

```shell
$ sudo apt-get install build-essential cmake
```

### compilation
```shell
$ mkdir build
$ cd build
$ cmake ..
$ make
```
## use

The first and second parameters specify the paths to the image and the result {PNG}. The `-c` option sets the despeckle factor. The `-r` option sets the radius despeckle.
```shell
./stbidespeckle [options] image_in image_out.png
options:
-c N.N coef (default 0.250000)
-r NUM radius (default 5)
-h show this help message and exit
```

## structure

- `dependencies.c` - API [stb](https://github.com/nothings/stb.git)
- `despeckle.h` - despeckle image.
- `stb/` - [stb](https://github.com/nothings/stb.git)
- `stbidespeckle.c` - CLI program.

---

Binary file added images/lena.despeckle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lena.despeckle.vifp1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lena.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions man/man1/stbidespeckle.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.TH "StbIDespeckle" 1 1.0 "23 Now 2023" "User Manual"

.SH NAME
stbidespeckle

.SH DESCRIPTION
Stb Image Despeckle.

.SH SYNOPSIS
stbidespeckle [options] image_in image_out.png

.SH OPTIONS
.TP
-c N.N
coef (default 0.250000)
.TP
-r NUM
radius (default 5)
.TP
-h
show this help message and exit

.SH EXAMPLE
stbidespeckle lena.png lena.despeckle.png

Load: lena.png
image: 512x512:3
Despeckle 5 0.250000
Save png: lena.despeckle.png

.SH COPYRIGHT
This is free and unencumbered software released into the public domain.

.SH SEE ALSO
convert(1), gm(1), imagew(1)

.SH CONTACTS
Website: https://github.com/ImageProcessing-ElectronicPublications/stbiscalenx
6 changes: 6 additions & 0 deletions src/dependencies.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define DESPECKLE_IMPLEMENTATION
#include <stb/stb_image.h>
#include <stb/stb_image_write.h>
#include "despeckle.h"
238 changes: 238 additions & 0 deletions src/despeckle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
/*
https://github.com/heptagonhust/bicubic-image-resize/issues/9
*/

#ifndef DESPECKLE_H_
#define DESPECKLE_H_

#include <stdbool.h>
#include <math.h>
#define DESPECKLE_VERSION "1.0"

#ifdef DESPECKLE_STATIC
#define DESPECKLEAPI static
#else
#define DESPECKLEAPI extern
#endif

#ifdef __cplusplus
extern "C" {
#endif
DESPECKLEAPI void Despeckle (unsigned char *src, int height, int width, int channels, int radius, float coef, unsigned char *res);
#ifdef __cplusplus
}
#endif

#ifdef DESPECKLE_IMPLEMENTATION

typedef struct
{
unsigned char c[4];
}
pixel;

typedef struct
{
float c[4];
}
pixelf;

static pixel PixelCopy(pixel p, int channels)
{
int d;
pixel r;

for (d = 0; d < channels; d++)
{
r.c[d] = p.c[d];
}

return r;
}

static float PixelDist (pixel pA, pixel pB, int channels)
{
int d;
float dist = 0.0f;

for(d = 0; d < channels; d++)
{
dist += (pB.c[d] - pA.c[d]) * (pB.c[d] - pA.c[d]);
}

return dist;
}

static float PixelCross (pixel pA, pixel pB,pixel pC, pixel pD, int channels)
{
int d;
float cross = 0.0f;

for(d = 0; d < channels; d++)
{
cross += (pB.c[d] - pA.c[d]) * (pD.c[d] - pC.c[d]);
}

return cross;
}

static pixel PixelMean9 (pixel pA, pixel pB,pixel pC, pixel pD, pixel pE, pixel pF, pixel pG, pixel pH, pixel pI, int channels)
{
int d, sum;
pixel mean;

for(d = 0; d < channels; d++)
{
sum = pA.c[d] + pB.c[d] + pC.c[d] + pD.c[d] + pE.c[d] + pF.c[d] + pG.c[d] + pH.c[d] + pI.c[d];
sum += 4;
sum /= 9;
mean.c[d] = sum;
}

return mean;
}

static pixelf PixelGrad (pixel p1, pixel p2, pixel p3, pixel p4, int channels)
{
int d;
pixelf pR;

for(d = 0; d < channels; d++)
{
pR.c[d] = p1.c[d];
pR.c[d] += p2.c[d];
pR.c[d] -= p3.c[d];
pR.c[d] -= p4.c[d];
}

return pR;
}

static pixelf PixelDelta (pixelf px, pixelf py, pixelf pz, float dx, float dy, float dz, int channels)
{
int d;
pixelf pR;

for(d = 0; d < channels; d++)
{
pR.c[d] = (py.c[d] * dx + px.c[d] * dy - 0.5f * pz.c[d] * dz);
}

return pR;
}

static pixel PixelAlign (pixel p0, pixel pM, pixelf pd, float coef, int channels)
{
int d;
float sum;
pixel pR;

for(d = 0; d < channels; d++)
{
sum = p0.c[d];
if ((pM.c[d] > 128 && pd.c[d] > 0.0f) || (pM.c[d] < 128 && pd.c[d] < 0.0f))
{
sum += (coef * pd.c[d]);
sum += 0.5f;
sum = (sum < 0.0f) ? 0.0f : (sum < 255.0f) ? sum : 255.0f;
}
pR.c[d] = (int) sum;
}

return pR;
}

static pixel PixelGet (unsigned char *image, int height, int width, int channels, int y, int x)
{
int d;
pixel p;
size_t k;

y = (y < 0) ? 0 : (y < height) ? y : (height - 1);
x = (x < 0) ? 0 : (x < width) ? x : (width - 1);
k = ((width * y) + x) * channels;

for (d = 0; d < channels; d++)
{
p.c[d] = image[k + d];
}

return p;
}

static void PixelSet (unsigned char *image, int height, int width, int channels, int y, int x, pixel p)
{
int d;
size_t k;

y = (y < 0) ? 0 : (y < height) ? y : (height - 1);
x = (x < 0) ? 0 : (x < width) ? x : (width - 1);
k = ((width * y) + x) * channels;

for (d = 0; d < channels; d++)
{
image[k + d] = p.c[d];
}
}

DESPECKLEAPI void Despeckle (unsigned char *src, int height, int width, int channels, int radius, float coef, unsigned char *res)
{
int j, y, x;
pixel pA, pB, pC, pD, pE, pF, pG, pH, pI, pM, pR;
float dIy, dIx, dIxy, dI;
pixelf gy, gx, gxy, gd2;

for (j = 0; j < radius; j++)
{
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
pA = PixelGet(src, height, width, channels, y - 1, x - 1);
pB = PixelGet(src, height, width, channels, y - 1, x);
pC = PixelGet(src, height, width, channels, y - 1, x + 1);
pD = PixelGet(src, height, width, channels, y, x - 1);
pE = PixelGet(src, height, width, channels, y, x);
pF = PixelGet(src, height, width, channels, y, x + 1);
pG = PixelGet(src, height, width, channels, y + 1, x - 1);
pH = PixelGet(src, height, width, channels, y + 1, x);
pI = PixelGet(src, height, width, channels, y + 1, x + 1);
dIy = PixelDist(pH, pB, channels);
dIx = PixelDist(pF, pD, channels);
dIxy = PixelCross(pB, pH, pD, pF, channels);
dI = dIy + dIx;
pM = PixelMean9(pA, pB, pC, pD, pE, pF, pG, pH, pI, channels);

if (dI > 0.0f)
{
dIx /= dI;
dIy /= dI;
dIxy /= dI;
gy = PixelGrad(pB, pH, pE, pE, channels);
gx = PixelGrad(pD, pF, pE, pE, channels);
gxy = PixelGrad(pC, pG, pA, pI, channels);
gd2 = PixelDelta(gy, gx, gxy, dIx, dIy, dIxy, channels);
pR = PixelAlign(pE, pM, gd2, coef, channels);
}
else
{
pR = pM;
}

PixelSet(res, height, width, channels, y, x, pR);
}
}
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
pR = PixelGet(res, height, width, channels, y, x);
PixelSet(src, height, width, channels, y, x, pR);
}
}
}
}

#endif /* DESPECKLE_IMPLEMENTATION */

#endif /* DESPECKLE_H_ */
1 change: 1 addition & 0 deletions src/stb
Submodule stb added at 03f50e
Loading

0 comments on commit a44e1fe

Please sign in to comment.