Como Criar Imagens do Zero com C++

👧 Nesse vídeo criamos imagens .ppm do zero com C++ sem bibliotecas!


Como Criar Imagens do Zero com C++


Nesse vídeo criamos imagens do zero com C++ sem bibliotecas!

Links da Wikipedia sobre formato de imagens:


🎥 Assista ao Vídeo


📰 Códigos criado no vídeo

color.cpp

#include <iostream>
#include <fstream>

int main(){
  std::ofstream image("image.ppm");
  const int width = 800, height = 450;

  if(image.is_open()){
    image << "P3\n";
    image << width << " " << height << '\n';
    image << "255\n";

    for(int y = 0; y < height; ++y){
      for(int x = 0; x < width; ++x){
        image << "214 5 175 ";
      }
      image << '\n';
    }

    image.close();
  }
}

rect.cpp

#include <iostream>
#include <fstream>

int main(){
  std::ofstream image("image.ppm");
  const int width = 800, height = 450;
  const int rectX = 45, rectY = 45, rectW = width - 100, rectH = height - 100;

  if(image.is_open()){
    image << "P3\n";
    image << width << " " << height << '\n';
    image << "255\n";

    for(int y = 0; y < height; ++y){
      for(int x = 0; x < width; ++x){
        if(x >= rectX && x < rectX + rectW && 
           y >= rectY && y < rectY + rectH){
          image << "214 5 175 ";
        }else{
          image << "253 204 59 ";
        }
      }
      image << '\n';
    }

    image.close();
  }
}

circle.cpp

#include <iostream>
#include <fstream>

int main(){
  std::ofstream image("image.ppm");
  const int width = 800, height = 450;

  const int centerX = width / 2;
  const int centerY = height / 2;
  const int radius = 80;

  if(image.is_open()){
    image << "P3\n";
    image << width << " " << height << '\n';
    image << "255\n";

    for(int y = 0; y < height; ++y){
      for(int x = 0; x < width; ++x){
        const int dx = x - centerX;
        const int dy = y - centerY;

        if(dx * dx + dy * dy <= radius * radius){
          image << "214 5 175 ";
        }else{
          image << "253 204 59 ";
        }

      }
      image << '\n';
    }

    image.close();
  }
}

gradiente.cpp

#include <iostream>
#include <fstream>

int main(){
  std::ofstream image("image.ppm");
  const int width = 800, height = 450;

  if(image.is_open()){
    image << "P3\n";
    image << width << " " << height << '\n';
    image << "255\n";

    for(int y = 0; y < height; ++y){
      for(int x = 0; x < width; ++x){
        const int r = (255 * x) / (width - 1);
        const int g = 0;
        const int b = 255;
        image << r << " " << g << " " << b << " ";
      }
      image << '\n';
    }

    image.close();
  }
}

lenna.cppconvert $1 -compress none -depth 8 -colorspace RGB -define pgm:format=plain $2

#include <iostream>
#include <fstream>

int main(){
  std::ifstream input("./lenna.ppm");
  std::ofstream output("./lenna_bw.ppm");

  std::string format;
  int width, height, maxval;
  input >> format >> width >> height >> maxval;

  output << format << '\n' << width << " " << height << '\n' << maxval << '\n';

  for(int i = 0; i < width * height; ++i){
    int r, g, b;
    input >> r >> g >> b;

    int gray = static_cast<int>(0.9f * r + 0.8f * g + 0.7f * b);
    //int bw = (gray > 100) ? maxval : 0;
    //output << bw << " " << bw << " " << bw << " ";
    output << gray << " " << gray << " " << gray << " ";
  }
}

👀 Veja também:


🔌 Mais


cpp


Compartilhe


Nosso canal no Youtube

Inscreva-se


Marcos Oliveira

Marcos Oliveira

Desenvolvedor de software
https://github.com/terroo


Crie Aplicativos Gráficos para Linux e Windows com C++

Aprenda C++ Moderno e crie Games, Programas CLI, GUI e TUI de forma fácil.

Saiba Mais

Receba as novidades no seu e-mail!

Após cadastro e confirmação do e-mail, enviaremos semanalmente resumos e também sempre que houver novidades por aqui para que você mantenha-se atualizado!