| 1 | /* --------------- */ |
|---|
| 2 | /* --- lutNR.c --- */ |
|---|
| 3 | /* --------------- */ |
|---|
| 4 | |
|---|
| 5 | // Copyright (c) 2013-2014 Lionel Lacassagne, All Rights Reserved |
|---|
| 6 | // Laboratoire de Recherche en Informatique |
|---|
| 7 | // Universite Paris-Sud / CNRS |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | #include <stdio.h> |
|---|
| 11 | #include <math.h> |
|---|
| 12 | #include <stdlib.h> |
|---|
| 13 | |
|---|
| 14 | #ifdef CLI |
|---|
| 15 | #include "nrc_os_config.h" |
|---|
| 16 | #include "nrc.h" |
|---|
| 17 | #endif |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include "lutNR.h" |
|---|
| 21 | |
|---|
| 22 | /* ------------------- */ |
|---|
| 23 | uint8* alloc_ui8lut(void) |
|---|
| 24 | /* ------------------- */ |
|---|
| 25 | { |
|---|
| 26 | return ui8vector(0, 255); |
|---|
| 27 | } |
|---|
| 28 | /* -------------------- */ |
|---|
| 29 | void free_ui8lut(uint8 *L) |
|---|
| 30 | /* -------------------- */ |
|---|
| 31 | { |
|---|
| 32 | free_ui8vector(L, 0, 255); |
|---|
| 33 | } |
|---|
| 34 | /* -------------------- */ |
|---|
| 35 | void zero_ui8lut(uint8 *L) |
|---|
| 36 | /* -------------------- */ |
|---|
| 37 | { |
|---|
| 38 | zero_ui8vector(L, 0, 255); |
|---|
| 39 | } |
|---|
| 40 | /* ------------------------------------------------- */ |
|---|
| 41 | void display_ui8lut(uint8 *L, char *format, char *name) |
|---|
| 42 | /* ------------------------------------------------- */ |
|---|
| 43 | { |
|---|
| 44 | display_ui8vector(L, 0, 255, format, name); |
|---|
| 45 | } |
|---|
| 46 | /* -------------------------------------------------- */ |
|---|
| 47 | void init_ui8lut(uint8 *L, uint8 threshold, uint8 value) |
|---|
| 48 | /* -------------------------------------------------- */ |
|---|
| 49 | { |
|---|
| 50 | int i; |
|---|
| 51 | for (i = 0; i < threshold; i++) { |
|---|
| 52 | L[i] = 0; |
|---|
| 53 | } |
|---|
| 54 | for (i = threshold; i <= 255; i++) { |
|---|
| 55 | L[i] = value; |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | /* --------------------------------------------------------------------------- */ |
|---|
| 59 | void apply_ui8lut(uint8 **X, int i0, int i1, int j0, int j1, uint8 *L, uint8 **Y) |
|---|
| 60 | /* --------------------------------------------------------------------------- */ |
|---|
| 61 | { |
|---|
| 62 | uint8 x, y; |
|---|
| 63 | int i, j; |
|---|
| 64 | for(i=i0; i<=i1; i++) { |
|---|
| 65 | for(j=j0; j<=j1; j++) { |
|---|
| 66 | //Y[i][j] = L[X[i][j]]; |
|---|
| 67 | x = X[i][j]; |
|---|
| 68 | y = L[x]; |
|---|
| 69 | Y[i][j] = y; |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | /* ------------------------------------------------------------------------------------------------ */ |
|---|
| 74 | void binary_threshold_ui8matrix(uint8 **X, int i0, int i1, int j0, int j1, uint8 threshold, uint8 **Y) |
|---|
| 75 | /* ------------------------------------------------------------------------------------------------ */ |
|---|
| 76 | { |
|---|
| 77 | uint8 *L; |
|---|
| 78 | |
|---|
| 79 | L = alloc_ui8lut(); |
|---|
| 80 | init_ui8lut(L, threshold, 1); |
|---|
| 81 | |
|---|
| 82 | apply_ui8lut(X, i0, i1, j0, j1, L, Y); |
|---|
| 83 | |
|---|
| 84 | free_ui8lut(L); |
|---|
| 85 | } |
|---|
| 86 | /* ------------------------------------------------------------------------------------------------------ */ |
|---|
| 87 | void threshold_ui8matrix(uint8 **X, int i0, int i1, int j0, int j1, uint8 threshold, uint8 value, uint8 **Y) |
|---|
| 88 | /* ------------------------------------------------------------------------------------------------------ */ |
|---|
| 89 | { |
|---|
| 90 | uint8 *L; |
|---|
| 91 | L = alloc_ui8lut(); |
|---|
| 92 | init_ui8lut(L, threshold, value); |
|---|
| 93 | |
|---|
| 94 | apply_ui8lut(X, i0, i1, j0, j1, L, Y); |
|---|
| 95 | |
|---|
| 96 | free_ui8lut(L); |
|---|
| 97 | } |
|---|
| 98 | /* ------------------------------------------------------------------------------- */ |
|---|
| 99 | void double_intensity_ui8matrix(uint8 **X, int i0, int i1, int j0, int j1, uint8 **Y) |
|---|
| 100 | /* ------------------------------------------------------------------------------- */ |
|---|
| 101 | { |
|---|
| 102 | int j; |
|---|
| 103 | uint8 *L; |
|---|
| 104 | L = alloc_ui8lut(); |
|---|
| 105 | for(j=0; j<=127; j++) { L[j] = 2*j; } |
|---|
| 106 | for(j=128; j<=255; j++) { L[j] = 255; } |
|---|
| 107 | |
|---|
| 108 | apply_ui8lut(X, i0, i1, j0, j1, L, Y); |
|---|
| 109 | |
|---|
| 110 | free_ui8lut(L); |
|---|
| 111 | } |
|---|
| 112 | /* ----------------------------------------------------------------------- */ |
|---|
| 113 | void gray2bin_ui8matrix(uint8 **X, int i0, int i1, int j0, int j1, uint8 **Y) |
|---|
| 114 | /* ----------------------------------------------------------------------- */ |
|---|
| 115 | { |
|---|
| 116 | // {0,255} -> {0,1} |
|---|
| 117 | |
|---|
| 118 | uint8 *L; |
|---|
| 119 | L = alloc_ui8lut(); |
|---|
| 120 | init_ui8lut(L, 1, 1); |
|---|
| 121 | apply_ui8lut(X, i0, i1, j0, j1, L, Y); |
|---|
| 122 | free_ui8lut(L); |
|---|
| 123 | } |
|---|
| 124 | /* ----------------------------------------------------------------------- */ |
|---|
| 125 | void bin2gray_ui8matrix(uint8 **X, int i0, int i1, int j0, int j1, uint8 **Y) |
|---|
| 126 | /* ----------------------------------------------------------------------- */ |
|---|
| 127 | { |
|---|
| 128 | // {0,1} -> {0,255} |
|---|
| 129 | |
|---|
| 130 | uint8 *L; |
|---|
| 131 | L = alloc_ui8lut(); |
|---|
| 132 | init_ui8lut(L, 1, 255); |
|---|
| 133 | apply_ui8lut(X, i0, i1, j0, j1, L, Y); |
|---|
| 134 | free_ui8lut(L); |
|---|
| 135 | } |
|---|