-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrop.h
More file actions
82 lines (71 loc) · 1.95 KB
/
Copy pathcrop.h
File metadata and controls
82 lines (71 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "structs.h"
#include "allocatememory.h"
#include "allocatepixelmatrix.h"
#ifndef CROP_H
#define CROP_H
void crop(selection *select, image *img)
{
int m = select->y2 - select->y1;
int n = select->x2 - select->x1;
// verific daca exista o imagine
if (!img->rgb && !img->data) {
printf("No image loaded");
printf("\n");
} else {
if (img->magic[1] == '2' || img->magic[1] == '5') {
int **temp = NULL;
allocatememory(&temp, m, n);
// in temp transfer elementele care apartin selectiei
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
temp[i][j] = img->data[i + select->y1][j + select->x1];
for (int i = 0; i < img->height; i++)
free(img->data[i]);
free(img->data);
img->height = m;
img->width = n;
allocatememory(&img->data, m, n);
// actualizez imaginea
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
img->data[i][j] = temp[i][j];
// eliberez matricea temporara
for (int i = 0; i < m; i++)
free(temp[i]);
free(temp);
} else {
pixel **temp = allocatepixelmatrix(m, n);
// in temp transfer elementele care apartin selectiei
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
temp[i][j] = img->rgb[i + select->y1][j + select->x1];
for (int i = 0; i < img->height; i++)
free(img->rgb[i]);
free(img->rgb);
img->height = m;
img->width = n;
img->rgb = allocatepixelmatrix(m, n);
// actualizez matricea cu pixelii din selectie
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
img->rgb[i][j] = temp[i][j];
// eliberez matricea temporara
for (int i = 0; i < m; i++)
free(temp[i]);
free(temp);
}
// actualizez dimensiunile si selectia
img->height = m;
img->width = n;
select->x1 = 0;
select->y1 = 0;
select->x2 = img->width;
select->y2 = img->height;
// confirm ca operatia a avut loc cu succes
printf("Image cropped\n");
}
}
#endif