Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma warning(disable : 4996)
- #include <cstdio>
- #include <cmath>
- #include <iostream>
- int type, h, w, max;
- unsigned char *pixels;
- //useful functions
- template <class T>
- void swap(T& a, T& b)
- {
- T tmp = a;
- a = b;
- b = tmp;
- }
- float CharTofloat(char *strnum)
- {
- float num = 0;
- int i, j;
- for (i = 0; strnum[i] != '\0' && strnum[i] != '.'; i++)
- num = num * 10 + strnum[i] - '0';
- if (strnum[i] == '\0')
- return num;
- for (j = 0; strnum[j + i + 1] != '\0'; j++)
- num = num * 10 + strnum[j + i + 1] - '0';
- for (int k = 0; k < j; k++)
- num /= 10;
- return num;
- }
- template <class T>
- T sign(T num) { return num < 0 ? -1 : num > 0 ? 1 : 0; }
- bool OnPic(int coord_x, int coord_y) { return coord_x > 0 && coord_x < w && coord_y > 0 && coord_y < h; }
- int Rounding(float num) { return (int)(sign(num) * (fabs(num) + 0.5)); }
- //file check
- int ReadFile(char* filename)
- {
- FILE *f;
- // file checking (closing file)
- if ((f = fopen(filename, "rb")) == NULL)
- {
- printf("Put another filename!\n");
- fclose(f);
- return 0;
- }
- if (fscanf(f, "P%i%i%i%i\n", &type, &w, &h, &max) == NULL)
- {
- printf("Can't read this file\n");
- fclose(f);
- return 0;
- }
- // data checking (closing file)
- if (h < 1 || w < 1)
- {
- printf("Wrong heigh or width\n");
- fclose(f);
- return 0;
- }
- if (type != 5 && type != 6)
- {
- printf("Wrong filetype\n");
- fclose(f);
- return 0;
- }
- if (max != 255)
- {
- printf("Wrong value of max color\n");
- fclose(f);
- return 0;
- }
- unsigned char check;
- pixels = new unsigned char[w * h];
- //memory checking (closing file and deleting array)
- if (pixels == nullptr)
- {
- printf("Not enoudh memory!\n");
- delete[]pixels;
- fclose(f);
- return 0;
- }
- //data checking (closing file and deleting array)
- if (fread(pixels, 1, w * h, f) < w * h)
- {
- printf("Not enougth pixels\n");
- delete[]pixels;
- fclose(f);
- return 0;
- }
- if (fread(&check, 1, 1, f) == 1)
- {
- printf("Too much pixels\n");
- delete[]pixels;
- fclose(f);
- return 0;
- }
- fclose(f);
- return 1;
- }
- int WriteFile(char* filename)
- {
- FILE* f;
- if ((f = fopen(filename, "wb")) == NULL)
- {
- printf("Can't open output file\n");
- delete[] pixels;
- return 0;
- }
- fprintf(f, "P%i\n%i %i\n%i\n", type, w, h, max);
- fwrite(pixels, 1, w * h, f);
- fclose(f);
- return 1;
- }
- //graphic fucntions
- float Correction(float color, float g) { return (g != 0) ? pow(color / max, 1 / g) * max : (color / max <= 0.0031308) ? 12.92 * color : (pow(1.055 * color / max, 1 / 2.4) - 0.055) * max; }
- float ToNormal(float color, float g) { return (g != 0) ? pow(color / max, g) * max : (color / max <= 12.92 * 0.0031308) ? color / 12.92 : pow((color / max + 0.055), 2.4) / 1.055 * max; }
- float TrueColor(float color) { return color < 0 ? 0 : color > max ? max : color; }
- void PutPoint(float x, float y, float color, float g, float intensity)
- {
- if (OnPic(x, y))
- pixels[Rounding(y * w + x)] = (int)TrueColor(Correction(ToNormal(pixels[Rounding(y * w + x)], g) * (1 - intensity) + color * intensity, g));
- }
- //easy line
- void VerticalLine(float x1, float y1, float x2, float y2, int color, float g, float thickness)
- {
- if ((int)thickness % 2 != 0 && (int)thickness == 0)
- for (int j = y1; j != y2; j += sign(y2 - y1))
- for (int i = -thickness / 2; i <= thickness / 2; i++)
- PutPoint(x1 + i, j, color, g, (1.0 - fabs(i) / thickness * 2.0 / 3.0));
- else
- for (int j = y1; j != y2; j += sign(y2 - y1))
- for (int i = -thickness / 2; i < thickness / 2; i++)
- PutPoint(x1 + i, j, color, g, (1.0 - fabs(i) / thickness * 2.0 / 3.0));
- }
- void HorizontalLine(float x1, float y1, float x2, float y2, int color, float g, float thickness)
- {
- if ((int)thickness % 2 != 0 && (int)thickness == 0)
- for (int j = x1; j != x2; j += sign(x2 - x1))
- for (int i = -thickness / 2; i <= thickness / 2; i++)
- PutPoint(j, y1 + i, color, g, (1.0 - fabs(i) / thickness * 2.0 / 3.0));
- else
- for (int j = x1; j != x2; j += sign(x2 - x1))
- for (int i = -thickness / 2; i < thickness / 2; i++)
- PutPoint(j, y1 + i, color, g, (1.0 - fabs(i) / thickness * 2.0 / 3.0));
- }
- void EasyLine(float x1, float y1, float x2, float y2, int color, float g, float thickness)
- {
- float intensity = 0;
- if (thickness < 1)
- color *= thickness;
- thickness = Rounding(thickness);
- if (y1 == y2)
- HorizontalLine(x1, y1, x2, y2, color, g, thickness);
- else
- VerticalLine(x1, y1, x2, y2, color, g, thickness);
- }
- //wu line
- void WuLine(float x1, float y1, float x2, float y2, int color, float g, float thickness)
- {
- bool is_swaped = false;
- if (abs(x1 - x2) < abs(y1 - y2))
- {
- is_swaped = true;
- swap(x1, y1);
- swap(x2, y2);
- }
- if (x1 > x2)
- {
- swap(x1, x2);
- swap(y1, y2);
- }
- float k = (y2 - y1) / (x2 - x1);
- // float thick_x = thickness * cos, thick_y;
- float cur_y = y1 + k * (Rounding(x1) - x1);
- for (float i = x1; i <= x2; i += 1)
- {
- for (float j = y1 - thickness; j <= y1 + thickness; j++)
- {
- if (!is_swaped)
- {
- PutPoint(i, (int)cur_y, color, g, TrueColor((thickness + 1.0) / 2.0 - fabs(cur_y - j) * cos(atan(k)) * max));
- PutPoint(i, (int)cur_y + 1, color, g, cur_y - (int)cur_y);
- }
- else
- {
- PutPoint((int)cur_y, i, color, g, 1 - (cur_y - (int)cur_y));
- PutPoint((int)cur_y + 1, i, color, g, cur_y - (int)cur_y);
- }
- cur_y += k;
- }
- }
- }
- //main functions
- void MakeLine(float x1, float y1, float x2, float y2, float g, int color, float thickness)
- {
- if ((Rounding(x1 - x2) == 0) || (Rounding(y1 - y2) == 0))
- EasyLine(x1, y1, x2, y2, color, g, thickness);
- else
- WuLine(x1, y1, x2, y2, color, g, thickness);
- }
- int main(int argc, char **argv)
- {
- if (argc < 9)
- {
- printf("Not enough data!\n");
- return 0;
- }
- if (!ReadFile(argv[1]))
- return 0;
- int color = (int)CharTofloat(argv[3]);
- float thickness = CharTofloat(argv[4]);
- float x1 = CharTofloat(argv[5]), y1 = CharTofloat(argv[6]), x2 = CharTofloat(argv[7]), y2 = CharTofloat(argv[8]);
- float g = argc < 10 ? 0 : CharTofloat(argv[9]);
- MakeLine(x1, y1, x2, y2, g, color, thickness);
- if (!WriteFile(argv[2]))
- return 0;
- delete[] pixels;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement