Comment detail

JPEGをGETして色反転して保存 (Nested Flatten)
C++BuilderのVCLを使います。(C++Builder5で確認)

C++Builder上で「VCLを使用するコンソールアプリ」を新規作成し、ソースに以下のコードを指定してください。
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <vcl.h>
#pragma hdrstop
#include <jpeg.hpp>

#include <wininet.h>
#pragma comment(lib, "wininet.lib")

#include <iostream>
#include <memory>

class TInternet : public TObject
{
public:
    __fastcall TInternet();

    virtual __fastcall ~TInternet();

    void __fastcall Load(const String& url, TStream* stream);

private:
    HINTERNET _hInternet;
};

__fastcall TInternet::TInternet()
{
    _hInternet = ::InternetOpen(
        "",
        INTERNET_OPEN_TYPE_PRECONFIG,
        NULL,
        NULL,
        0);

    if (! _hInternet)
    {
        RaiseLastWin32Error();
    }
}

__fastcall TInternet::~TInternet()
{
    ::InternetCloseHandle(_hInternet);
}

void __fastcall TInternet::Load(const String& url, TStream* stream)
{
    HANDLE hFile = ::InternetOpenUrl(
        _hInternet,
        url.c_str(),
        NULL,
        0,
        INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_EXISTING_CONNECT,
        0);

    if (hFile == NULL)
    {
        RaiseLastWin32Error();
    }

    try
    {
        while (true)
        {
            char buf[2048];

            DWORD read = 0;

            if (! ::InternetReadFile(hFile, buf, sizeof(buf), &read))
            {
                RaiseLastWin32Error();
            }

            if (read == 0)
            {
                break;
            }

            stream->Write(buf, read);
        }
    }
    __finally
    {
        ::InternetCloseHandle(hFile);
    }
}

std::auto_ptr<TJPEGImage> Load(const String& url)
{
    const std::auto_ptr<TInternet> internet(new TInternet());

    const std::auto_ptr<TStream> stream(new TMemoryStream());

    internet->Load(url, stream.get());

    stream->Position = 0;

    std::auto_ptr<TJPEGImage> image(new TJPEGImage());

    image->LoadFromStream(stream.get());

    return image;
}

void Invert(Graphics::TBitmap* bitmap)
{
    for (int y = 0; y < bitmap->Height; ++y)
    {
        for (int x = 0; x < bitmap->Width; ++x)
        {
            DWORD color = bitmap->Canvas->Pixels[x][y];

            color = (color & 0xff000000) + (0x00ffffff - (color & 0x00ffffff));

            bitmap->Canvas->Pixels[x][y] = color;
        }
    }
}

void Invert(const String& url, const String& path)
{
    const std::auto_ptr<TJPEGImage> jpeg = Load(url);

    const std::auto_ptr<Graphics::TBitmap> bitmap(new Graphics::TBitmap());

    bitmap->Assign(jpeg.get());

    Invert(bitmap.get());

    jpeg->Assign(bitmap.get());

    jpeg->SaveToStream(std::auto_ptr<TStream>(new TFileStream(path, fmCreate)).get());
}

int main()
{
    try
    {
        Invert("http://www.python.org/images/success/nasa.jpg", "nasa.jpg");
    }
    catch (Exception& e)
    {
        std::cerr << e.Message.c_str() << std::endl;
    }
}
-1 orz。商用コンパイラが必要だからとかかなあ。そんなにひどいコードじゃないと思うけど・・・

Index

Feed

Other

Link

Pathtraq

loading...