|
1 | 1 | #include <math.h>
|
| 2 | +#include <pthread.h> |
2 | 3 | #include <stdio.h>
|
3 | 4 | #include <stdlib.h>
|
4 | 5 | #include "haar.h"
|
5 | 6 |
|
| 7 | +#define N_THREADS 8 |
| 8 | + |
| 9 | + |
| 10 | +struct haar_transform_job { |
| 11 | + struct spectral_image* images; |
| 12 | + unsigned int first_image; |
| 13 | + unsigned int last_image; |
| 14 | +}; |
| 15 | + |
6 | 16 |
|
7 | 17 | /**
|
8 | 18 | * Applies in place the 1-dimension Haar transform.
|
@@ -34,11 +44,8 @@ static void transform_array(float* data, unsigned int size) {
|
34 | 44 |
|
35 | 45 | /**
|
36 | 46 | * Transforms in place a spectral image.
|
37 |
| - * |
38 |
| - * @param data The frame buffer |
39 |
| - * @param image_index The index of the image to transform |
40 | 47 | */
|
41 |
| -static void transform_image(struct spectral_image* image) { |
| 48 | +void transform_image(struct spectral_image* image) { |
42 | 49 | // The 2D standard Haar transform consists of applying
|
43 | 50 | // the 1D Haar transform to each row of the image and then
|
44 | 51 | // to each column of the result
|
@@ -66,8 +73,33 @@ static void transform_image(struct spectral_image* image) {
|
66 | 73 | }
|
67 | 74 |
|
68 | 75 |
|
69 |
| -void apply_Haar_transform(struct spectral_images* images) { |
70 |
| - for (unsigned int i = 0 ; i < images->n_images ; i++) { |
71 |
| - transform_image(&(images->images[i])); |
| 76 | +static void* launch_Haar_job(struct haar_transform_job* job) { |
| 77 | + for (unsigned int i = job->first_image ; i <= job->last_image ; i++) { |
| 78 | + transform_image(&(job->images[i])); |
72 | 79 | }
|
| 80 | + |
| 81 | + return NULL; |
73 | 82 | }
|
| 83 | + |
| 84 | + |
| 85 | +void apply_Haar_transform(struct spectral_images* spectral_images) { |
| 86 | + pthread_t thread[N_THREADS]; |
| 87 | + struct haar_transform_job haar_job[N_THREADS]; |
| 88 | + unsigned int images_per_thread = spectral_images->n_images / N_THREADS; |
| 89 | + |
| 90 | + for (unsigned int k = 0 ; k < N_THREADS ; k++) { |
| 91 | + unsigned int start = k * images_per_thread; |
| 92 | + unsigned int end = (k == N_THREADS - 1) |
| 93 | + ? spectral_images->n_images - 1 |
| 94 | + : (k + 1) * images_per_thread - 1; |
| 95 | + haar_job[k].images = spectral_images->images; |
| 96 | + haar_job[k].first_image = start; |
| 97 | + haar_job[k].last_image = end; |
| 98 | + |
| 99 | + pthread_create(&(thread[k]), NULL, (void* (*)(void*))launch_Haar_job, &(haar_job[k])); |
| 100 | + } |
| 101 | + for (unsigned int k = 0 ; k < N_THREADS ; k++) { |
| 102 | + pthread_join(thread[k], NULL); |
| 103 | + } |
| 104 | +} |
| 105 | + |
0 commit comments