Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Роман Мищенко
assignment-3-image-transform
Commits
bf9eefd4
Commit
bf9eefd4
authored
1 month ago
by
Роман Мищенко
Browse files
Options
Download
Email Patches
Plain Diff
tests passed
parent
a7d71594
master
No related merge requests found
Pipeline
#119755
failed with stages
in 6 seconds
Changes
7
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
191 additions
and
22 deletions
+191
-22
solution/include/bmp.h
solution/include/bmp.h
+2
-3
solution/include/image.h
solution/include/image.h
+6
-5
solution/include/io.h
solution/include/io.h
+2
-4
solution/include/utils.h
solution/include/utils.h
+0
-9
solution/src/bmp.c
solution/src/bmp.c
+68
-0
solution/src/main.c
solution/src/main.c
+24
-1
solution/src/transformations.c
solution/src/transformations.c
+89
-0
No files found.
solution/include/bmp.h
View file @
bf9eefd4
...
...
@@ -2,8 +2,7 @@
#define IMAGE_TRANSFORM_BMP_H
#include <image.h>
#include <read_status.h>
#include <write_status.h>
#include <io.h>
#include <stdint.h>
#include <stdio.h>
...
...
@@ -32,7 +31,7 @@ bmp_header {
};
enum
read_status
from_bmp
(
FILE
*
in
,
struct
image
*
img
);
enum
write_status
to_bmp
(
FILE
*
file
,
struct
image
*
img
);
enum
write_status
to_bmp
(
FILE
*
dest
,
struct
image
*
img
);
#endif
This diff is collapsed.
Click to expand it.
solution/include/image.h
View file @
bf9eefd4
...
...
@@ -9,11 +9,12 @@ struct image {
struct
pixel
*
data
;
};
void
none
(
struct
image
*
init_pic
,
struct
image
*
final_pic
);
void
cw_90
(
struct
image
*
init_pic
,
struct
image
*
final_pic
);
void
ccw_90
(
struct
image
*
init_pic
,
struct
image
*
final_pic
);
void
flip_v
(
struct
image
*
init_pic
,
struct
image
*
final_pic
);
void
flip_h
(
struct
image
*
init_pic
,
struct
image
*
final_pic
);
void
transform_image
(
struct
image
*
first_image
,
struct
image
*
second_image
,
char
*
option
);
void
none
(
struct
image
*
first_image
,
struct
image
*
second_image
);
void
cw_90
(
struct
image
*
first_image
,
struct
image
*
second_image
);
void
ccw_90
(
struct
image
*
first_image
,
struct
image
*
second_image
);
void
flip_v
(
struct
image
*
first_image
,
struct
image
*
second_image
);
void
flip_h
(
struct
image
*
first_image
,
struct
image
*
second_image
);
#endif
This diff is collapsed.
Click to expand it.
solution/include/io.h
View file @
bf9eefd4
...
...
@@ -7,14 +7,12 @@ enum read_status {
READ_OK
=
0
,
READ_INVALID_SIGNATURE
,
READ_INVALID_BITS
,
READ_INVALID_HEADER
,
READ_OPEN_FAIL
READ_INVALID_HEADER
};
enum
write_status
{
WRITE_OK
=
0
,
WRITE_ERROR
,
WRITE_OPEN_FAIL
WRITE_ERROR
};
enum
read_status
read_img
(
char
*
path
,
struct
image
*
img
);
...
...
This diff is collapsed.
Click to expand it.
solution/include/utils.h
deleted
100644 → 0
View file @
a7d71594
#ifndef IMAGE_TRANSFORM_UTILS_H
#define IMAGE_TRANSFORM_UTILS_H
#include <image.h>
void
free_img_data
(
struct
image
*
image
);
#endif
This diff is collapsed.
Click to expand it.
solution/src/bmp.c
0 → 100644
View file @
bf9eefd4
#include <bmp.h>
#include <stdlib.h>
#include <io.h>
enum
read_status
validate
(
struct
bmp_header
*
headers
,
FILE
*
source
){
if
(
fread
(
headers
,
sizeof
(
struct
bmp_header
),
1
,
source
)
!=
1
||
headers
->
bfileSize
<=
HEADER_NORMAL_SIZE
)
return
READ_INVALID_HEADER
;
if
(
headers
->
biPlanes
!=
1
||
headers
->
biBitCount
!=
BITS_PER_PIXEL
)
return
READ_INVALID_BITS
;
if
(
headers
->
bfType
!=
BMP
)
return
READ_INVALID_SIGNATURE
;
return
READ_OK
;
}
enum
read_status
from_bmp
(
FILE
*
source
,
struct
image
*
img
){
enum
read_status
status
;
struct
bmp_header
headers
;
status
=
validate
(
&
headers
,
source
);
img
->
data
=
malloc
(
headers
.
biHeight
*
headers
.
biWidth
*
sizeof
(
struct
pixel
));
if
(
!
img
->
data
)
return
READ_INVALID_BITS
;
img
->
height
=
headers
.
biHeight
;
img
->
width
=
headers
.
biWidth
;
uint32_t
padding_size
=
(
4
-
(
img
->
width
*
3
)
%
4
)
%
4
;
for
(
size_t
i
=
0
;
i
<
img
->
height
;
i
++
)
{
if
(
fread
(
img
->
data
+
i
*
img
->
width
,
sizeof
(
struct
pixel
),
img
->
width
,
source
)
!=
img
->
width
)
return
READ_INVALID_BITS
;
fseek
(
source
,
padding_size
,
SEEK_CUR
);
}
return
status
;
}
enum
write_status
to_bmp
(
FILE
*
dest
,
struct
image
*
img
){
struct
bmp_header
header
=
{
0
};
header
.
bfType
=
BMP
;
header
.
bOffBits
=
HEADER_NORMAL_SIZE
;
header
.
biSize
=
40
;
header
.
biHeight
=
img
->
height
;
header
.
biWidth
=
img
->
width
;
header
.
biPlanes
=
1
;
header
.
biBitCount
=
BITS_PER_PIXEL
;
size_t
padding_size
=
(
4
-
(
header
.
biWidth
*
3
)
%
4
)
%
4
;
uint8_t
padding_byte
=
0
;
header
.
biSizeImage
=
(
header
.
biWidth
*
sizeof
(
struct
pixel
)
+
padding_size
)
*
header
.
biHeight
;
header
.
bfileSize
=
HEADER_NORMAL_SIZE
+
header
.
biSizeImage
;
if
(
fwrite
(
&
header
,
HEADER_NORMAL_SIZE
,
1
,
dest
)
!=
1
)
return
WRITE_ERROR
;
for
(
size_t
i
=
0
;
i
<
img
->
height
;
i
++
)
{
if
(
fwrite
(
img
->
data
+
i
*
img
->
width
,
sizeof
(
struct
pixel
),
img
->
width
,
dest
)
!=
img
->
width
)
return
WRITE_ERROR
;
for
(
size_t
j
=
0
;
j
<
padding_size
;
j
++
)
{
if
(
fwrite
(
&
padding_byte
,
1
,
1
,
dest
)
!=
1
)
return
WRITE_ERROR
;
}
}
free
(
img
->
data
);
return
WRITE_OK
;
}
This diff is collapsed.
Click to expand it.
solution/src/main.c
View file @
bf9eefd4
#include <bmp.h>
#include <image.h>
#include <io.h>
#include <stdio.h>
int
main
(
int
argc
,
char
**
argv
)
{
(
void
)
argc
;
(
void
)
argv
;
// supress 'unused parameters' warning
if
(
argc
!=
4
)
{
fprintf
(
stderr
,
"Error: invalid count of arguments!"
);
return
1
;
}
struct
image
first_image
=
{
0
};
FILE
*
source
=
fopen
(
argv
[
1
],
"rb"
);
if
(
!
source
)
return
2
;
enum
read_status
status_r
=
from_bmp
(
source
,
&
first_image
);
if
(
status_r
!=
READ_OK
)
return
12
;
fclose
(
source
);
struct
image
second_image
=
{
0
};
transform_image
(
&
first_image
,
&
second_image
,
argv
[
3
]);
FILE
*
destination
=
fopen
(
argv
[
2
],
"wb"
);
if
(
!
destination
)
return
4
;
enum
write_status
status_w
=
to_bmp
(
destination
,
&
second_image
);
if
(
status_w
!=
WRITE_OK
)
return
3
;
fclose
(
destination
);
return
0
;
}
This diff is collapsed.
Click to expand it.
solution/src/transformations.c
0 → 100644
View file @
bf9eefd4
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "image.h"
bool
create_image
(
struct
image
*
second_image
,
struct
image
*
first_image
){
second_image
->
height
=
first_image
->
height
;
second_image
->
width
=
first_image
->
width
;
second_image
->
data
=
malloc
(
first_image
->
height
*
first_image
->
width
*
sizeof
(
struct
pixel
));
return
second_image
->
data
!=
NULL
;
}
void
none
(
struct
image
*
first_image
,
struct
image
*
second_image
){
if
(
!
create_image
(
second_image
,
first_image
))
return
;
for
(
int64_t
i
=
0
;
i
<
second_image
->
height
;
i
++
)
{
for
(
int64_t
j
=
0
;
j
<
second_image
->
width
;
j
++
)
{
second_image
->
data
[
i
*
second_image
->
width
+
j
]
=
first_image
->
data
[
i
*
first_image
->
width
+
j
];
}
}
}
void
flip_h
(
struct
image
*
first_image
,
struct
image
*
second_image
){
if
(
!
create_image
(
second_image
,
first_image
))
return
;
for
(
int64_t
i
=
0
;
i
<
first_image
->
height
;
i
++
)
{
for
(
int64_t
j
=
0
;
j
<
first_image
->
width
;
j
++
)
{
second_image
->
data
[
i
*
second_image
->
width
+
(
first_image
->
width
-
1
-
j
)]
=
first_image
->
data
[
i
*
first_image
->
width
+
j
];
}
}
}
void
flip_v
(
struct
image
*
first_image
,
struct
image
*
second_image
){
if
(
!
create_image
(
second_image
,
first_image
))
return
;
for
(
int64_t
i
=
0
;
i
<
first_image
->
height
;
i
++
)
{
for
(
int64_t
j
=
0
;
j
<
first_image
->
width
;
j
++
)
{
second_image
->
data
[(
first_image
->
height
-
1
-
i
)
*
second_image
->
width
+
j
]
=
first_image
->
data
[
i
*
first_image
->
width
+
j
];
}
}
}
void
cw90
(
struct
image
*
first_image
,
struct
image
*
second_image
){
second_image
->
width
=
first_image
->
height
;
second_image
->
height
=
first_image
->
width
;
second_image
->
data
=
malloc
(
second_image
->
height
*
second_image
->
width
*
sizeof
(
struct
pixel
));
if
(
!
second_image
->
data
)
return
;
for
(
int64_t
i
=
0
;
i
<
first_image
->
height
;
i
++
)
{
for
(
int64_t
j
=
0
;
j
<
first_image
->
width
;
j
++
)
{
second_image
->
data
[
j
*
second_image
->
width
+
i
]
=
first_image
->
data
[
i
*
first_image
->
width
+
(
first_image
->
width
-
1
-
j
)];
}
}
}
void
ccw90
(
struct
image
*
first_image
,
struct
image
*
second_image
){
second_image
->
width
=
first_image
->
height
;
second_image
->
height
=
first_image
->
width
;
second_image
->
data
=
malloc
(
second_image
->
width
*
second_image
->
height
*
sizeof
(
struct
pixel
));
if
(
!
second_image
->
data
)
return
;
for
(
int64_t
i
=
0
;
i
<
first_image
->
height
;
i
++
)
{
for
(
int64_t
j
=
0
;
j
<
first_image
->
width
;
j
++
)
{
second_image
->
data
[
j
*
second_image
->
width
+
first_image
->
height
-
1
-
i
]
=
first_image
->
data
[
i
*
first_image
->
width
+
j
];
}
}
}
void
transform_image
(
struct
image
*
first_image
,
struct
image
*
second_image
,
char
*
option
){
if
(
strcmp
(
option
,
"none"
)
==
0
)
none
(
first_image
,
second_image
);
if
(
strcmp
(
option
,
"fliph"
)
==
0
)
flip_h
(
first_image
,
second_image
);
if
(
strcmp
(
option
,
"flipv"
)
==
0
)
flip_v
(
first_image
,
second_image
);
if
(
strcmp
(
option
,
"ccw90"
)
==
0
)
ccw90
(
first_image
,
second_image
);
if
(
strcmp
(
option
,
"cw90"
)
==
0
)
cw90
(
first_image
,
second_image
);
free
(
first_image
->
data
);
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help