lundi 29 juin 2015

what is really inside a picture and how to create one using C


I once saw someone create a picture using C, like this:

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <math.h>

float f(float x, float y, float z) {
    float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
    return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
}

float h(float x, float z) {
    for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
        if (f(x, y, z) <= 0.0f)
            return y;
    return 0.0f;
}

int main() {
    FILE* fp = fopen("heart.ppm", "w");
    int sw = 512, sh = 512;
    fprintf(fp, "P3\n%d %d\n255\n", sw, sh);
    for (int sy = 0; sy < sh; sy++) {
        float z = 1.5f - sy * 3.0f / sh;
        for (int sx = 0; sx < sw; sx++) {
            float x = sx * 3.0f / sw - 1.5f;
            float v = f(x, 0.0f, z);
            int r = 0;
            if (v <= 0.0f) {
                float y0 = h(x, z);
                float ny = 0.001f;
                float nx = h(x + ny, z) - y0;
                float nz = h(x, z + ny) - y0;
                float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
                float d = (nx + ny - nz) / sqrtf(3) * nd * 0.5f + 0.5f;
                r = (int)(d * 255.0f);
            }
            fprintf(fp, "%d 0 0 ", r);
        }
        fputc('\n', fp);
    }
    fclose(fp);
        return 0;
}

What he have done is only putting a few number in the file heart.ppm. Only a few number, and he get an amazing 3D picture in photoshop, like this:enter image description here

So I was very curious that what is inside that heart.ppm file. But after opening it, I can only see a bunch of numbers, pure number. And then I open another jpg file, and see only a mess.

So, I was curious what is the magic here. Maybe only the format count. Maybe there is always only number and characters in a picture. But when using some tool (like photoshop) to open it, the tool would translate that message and then make it a picture for you.(Right?)

And I wanna ask,

1.How to make a photo like that using C ? You can't get a picture with only putting a few number in a file. There must be some standard to give those numbers a sense and make them consist of a picture.

  1. what is the standard of those format ? .jpg, .ppm, .png, etc. any citation and related links?

Aucun commentaire:

Enregistrer un commentaire