forked from barbaraalvx/bitmap-manipulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimgbitmap.c
More file actions
93 lines (70 loc) · 2.34 KB
/
imgbitmap.c
File metadata and controls
93 lines (70 loc) · 2.34 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
83
84
85
86
87
88
89
90
91
92
93
/*USAR O CAMINHO DAS IMAGENS, TANTO NO INPUT DO ARQUIVO DE ENTRADA, QUANTO NO DE SAÍDA*/
#include <stdio.h>
#include <stdlib.h>
void censurar(char linhaBuffer[], int x, int larg) {
for (int i = 0; i < larg; i++) {
linhaBuffer[x + i] = 0;
}
}
int main() {
char arqEntrada[100];
char arqSaida[100];
int x, y, height, width;
printf("Informe o caminho do arquivo de entrada: ");
scanf("%99s", arqEntrada);
printf("Informe as coordenadas da área a ser censurada (X Y): ");
scanf("%d %d", &x, &y);
printf("Informe a altura da censura: ");
scanf("%d", &height);
printf("Informe a largura da censura: ");
scanf("%d", &width);
printf("Qual será o nome do arquivo de saída? ");
scanf("%99s", arqSaida);
FILE *fptrIn;
FILE *fptrOut;
fptrIn = fopen(arqEntrada, "rb");
fptrOut = fopen(arqSaida, "wb");
if (fptrIn == NULL || fptrOut == NULL) {
printf("Não foi possível abrir os arquivos. Encerrando o programa.\n");
return 1;
}
printf("Arquivo aberto com sucesso.\n");
char buffer[54];
size_t bytesLidos;
bytesLidos = fread(buffer, 1, 54, fptrIn);
if (bytesLidos != 54) {
printf("Erro ao ler o cabeçalho do arquivo de entrada. Encerrando o programa.\n");
fclose(fptrIn);
fclose(fptrOut);
return 1;
}
fwrite(buffer, 1, 54, fptrOut);
int arqWidth = *(int*)(buffer + 18);
char linhaBuffer[arqWidth * 3];
size_t bytesPorLinha = arqWidth * 3;
for (int i = 0; i < arqWidth; i++) {
bytesLidos = fread(linhaBuffer, 1, bytesPorLinha, fptrIn);
if (bytesLidos != bytesPorLinha) {
printf("Erro ao ler uma linha da imagem. Encerrando o programa.\n");
fclose(fptrIn);
fclose(fptrOut);
return 1;
}
if (i >= y && i < y + height) {
censurar(linhaBuffer, x * 3, width * 3);
}
fwrite(linhaBuffer, 1, bytesPorLinha, fptrOut);
}
char pixelBuffer[1024];
while (1) {
bytesLidos = fread(pixelBuffer, 1, sizeof(pixelBuffer), fptrIn);
if (bytesLidos == 0) {
break;
}
fwrite(pixelBuffer, 1, bytesLidos, fptrOut);
}
fclose(fptrIn);
fclose(fptrOut);
printf("Censura concluída com sucesso. Imagem salva em %s\n", arqSaida);
return 0;
}