Translate

I have written an image processing library in the C language.

I have written an image processing library in the C language.
I have added the functionality of simulating graphic display in the terminal, which makes it more convenient to test and verify results when developing new features. The feature that converts normal colors into colors that are close and can be displayed on a 256-color terminal has been rewritten many times, requiring the AI to calculate the closest color that can be displayed on the terminal by converting normal colors. This feature is something I have always wanted to implement. It can be used not only in Terminal, but also as the foundation for many other interesting functionalities. There is still much room for improvement, and behind each feature lies much knowledge that must be learned and improved upon during use.
There are now 1300 lines of code, With the help of AI, it only took a few days to complete the spatial-temporal task, and there is no need to worry about problems arising. Each feature is only related to one or two basic elements. As long as the basic elements are not modified, fixing a feature's problem only requires changing the problematic part, which only involves a few lines of code. The AI is fully capable of handling this. As long as this process is maintained, even if the code increases by thousands of lines, it can be easily handled.Ultimately, it will be combined with an AI model. Once this library is finished, we will start researching loading large language models through C programming language.


const int colorMap[256][3] = {

{0, 0, 0}, {128, 0, 0}, {0, 128, 0}, {128, 128, 0},

    {0, 0, 128}, {128, 0, 128}, {0, 128, 128}, {192, 192, 192},

    {128, 128, 128}, {255, 0, 0}, {0, 255, 0}, {255, 255, 0},

    {0, 0, 255}, {255, 0, 255}, {0, 255, 255}, {255, 255, 255},

    {0, 0, 0}, {0, 0, 95}, {0, 0, 135}, {0, 0, 175},

    {0, 0, 215}, {0, 0, 255}, {0, 95, 0}, {0, 95, 95},

    {0, 95, 135}, {0, 95, 175}, {0, 95, 215}, {0, 95, 255},

    {0, 135, 0}, {0, 135, 95}, {0, 135, 135}, {0, 135, 175},

    {0, 135, 215}, {0, 135, 255}, {0, 175, 0}, {0, 175, 95},

    {0, 175, 135}, {0, 175, 175}, {0, 175, 215}, {0, 175, 255},

    {0, 215, 0}, {0, 215, 95}, {0, 215, 135}, {0, 215, 175},

    {0, 215, 215}, {0, 215, 255}, {0, 255, 0}, {0, 255, 95},

    {0, 255, 135}, {0, 255, 175}, {0, 255, 215}, {0, 255, 255},

    {95, 0, 0}, {95, 0, 95}, {95, 0, 135}, {95, 0, 175},

    {95, 0, 215}, {95, 0, 255}, {95, 95, 0}, {95, 95, 95},

    {95, 95, 135}, {95, 95, 175}, {95, 95, 215}, {95, 95, 255},

    {95, 135, 0}, {95, 135, 95}, {95, 135, 135}, {95, 135, 175},

    {95, 135, 215}, {95, 135, 255}, {95, 175, 0}, {95, 175, 95},

    {95, 175, 135}, {95, 175, 175}, {95, 175, 215}, {95, 175, 255},

    {95, 215, 0}, {95, 215, 95}, {95, 215, 135}, {95, 215, 175},

    {95, 215, 215}, {95, 215, 255}, {95, 255, 0}, {95, 255, 95},

    {95, 255, 135}, {95, 255, 175}, {95, 255, 215}, {95, 255, 255},

    {135, 0, 0}, {135, 0, 95}, {135, 0, 135}, {135, 0, 175},

    {135, 0, 215}, {135, 0, 255}, {135, 95, 0}, {135, 95, 95},

    {135, 95, 135}, {135, 95, 175}, {135, 95, 215}, {135, 95, 255},

    {135, 135, 0}, {135, 135, 95}, {135, 135, 135}, {135, 135, 175},

    {135, 135, 215}, {135, 135, 255}, {135, 175, 0}, {135, 175, 95},

    {135, 175, 135}, {135, 175, 175}, {135, 175, 215}, {135, 175, 255},

    {135, 215, 0}, {135, 215, 95}, {135, 215, 135}, {135, 215, 175},

    {135, 215, 215}, {135, 215, 255}, {135, 255, 0}, {135, 255, 95},

    {135, 255, 135}, {135, 255, 175}, {135, 255, 215}, {135, 255, 255},

    {175, 0, 0}, {175, 0, 95}, {175, 0, 135}, {175, 0, 175},

    {175, 0, 215}, {175, 0, 255}, {175, 95, 0}, {175, 95, 95},

    {175, 95, 135}, {175, 95, 175}, {175, 95, 215}, {175, 95, 255},

    {175, 135, 0}, {175, 135, 95}, {175, 135, 135}, {175, 135, 175},

    {175, 135, 215}, {175, 135, 255}, {175, 175, 0}, {175, 175, 95},

    {175, 175, 135}, {175, 175, 175}, {175, 175, 215}, {175, 175, 255},

    {175, 215, 0}, {175, 215, 95}, {175, 215, 135}, {175, 215, 175},

    {175, 215, 215}, {175, 215, 255}, {175, 255, 0}, {175, 255, 95},

    {175, 255, 135}, {175, 255, 175}, {175, 255, 215}, {175, 255, 255},

    {215, 0, 0}, {215, 0, 95}, {215, 0, 135}, {215, 0, 175},

    {215, 0, 215}, {215, 0, 255}, {215, 95, 0}, {215, 95, 95},

    {215, 95, 135}, {215, 95, 175}, {215, 95, 215}, {215, 95, 255},

    {215, 135, 0}, {215, 135, 95}, {215, 135, 135}, {215, 135, 175},

    {215, 135, 215}, {215, 135, 255}, {215, 175, 0}, {215, 175, 95},

    {215, 175, 135}, {215, 175, 175}, {215, 175, 215}, {215, 175, 255},

    {215, 215, 0}, {215, 215, 95}, {215, 215, 135}, {215, 215, 175},

    {215, 215, 215}, {215, 215, 255}, {215, 255, 0}, {215, 255, 95},

    {215, 255, 135}, {215, 255, 175}, {215, 255, 215}, {215, 255, 255},

    {255, 0, 0}, {255, 0, 95}, {255, 0, 135}, {255, 0, 175},

    {255, 255, 255}

}; 

struct Matrix {

    int width;

    int height;

    unsigned char* array;

    Matrix(int w, int h,unsigned char value=0) {

        width = w;

        height = h;

        array = (unsigned char*)malloc(width * height * sizeof(unsigned char));

        if (array == NULL) {

            fprintf(stderr, "Failed to allocate memory for array\n");

            exit(EXIT_FAILURE);

        }

        for (int i = 0; i < width * height; i++) {

            array[i] = value ;

        }

    }

    ~Matrix() {

        free(array);

    }

    int getWidth() {

        return width;

    }

    int getHeight() {

        return height;

    }

    unsigned char getValue(Point2D point) {

        int x = round(point.x);

        int y = round(point.y);

        if (x < 0 || x >= width || y < 0 || y >= height) {

            return 0;

        }

        return array[y * width + x];

    }

    void setValue(Point2D point, unsigned char value) {

        int x = round(point.x);

        int y = round(point.y);

        if (x < 0 || x >= width || y < 0 || y >= height) {

            return;

        }

        array[y * width + x] = value;

    }

    

    void clear(unsigned char value = 0) {

        for (int i = 0; i < width * height; i++) {

            array[i] = value;

        }

    }

void showMatrix(int col, int row) {

    for (int i = 0; i < 256; i++) {

        init_color(i, colorMap[i][0], colorMap[i][1], colorMap[i][2]);

        init_pair(i, i, i);

    }

    // Draw the matrix on the ncurses screen

    for (int y = 0; y < getHeight(); y++) {

        for (int x = 0; x < getWidth(); x++) {

            Point2D point = { (float)x, (float)y };

            unsigned char value = getValue(point);

            // Choose the color pair based on the value

            int colorPair = (int)value;

            // Set the color pair and print a star

            attron(COLOR_PAIR(colorPair));

            mvprintw(row + y, col + x*2, "  ");

            attroff(COLOR_PAIR(colorPair));

        }

    }

    // Refresh the screen and wait for a key press to exit

    refresh();

    

}

};



#include "myimage.h"

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include  <cassert>

#include <assert.h>

#include <vector>

#include <cmath>

#include <stdint.h>

#include <iostream>

#include <ncurses.h>

#include <time.h>

#include <sys/time.h>

#include <unistd.h>

#include <termios.h>

#include <sys/ioctl.h>

#include "png.h"

#include <jpeglib.h>

#include "uuid4.h"

#include <ncurses.h>

#include <stdlib.h>

#include <time.h>

#include <sys/time.h>

#include <unistd.h>

void main() {

 initscr();

   int maxRow, maxCol; 

    getmaxyx(stdscr, maxRow, maxCol);

  start_color();

 Image img=FromImage("icon.png").resize(maxCol/2,maxCol/2);

 Image img2=FromImage("bot.png").resize(maxCol/2,maxCol/2);

 Image img3=FromImage("image.jpg").resize(maxCol/2,maxCol/2);

 Matrix matrix=img.toMatrix();

 Matrix matrix2=img2.toMatrix();

 Matrix matrix3=img3.toMatrix();

 Matrix matrix4=Matrix(maxCol/2,maxCol/2,64);

 Bezier bezier;

 Line line;

 bezier.controlPoint={(float)maxCol/4,(float)maxCol/4};

 bezier.points=new Point2D[3];

bezier.points[0]={(float)0,(float)maxCol/2};

bezier.points[1]={(float)maxCol/8,(float)maxCol/8};

bezier.points[2]={(float)maxCol/2,(float)maxCol/8};

line.points={{},{},{}};

line.points[0]={(float)0,(float)maxCol/2};

line .points[1]={(float)maxCol/8,(float)maxCol/8};

line.points[2]={(float)maxCol/2,(float)maxCol/8};

 

 struct timeval startTime;   

 struct timeval endTime; 

gettimeofday(&startTime, NULL); 

 struct timespec req = {.tv_sec = 1, .tv_nsec = 0 }; 

struct timespec req1 = {.tv_sec = 0, .tv_nsec = 90000000 }; 

struct timespec req2 = {.tv_sec = 0, .tv_nsec = 30000000 }; 

while (1){ 

mvprintw(0,0,"Matrix (%d X %d) from icon.png. " ,maxCol/2,maxCol/2);

   matrix.showMatrix(0,1);

   nanosleep(&req, NULL);

mvprintw(0,0,"Matrix (%d X %d) from bot.png. " ,maxCol/2,maxCol/2);

   matrix2.showMatrix(0,1);

  nanosleep(&req, NULL);

mvprintw(0,0,"Matrix (%d X %d) from image.jpg. " ,maxCol/2,maxCol/2);

   matrix3.showMatrix(0,1);

  nanosleep(&req, NULL);

  for (int i = 0; i < 5; i++) {

mvprintw(0,0,"Matrix4 (%d X %d) . " ,maxCol/2,maxCol/2);

matrix4.clear(rand()%239+16);

   matrix4.showMatrix(0,1);

  nanosleep(&req1, NULL);

}

matrix4.clear(0);

Rectangle rect;

rect.topLeft={(float )0,(float )0};

rect.bottomRight={(float )maxCol/2,(float )maxCol/2};

Ellipse ellipse;

ellipse.center={(float )maxCol/4,(float)maxCol /4};

ellipse.xRadius=(float)maxCol /4-1;

ellipse.yRadius=(float)maxCol /4-5;

Polygon pologon0=ellipse.toPolygon(100);

Polygon pologon=rect.toPolygon();

 StraightLine *xLines= pologon.scanLineX(0,maxCol/2);

StraightLine *yLines0= pologon.scanLineY(0,maxCol/2);

mvprintw(0,0,"Matrix4 scanLineXY (%d X %d) . " ,maxCol/2,maxCol/2);

 	for (int i = 0; i <maxCol/2; i++)

	{

int v=rand()%239+16;		

for (auto point : xLines[i].toPoints()){

matrix4.setValue(point,v);

   

}

matrix4.showMatrix(0,1);

  nanosleep(&req2, NULL);  

}

	for (int i = 0; i <maxCol/2; i++)

	{

int v=rand()%239+16;		

for (auto point : yLines0[i].toPoints()){

matrix4.setValue(point,v);

   

}

matrix4.showMatrix(0,1);

  nanosleep(&req2, NULL);  

}

struct StraightLine *yLines= pologon.scanLineY(0,maxCol/2);

matrix4.clear(0);

mvprintw(0,0,"Matrix4 ellipse and  bezier (%d X %d) . " ,maxCol/2,maxCol/2);

 	for (int i = 0; i <maxCol/2; i++)

	{

int v=rand()%239+16;		

for (auto point : ellipse.toPoints()){

matrix4.setValue(point,v);  

}

for (auto point : bezier.toPoints()){

matrix4.setValue(point,v);  

}

for (auto point : line.toPoints()){

matrix4.setValue(point,v);  

}

matrix4.showMatrix(0,1);

  nanosleep(&req2, NULL);  

}

} 

}

Still in constant improvement, this is written in C language. The speed of processing pixels is very fast. The slowness observed when dealing with high pixel count is due to the slow process of simulating images with colored characters in the terminal. The rotation function has also been modified multiple times, and in the end, I had to come up with my own solution. Now, in the rotated image, there will absolutely be no empty spaces in the center. The advantage of developing it on my own is that I can customize functionalities as required and efficiently accomplish various tasks. Still in constant improvement, this is written in C language. The speed of processing pixels is very fast. The slowness observed when dealing with high pixel count is due to the slow process of simulating images with colored characters in the terminal. The rotation function has also been modified multiple times, and in the end, I had to come up with my own solution. Now, in the rotated image, there will absolutely be no empty spaces in the center. The advantage of developing it on my own is that I can customize functionalities as required and efficiently accomplish various tasks.
Finally, this idea be implemented in C language.
The rotation function,only requires 50 lines of code and includes rotation, translation, center point adjustment,, position adjustment and the simulation of 3D rotation. The key is that the simplest method was used within a single computational cycle to achieve all of the above functions, thereby minimizing computational power consumption. This also proves that with C langua,ge, it is possible to achieve powerful functionality with very little code.

#include "myimage.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cassert>
#include <assert.h>
#include <vector>
#include <cmath>
#include <stdint.h>
#include <iostream>
#include <ncurses.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#include "png.h"
#include <jpeglib.h>
#include "uuid4.h"
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>

void main()
{
	initscr();
	int maxRow, maxCol;
	getmaxyx(stdscr, maxRow, maxCol);
	start_color();
	Image img = FromImage("icon.png").resize(maxCol / 2, maxCol / 2);
	Image img2 = FromImage("bot.png").resize(maxCol / 2, maxCol / 2);
	Image img3 = FromImage("image.jpg").resize(maxCol / 2, maxCol / 2);
	Matrix matrix = img.toMatrix();
	Matrix matrix2 = img2.toMatrix();
	Matrix matrix3 = img3.toMatrix();
	Matrix matrix4 = Matrix(maxCol / 2, maxCol / 2, 64);
	Bezier bezier;
	Line line;
	bezier.controlPoint = {(double)maxCol / 4, (double)maxCol / 4};
	bezier.points = new Point2D[3];
	bezier.points[0] = {(double)0, (double)maxCol / 2};
	bezier.points[1] = {(double)maxCol / 8, (double)maxCol / 8};
	bezier.points[2] = {(double)maxCol / 2, (double)maxCol / 8};
	line.points = {{}, {}, {}};
	line.points[0] = {(double)0, (double)maxCol / 2};
	line.points[1] = {(double)maxCol / 8, (double)maxCol / 8};
	line.points[2] = {(double)maxCol / 2, (double)maxCol / 8};

	struct timeval startTime;
	struct timeval endTime;
	gettimeofday(&startTime, NULL);
	struct timespec req = {.tv_sec = 1, .tv_nsec = 0};
	struct timespec req1 = {.tv_sec = 0, .tv_nsec = 90000000};
	struct timespec req2 = {.tv_sec = 0, .tv_nsec = 1000000};

	while (1)
	{
		matrix4.clear(0);
		Rectangle rect;
		rect.topLeft = {(double)0, (double)0};
		rect.bottomRight = {(double)maxCol / 2, (double)maxCol / 2};
		Ellipse ellipse;
		ellipse.center = {(double)maxCol / 4, (double)maxCol / 4};
		ellipse.xRadius = (double)maxCol / 8 - 1;
		ellipse.yRadius = (double)maxCol / 4 - 1;
		Polygon pologon0 = ellipse.toPolygon(maxCol);
		Polygon pologon1 = ellipse.toPolygon(maxCol);
		pologon1.resize(0.5);

		Polygon pologon = rect.toPolygon();
		StraightLine *xLines = pologon.scanLineX(0, maxCol / 2);
		StraightLine *yLines0 = pologon.scanLineY(0, maxCol / 2);

		struct StraightLine *yLines = pologon0.scanLineY(0, maxCol / 2);

		mvprintw(0, 0, "Matrix (%d X %d) from icon.png.          ", maxCol / 2, maxCol / 2);
		double aX, aY, aZ, r, oX, oY;
		aX = 0;
		r = 1.42;
		oX = (double)(0 - maxCol / 4.0);
		oY = (double)(maxCol / 4.0);

		for (double j = 0; j < 360; j++)
		{
			Matrix matrixT = img.rotated(aX, r, NULL, NULL, oX, oY).toMatrix();
			oX += (double)maxCol / 720.0;
			oY -= (double)maxCol / 720.0;
			aX--;
			r += 0.5 / 360;
			matrixT.showMatrix(0, 1);
			nanosleep(&req2, NULL);
		}

		mvprintw(0, 0, "Matrix (%d X %d) from bot.png.         ", maxCol / 2, maxCol / 2);

		aX = 0;
		oX = (double)(maxCol / 4.0);
		oY = (double)(maxCol / 4.0);

		for (double j = 0; j < 360; j++)
		{
			Matrix matrixT = img2.rotated(aX, r, NULL,NULL,oX, oY).toMatrix();
			oX -= maxCol / 720.0;
			oY -= maxCol / 720.0;
			aX++;
			r -= (0.5 / 360);
			matrixT.showMatrix(0, 1);
			nanosleep(&req2, NULL);
		}

		mvprintw(0, 0, "Matrix (%d X %d) 3D simulation         ", maxCol / 2, maxCol / 2);
		aX = 0;
		aY=90;
		aZ=45;

		for (double j = 0; j < 720; j++)
		{
			Matrix matrixT = img3.rotated(aX, 1, NULL, NULL, NULL, NULL,aY,aZ).toMatrix();
			matrixT.showMatrix(0, 1);
			aX++;
			aY++;
			aZ--;
			nanosleep(&req2, NULL);
		}

	   aX = 0;
		aY=45;
		aZ=90;

		for (double j = 0; j < 720; j++)
		{
			Matrix matrixT = img3.rotated(aX, 1, NULL, NULL, NULL, NULL,aY,aZ).toMatrix();
			matrixT.showMatrix(0, 1);
			aX--;
			aY--;
			aZ++;
			nanosleep(&req2, NULL);
		}


aX = 0;
		r = 1.42;
		oX = (double)(0 - maxCol / 4.0);
		oY = (double)(maxCol / 4.0);
		aX = 0;
		aY=45;
		aZ=90;

		for (double j = 0; j < 720; j++)
		{
			Matrix matrixT = img.rotated(aX, r, NULL, NULL, oX, oY,aY,aZ).toMatrix();
			oX += (double)maxCol / 720.0/2;
			oY -= (double)maxCol / 720.0/2;
			aX--;
			//r += 0.5 / 360;
			aX--;
			aY++;
			aZ--;
			matrixT.showMatrix(0, 1);
			nanosleep(&req2, NULL);
		}

aX = 0;
		r = 1.42;
		oX = (double)(maxCol / 4.0);
		oY = (double)(maxCol / 4.0);
		aX = 0;
		aY=45;
		aZ=90;

		for (double j = 0; j < 720; j++)
		{
			Matrix matrixT = img2.rotated(aX, 1, NULL, NULL, oX, oY,aY,aZ).toMatrix();
			oX -= (double)maxCol / 720.0/2;
			oY -= (double)maxCol / 720.0/2;
			aX--;
			//r += 0.5 / 360;
			aX--;
			aY--;
			aZ++;
			matrixT.showMatrix(0, 1);
			nanosleep(&req2, NULL);
		}


		matrix4.clear(0);
		mvprintw(0, 0, "Matrix4 ellipse and  bezier (%d X %d) . ", maxCol / 2, maxCol / 2);
		int v;
		int vx, vy, k;
		k = maxCol / 8;
		vx = maxCol / 4 - k;
		vy = maxCol / 4 - k;
		pologon0.moveTo(vx, vy);
		for (int i = 0; i < k; i++)
		{
			matrix4.clear(0);
			v = rand() % 239 + 16;

			for (auto point : pologon0.toPoints())
			{
				matrix4.setValue(point, v);
			}
			v = rand() % 239 + 16;
			for (auto point : bezier.toPoints())
			{
				matrix4.setValue(point, v);
			}
			v = rand() % 239 + 16;
			for (auto point : line.toPoints())
			{
				matrix4.setValue(point, v);
			}

			vx++;
			vy++;
			pologon0.moveTo(vx, vy);

			matrix4.showMatrix(0, 1);
			nanosleep(&req1, NULL);
		}
		matrix4.clear(0);
		mvprintw(0, 0, "Matrix4 ellipse  rotate(%d X %d) .       ", maxCol / 2, maxCol / 2);
		for (int j = 0; j < 50; j++)
		{
			v = rand() % 239 + 16;
			pologon1.rotate(0.15);
			matrix4.clear(0);
			for (auto point : pologon1.toPoints())
			{
				matrix4.setValue(point, v);
			}
			matrix4.showMatrix(0, 1);
			nanosleep(&req1, NULL);
		}
		mvprintw(0, 0, "Matrix4 ellipse  resize(%d X %d) .       ", maxCol / 2, maxCol / 2);
		matrix4.clear(0);
		for (int j = 0; j < 50; j++)
		{
			v = rand() % 239 + 16;
			pologon1.resize(1.015);
			matrix4.clear(0);
			for (auto point : pologon1.toPoints())
			{
				matrix4.setValue(point, v);
			}
			matrix4.showMatrix(0, 1);
			nanosleep(&req1, NULL);
		}
		for (int j = 0; j < 50; j++)
		{
			v = rand() % 239 + 16;
			pologon1.resize(1 / 1.015);
			matrix4.clear(0);
			for (auto point : pologon1.toPoints())
			{
				matrix4.setValue(point, v);
			}
			matrix4.showMatrix(0, 1);
			nanosleep(&req1, NULL);
		}

		matrix4.clear(0);
		mvprintw(0, 0, "Matrix4 scanLineXY (%d X %d) .        ", maxCol / 2, maxCol / 2);
		for (int i = 0; i < maxCol / 2; i++)
		{
			v = rand() % 239 + 16;
			for (auto point : xLines[i].toPoints())
			{
				matrix4.setValue(point, v);
			}
			matrix4.showMatrix(0, 1);
			nanosleep(&req2, NULL);
		}
		matrix4.clear(0);
		for (int i = 0; i < maxCol / 2; i++)
		{
			v = rand() % 239 + 16;
			for (auto point : yLines0[i].toPoints())
			{
				matrix4.setValue(point, v);
			}
			matrix4.showMatrix(0, 1);
			nanosleep(&req2, NULL);
		}

		for (int i = 0; i < 5; i++)
		{
			mvprintw(0, 0, "Matrix4 (%d X %d) .               ", maxCol / 2, maxCol / 2);
			matrix4.clear(rand() % 239 + 16);
			matrix4.showMatrix(0, 1);
			nanosleep(&req1, NULL);
		}
	}
}

沒有留言:

發佈留言