问题二:用C++输出第一张图片

将书上对应的代码抄写了一遍。

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int nx = 200;
    int ny = 100;

    ofstream outfile( "mytest.txt", ios_base::out);
    outfile << "P3\n" << nx << " " << ny << "\n255\n";

    std::cout << "P3\n" << nx << " " << ny << "\n255\n";
    for (int j = ny-1; j >= 0; j--)
    {
        for (int i = 0; i < nx; i++)
        {
            float r = float(i) / float(nx);
            float g = float(j) / float(ny);
            float b = 0.2;
            int ir = int (255.99*r);
            int ig = int (255.99*g);
            int ib = int (255.99*b);

            outfile << ir << " " << ig << " " << ib << "\n";
            std::cout << ir << " " << ig << " " << ib << "\n";
        }
    }
}

将代码原封不动的输入后,确实能够运行(给作者点个赞)。但是说好的图片呢?原文有这么一句话:Opening the output file (in ToyViewer on my mac, but try it in your favorite viewer and google “ppm viewer” if your viewer doesn’t support it) shows(结果的那张图)。
有两个疑问:
1. “the output file”在哪?
2. “ppm viewer”是什么?
感觉告诉我接下来应该这么做:先将输出结果导入文件(即输出到文件),然后用一个ppm viewer来将这个图片看成图片(但愿如此)。

2.1 将结果输出到文件

如上红色部分的代码就是为了将输出结果导入文件。

#include <fstream>
ofstream outfile( "mytest.txt", ios_base::out);
outfile << "P3\n" << nx << " " << ny << "\n255\n";
outfile << ir << " " << ig << " " << ib << "\n";

简单解释如下:

#include <fstream>
/*iostream是输入输出流库标准文件(注意它没有后缀),它包含cout的信息,这对我们的程序是必需的。#include是预处理器指示符(preprocessor directive),它把iostream的内容读入我们的文本文件中*/
ofstream outfile( ".\\results\\FirstImage.txt", ios_base::out);
/*打开当前目录(工程目录)下results文件夹中的FirstImage.txt文件*/
outfile << "P3\n" << nx << " " << ny << "\n255\n";
/*往文件中写入数据*/

顺便说一下标准输出:

#include <iostream>
using namespace std;
/*这条语句被称作using指示符(using directive)。 C++标准库中的名字都是在一个称作std的名字空间中声明的,这些名字在我们的程序文本文件中是不可见的,除非我们显式地使它们可见。using指示符告诉编译器要使用在名字空间std中声明的名字。*/
std::cout << "P3\n" << nx << " " << ny << "\n255\n";
/*往屏幕上输出数据*/

将结果输出到文件mytest.txt,这个就是“the output file”啦。
(这个文件已经传到“我的资源”中)

2.2 找一个ppm viewer

结缘巧合,找到了XnView。用这个软件看mytest.txt时,看到的是这个:这里贴出一张通过“XnView”将结果的.txt转换成.jpg后的图片:
这里写图片描述

完整的操作步骤是这样的滴:

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

  • 11
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
C++中可以使用`std::async`和`std::future`来实现异步线程,保证不用等待第一个线程处理完就开始第个线程。 具体实现过程如下: 1. 定义两个线程函数,分别为`threadFunc1`和`threadFunc2`。 ```c++ void threadFunc1(std::string inputImage, std::future<std::string>& futureObj) { // 处理图片输出结果 std::string outputImage = processImage(inputImage); std::cout << "Thread 1 output: " << outputImage << std::endl; // 将处理结果传递给future对象 futureObj.set_value(outputImage); } void threadFunc2(std::future<std::string>& futureObj) { // 获取线程1的处理结果 std::string inputImage = futureObj.get(); // 处理图片输出结果 std::string outputImage = processImage(inputImage); std::cout << "Thread 2 output: " << outputImage << std::endl; } ``` 2. 在主函数中,创建一个future对象,并将其传递给线程1和线程2。 ```c++ int main() { // 创建future对象 std::promise<std::string> promiseObj; std::future<std::string> futureObj = promiseObj.get_future(); // 创建线程1和线程2 std::thread thread1(threadFunc1, "inputImage.jpg", std::ref(futureObj)); std::thread thread2(threadFunc2, std::ref(futureObj)); // 等待线程1和线程2执行完毕 thread1.join(); thread2.join(); return 0; } ``` 在上述代码中,线程1的输出会被传递给future对象,而线程2会等待future对象获取到线程1的输出后再开始执行。这样就能保证线程2不需要等待线程1处理完就可以开始执行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值