vaja 3 predvecer

This commit is contained in:
2022-03-28 23:48:47 +02:00
parent a95c3bd90a
commit 4f0a9d0783
9 changed files with 232 additions and 0 deletions

3
vaja3/Makefile Normal file
View File

@@ -0,0 +1,3 @@
julia:
cc -lm julia.c cplxlib.c knjiznica/bitmap.c -o julia

36
vaja3/cplxlib.c Normal file
View File

@@ -0,0 +1,36 @@
#include <stdio.h>
typedef struct {double re, im;} cplx;
cplx csum (cplx a, cplx b) // Complex sum
{
cplx sum;
sum.re = a.re + b.re;
sum.im = a.im + b.im;
return sum;
}
cplx cdif (cplx a, cplx b) // Complex difference
{
cplx difference;
difference.re = a.re - b.re;
difference.im = a.im - b.im;
return difference;
}
cplx cprod (cplx a, cplx b) // Complex product
{
cplx product;
product.re = (a.re * b.re) - (a.im * b.im);
product.im = (a.im * b.re) + (a.re * b.im);
return product;
}
cplx cquo (cplx a, cplx b) // Complex quotient
{
cplx quotient;
quotient.re = ((a.re * b.re)+(a.im * b.im)) / ((b.re*b.re)+(b.im*b.im));
quotient.im = ((a.im * b.re)-(a.re * b.im)) / ((b.re*b.re)+(b.im*b.im));
return quotient;
}

5
vaja3/cplxlib.h Normal file
View File

@@ -0,0 +1,5 @@
typedef struct {double re, im;} cplx;
cplx csum (cplx a, cplx b); // Complex sum
cplx cdif (cplx a, cplx b); // Complex difference
cplx cprod (cplx a, cplx b); // Complex product
cplx cquo (cplx a, cplx b); // Complex quotient

BIN
vaja3/julia Executable file

Binary file not shown.

BIN
vaja3/julia.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 MiB

47
vaja3/julia.c Normal file
View File

@@ -0,0 +1,47 @@
#include "knjiznica/bitmap.h"
#include "cplxlib.h"
#include "stdio.h"
#include "math.h"
#define resolution 3400
const double dimension = 1.7;
const double orbit = 2.0;
unsigned char canvas[resolution][resolution]; // platno za risanje
unsigned char iteratePolynomial(cplx z0, cplx c)
{
cplx z = z0;
for (unsigned i = 0; i<255; i++)
{
z = cprod(z, z);
z = csum(z, c);
if (sqrt((z.re*z.re)+(z.im*z.im)) > orbit) return i+1;
}
return 255;
}
int main(void)
{
cplx c;
double a,b;
printf("Vnesi adicijsko konstanto polinoma: ");
scanf("%lf%lfi", &a, &b);
puts("Iteriram:");
c.re = a;
c.im = b;
for(unsigned p = 0; p < resolution; p++)
{
printf(".");
for(unsigned q = 0; q < resolution; q++)
{
cplx z0;
z0.re = ((dimension/resolution) * p * 2) - dimension;
z0.im = -(((dimension/resolution) * q * 2) - dimension);
canvas[p][q] = iteratePolynomial(z0, c);
}
}
shraniBMP(canvas, resolution, resolution, "julia.bmp");
printf("\n\n Končano. Slika shranjena\n");
}

12
vaja3/knjiznica/README.txt Executable file
View File

@@ -0,0 +1,12 @@
Knji¾nica za pretvorbo 2D arraya v BMP datoteko (slika).
Deluje na platformah Windows, Linux, Mac.
CodeBlocks:
1. Knji¾nico bitmap.c in header datoteko bitmap.h skopiraj v mapo svojega projekta.
2. bitmap.cpp vkljuèi v svoj projekt. (desni klik na ime projekta -> Add files -> izberete datoteke -> Open files -> v oknu Multiple Decision izberete OK)
3. Na vrhu svojega projekta to knji¾nico tudi vkljuèi z vrstico #include "bitmap.h"
4. Uporabljaj funkcijo na naèin:
shraniBMP(ime_dvodimenzionalnega_arraya_s_podatki_tipa_unsigned_char, DIMx, DIMy, "imeslike.bmp");
Datoteka .bmp se shrani v mapo projekta. Èe je ni, poglejte ¹e pod mapo Debug.

122
vaja3/knjiznica/bitmap.c Executable file
View File

@@ -0,0 +1,122 @@
#include "bitmap.h"
#include <stdio.h>
#include <stdint.h>
//#include <windows.h>
//#ifndef WIN32
#pragma pack(push, bmp_packing, 1)
typedef uint8_t BYTE; // 8 nit unsigned
typedef int32_t LONG; // 32 bit signed
typedef uint16_t WORD; // 16 bit unsigned
typedef char* LPCTSTR;
typedef uint32_t DWORD; // 32 bit unsigned
typedef uint16_t WCHAR; // 16 bit unicode char
typedef struct tagRGBQUAD {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;
typedef struct tagBITMAPINFOHEADER {
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;
#pragma pack(pop, bmp_packing)
//#endif
// Return 0 on failure
int SaveBitmapToFile(void* data, unsigned int lWidth, unsigned int lHeight, unsigned int wBitsPerPixel, const char *fileName) {
RGBQUAD palette[256];
FILE *f;
BITMAPINFOHEADER bmpInfoHeader = {0};
BITMAPFILEHEADER bfh = {0};
unsigned char *pBitmapBits=(unsigned char *)data;
unsigned int i;
char emptySpace[] = {0, 0, 0, 0};
for(i = 0; i < 256; ++i)
{
palette[i].rgbBlue = i;
palette[i].rgbGreen = (i * 2) % 256;
palette[i].rgbRed = (i * 3) % 256;
}
// Set the size
bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
// Bit count
bmpInfoHeader.biBitCount = wBitsPerPixel;
// Use all colors
bmpInfoHeader.biClrImportant = 0;
// Use as many colors according to bits per pixel
bmpInfoHeader.biClrUsed = 0;
// Store as un Compressed
bmpInfoHeader.biCompression = 0; // BI_RGB;
// Set the height in pixels
bmpInfoHeader.biHeight = lHeight;
// Width of the Image in pixels
bmpInfoHeader.biWidth = lWidth;
// Default number of planes
bmpInfoHeader.biPlanes = 1;
// Calculate the image size in bytes
bmpInfoHeader.biSizeImage = lWidth* lHeight * (wBitsPerPixel/8);
// This value should be values of BM letters i.e 0x4D42
// 0x4D = M 0×42 = B storing in reverse order to match with endian
bfh.bfType = 'B'+('M' << 8);
// <<8 used to shift M to end
// Offset to the RGBQUAD
bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER) + sizeof(RGBQUAD) * 256;
// Total size of image including size of headers
bfh.bfSize = bfh.bfOffBits + bmpInfoHeader.biSizeImage;
// Create the file in disk to write
f=fopen(fileName, "wb");
if (!f) {
return 0;
}
// Write the File header
fwrite(&bfh, sizeof(bfh), 1, f);
// Write the bitmap info header
fwrite(&bmpInfoHeader, sizeof(bmpInfoHeader), 1, f);
// Write the palette
fwrite(&palette[0], sizeof(RGBQUAD), 256, f);
// Write the RGB Data
if(lWidth%4 == 0) {
fwrite(pBitmapBits, bmpInfoHeader.biSizeImage, 1, f);
} else {
for(i = 0; i < lHeight; ++i) {
fwrite(&pBitmapBits[i * lWidth], lWidth, 1, f);
fwrite(emptySpace, 4 - lWidth % 4, 1, f);
}
}
// Close the file handle
fclose(f);
return 1;
}
int shraniBMP(void *slika, unsigned int w, unsigned int l, const char *f) {
return SaveBitmapToFile(slika, w, l, 8, f);
}

7
vaja3/knjiznica/bitmap.h Executable file
View File

@@ -0,0 +1,7 @@
#ifndef BITMAP_H
#define BITMAP_H
int SaveBitmapToFile(void* data, unsigned int lWidth, unsigned int lHeight, unsigned int wBitsPerPixel, const char *fileName);
int shraniBMP(void *slika, unsigned int w, unsigned int l, const char *f);
#endif //BITMAP_H